1、队列通讯
1.1 生产者
1.1.1 添加依赖
首先创建一个SpringBoot项目,这里我将项目命名为boot_mq_produce,添加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
这里我的SpringBoot版本为2.4.0
1.1.2 创建配置文件
在application.properties文件中添加如下配置:
# Tomcat端口
server.port=7777
# ActiveMQ服务器地址
spring.activemq.broker-url=tcp://192.168.1.2:61617
spring.activemq.user=admin
spring.activemq.password=admin
# false=Queue true=Topic
spring.jms.pub-sub-domain=false
myqueue=boot-activemq-queue
1.1.3 创建配置类
新建一个配置类ConfigBean,其代码如下:
@Component
@EnableJms
public class ConfigBean {
@Value("${myqueue}")
private String myQueue;
@Bean
public Queue queue(){
return new ActiveMQQueue(myQueue);
}
}
1.1.4 创建生产者
新建生产者类QueueProducer,其代码如下:
@Component
public class QueueProducer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
public void produceMsg(){
jmsMessagingTemplate.convertAndSend(queue, "xxxx" + UUID.randomUUID().toString().substring(0,6));
}
@Scheduled(fixedDelay = 3000)
public void produceMsgScheduled(){
jmsMessagingTemplate.convertAndSend(queue, "****" + UUID.randomUUID().toString().substring(0,6));
System.out.println("scheduled.");
}
}
通过自动注入获取配置类中的Queue,以及导入依赖中的Jms消息模版,使用消息模板向队列中发送消息。
可以看到代码中有两个发送消息的函数:
- produceMsg:调用本函数,发送一条消息
- produceMsgScheduled:定时发送消息,三秒一条
1.1.5 修改启动类
因为创建生产者的时候,用到了定时发送,因此在启动类上要加上注解@EnableScheduling
1.2 消费者
1.2.1 添加依赖
创建一个SpringBoot项目,这里我将项目命名为boot_mq_consume,添加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
SpringBoot版本也为2.4.0
1.2.2 创建配置文件
server.port=8888
spring.activemq.broker-url=tcp://192.168.1.2:61617
spring.activemq.user=admin
spring.activemq.password=admin
# false=Queue true=Topic
spring.jms.pub-sub-domain=false
myqueue=boot-activemq-queue
1.2.3 创建消费者
新建消费者类QueueConsumer,其代码如下:
@Component
public class QueueConsumer {
@JmsListener(destination = "${myqueue}")
public void receive(TextMessage textMessage) throws JMSException {
System.out.println(textMessage.getText());
}
}
使用注解JmsListener监听队列。
1.3 测试
参照Java编码实现ActiveMQ通讯,分别执行生产者和消费者的启动类。
2、话题通讯
使用Topic通讯和使用Queue通讯在代码上大致是相同的,因此只需在上述项目上进行相应修改即可。
2.1 生产者
对生产者的修改,都是在修改boot_mq_produce项目
2.1.1 修改配置文件
# Tomcat端口
server.port=7777
# ActiveMQ服务器地址
spring.activemq.broker-url=tcp://192.168.1.2:61617
spring.activemq.user=admin
spring.activemq.password=admin
# false=Queue true=Topic
spring.jms.pub-sub-domain=true
mytopic=boot-activemq-topic
2.1.2 修改配置类
@Component
@EnableJms
public class ConfigBean {
@Value("${mytopic}")
private String myTopic;
@Bean
public Topic topic(){
return new ActiveMQTopic(myTopic);
}
}
2.1.3 创建生产者
新建生产者类QueueProducer,其代码如下:
@Component
@EnableJms
public class TopicProducer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Topic topic;
@Scheduled(fixedDelay = 3000)
public void produceTopic(){
jmsMessagingTemplate.convertAndSend(topic, "topic: " + UUID.randomUUID().toString().substring(0, 6));
}
}
2.2 消费者
2.2.1 修改配置文件
server.port=8888
spring.activemq.broker-url=tcp://192.168.1.2:61617
spring.activemq.user=admin
spring.activemq.password=admin
# false=Queue true=Topic
spring.jms.pub-sub-domain=true
mytopic=boot-activemq-topic
2.2.2 创建消费者
新建消费者类TopicConsumer,其代码如下:
@Component
public class TopicConsumer {
@JmsListener(destination = "${mytopic}")
public void receive(TextMessage textMessage){
try {
System.out.println(textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
2.3 测试
参照Java编码实现ActiveMQ通讯,分别执行生产者和消费者的启动类。
3、思考
其实我觉得还是这篇博客比较重要,Java编码实现ActiveMQ通讯,因为这是基础,不管是Spring整合还是SpringBoot整合,只要搞懂了基础,整合完全不是问题。