文章目录
SpringBoot整合ActiveMQ
创建一个SpringBoot项目
可以使用IDEA中的快速创建器创建一个SpringBoot项目,就不啰嗦了
pom.xml
- 注意:SpringBoot的版本为:
2.5.3
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--SpringBoot整合ActiveMQ的场景启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!--SpringBoot的测试场景启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--SpringBoot的web场景启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot场景启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
application.yml配置文件
# web占用的端口
server:
port: 8081
spring:
activemq:
# activemq的broker的url
broker-url: tcp://127.0.0.1:61616
# 连接activemq的broker所需的账号和密码
user: admin
password: admin
jms:
#目的地是queue还是topic,false(默认) = queue true = topic
pub-sub-domain: false
# 自定义队列名称,这只是个常量
myqueue: boot-activemq-queue
# 自定义主题名称
mytopic: boot-activemq-topic
queue生产者和消费者
配置目的地的bean
// 让spring管理的注解,相当于spring中在xml 中写了个bean
@Component
// 开启jms适配
@EnableJms
public class ConfigBean {
// 注入配置文件中的 myqueue
@Value("${myqueue}")
private String myQueue ;
@Bean // bean id="" class="…"
public Queue queue(){
return new ActiveMQQueue(myQueue);
}
}
队列生产者代码
@Component
public class Queue_Produce {
// JMS模板
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate ;
// 这个是我们配置的队列目的地
@Autowired
private Queue queue ;
// 发送消息
public void produceMessage(){
// 一参是目的地,二参是消息的内容
jmsMessagingTemplate.convertAndSend(queue,"****"+ UUID.randomUUID().toString().substring(0,6));
}
// 定时任务。每3秒执行一次。非必须代码,仅为演示。
@Scheduled(fixedDelay = 3000)
public void produceMessageScheduled(){
produceMessage();
}
}
队列消费者代码
@Component
public class Queue_Consumer {
// 注册一个监听器,destination指定监听的主题。
@JmsListener(destination = "${myqueue}")
public void receive(TextMessage textMessage) throws Exception{
System.out.println(" *** 消费者收到消息 ***"+textMessage.getText());
}
}
topic生产者和消费者
配置目的地的bean
@Component
//开启JMS
@EnableJms
public class ConfigBean {
@Value("${mytopic}")
private String topicName ;
@Bean
public Topic topic() {
return new ActiveMQTopic(topicName);
}
}
主题生产者代码
@Component
public class Topic_Produce {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate ;
@Autowired
private Topic topic ;
@Scheduled(fixedDelay = 3000)
public void produceTopic(){
jmsMessagingTemplate.convertAndSend(topic,"主题消息"+ UUID.randomUUID().toString().substring(0,6));
}
}
主题消费者代码
@Component
public class Topic_Consumer {
// 注册一个监听器,destination指定监听的主题。
@JmsListener(destination = "${mytopic}")
public void receive(TextMessage textMessage) throws Exception{
System.out.println("消费者受到订阅的主题:"+textMessage.getText());
}
}
测试代码
注意:
-
我们只需要测试
生产者
即可,因为我们在消费者的代码上指定了监听器,当监听到相应的生产者(队列或者主题)生产消息之后,消费者就会自动被调用。 -
当我们测试不同形式的
目的地
的时候记得修改yml配置文件。#目的地是queue还是topic,false(默认) = queue true = topic pub-sub-domain: true
测试代码:
// 加载主类
@SpringBootTest(classes = SpringbootactivemqApplication.class)
// 加载spring的junit
@RunWith(SpringJUnit4ClassRunner.class)
//加载web
@WebAppConfiguration
class SpringbootactivemqApplicationTests {
@Autowired
private Queue_Produce queue_produce;
@Autowired
private Topic_Produce topic_produce;
//测试队列生产者
@Test
public void testQueueProduce() throws Exception{
queue_produce.produceMessage();
}
//测试主题生产者
@Test
public void testTopicProduce() throws Exception{
topic_produce.produceTopic();
}
}