SpringBoot中如何使用ActiveMQ呢?
下文笔者讲述SpringBoot中使用ActiveMQ的方法及示例分享
SpringBoot使用ActiveMQ的实现思路
1.引入activeMQ的相应依赖 2.编写activeMQ相应的配置文件 3.定义MQ相应的Queue例:SpringBoot使用ActiveMQ
1.创建项目,添加ActiveMQ依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2.在application.properties中进行连接配置
spring.activemq.broker-url=tcp://localhost:61616 #端口
spring.activemq.packages.trust-all=true #信任所有的包
spring.activemq.user=admin #用户名
spring.activemq.password=admin #密码
//提供一个消息队列Bean
//该Bean的实例就由ActiveMQ提供:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
Queue queue(){
return new ActiveMQQueue("amq");
}
}
//创建一个JMS组件来完成消息的发送和接收:
@Component
public class JmsComponent {
@Autowired
JmsMessagingTemplate messagingTemplate;
@Autowired
Queue queue;
public void send(Message msg){
messagingTemplate.convertAndSend(this.queue, msg);
}
@Jmslistener(destination = "amq")
public void receive(Message msg){
System.out.println("receive:" + msg);
}
}
//4.测试
@Autowired
JmsComponent jmsComponent;
@Test
public void test(){
Message msg = new Message();
msg.setContent("hello java265.com!");
msg.setDate(new Date());
jmsComponent.send(msg);
}
----将输出以下信息
receive:com.example.demo.pojo.Message@23114io
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


