Build infrastructure
New producers and consumers modules:
modify pom file, increased reliance jar package:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
<!--spring boot整合activemq的jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
</dependencies>
News producer
Producer application.yml
:
server:
port: 8081 # 端口号
spring:
activemq:
# activemq的broker的url
broker-url: tcp://192.168.0.166:61616
# 连接activemq的broker所需的账号和密码
user: admin
password: admin
jms:
# 目的地是queue还是topic, false(默认) = queue true = topic
pub-sub-domain: false
# 自定义队列名称。这只是个常量
queueName: boot-activemq-queue
# 主题名称
topicName: boot-activemq-topic
Producers configuration class:
package com.chaytech.producer;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;
@Component
@EnableJms // 开启jms
public class Config {
@Value("${queueName}")
private String queueName;
@Value("${topicName}")
private String topicName;
@Bean
public ActiveMQQueue activeMQQueue(){
return new ActiveMQQueue(queueName);
}
@Bean
public ActiveMQTopic activeMQTopic(){
return new ActiveMQTopic(topicName);
}
}
Manufacturer messaging Controller:
package com.chaytech.producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Queue;
import javax.jms.Topic;
/**
* 消息生产者
*
* @author Chency
* @email [email protected]
* @Date 2020/03/25 20:55
*/
@RestController
@RequestMapping("/producer")
public class ProducerController {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@Autowired
private Topic topic;
/**
* 发送队列消息
*
* @param msg
*/
@GetMapping("/sendQueueMessage/{msg}")
public void sendQueueMessage(@PathVariable String msg){
jmsMessagingTemplate.convertAndSend(this.queue, msg);
}
/**
* 发送主题消息
*
* @param msg
*/
@GetMapping("/sendTopicMessage/{msg}")
public void sendTopicMessage(@PathVariable String msg){
jmsMessagingTemplate.convertAndSend(this.topic, msg);
}
}
Start categories:
package com.chaytech;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* 生产者启动类
*
* @author Chency
* @email [email protected]
* @Date 2020/03/25 21:04
*/
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
Consumer news
Consumers application.yml
:
server:
port: 8091 # 端口号
spring:
activemq:
# activemq的broker的url
broker-url: tcp://192.168.0.166:61616
# 连接activemq的broker所需的账号和密码
user: admin
password: admin
jms:
# 目的地是queue还是topic, false(默认) = queue true = topic
pub-sub-domain: false
# 自定义队列名称。这只是个常量
queueName: boot-activemq-queue
# 主题名称
topicName: boot-activemq-topic
Consumers message listener, when the producer has issued a new message, here are automatically consumption:
package com.chaytech.consumer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.TextMessage;
/**
* 消费者消息监听器
*
* @author Chency
* @email [email protected]
* @Date 2020/03/25 21:14
*/
@Component
public class ConsumerListener {
@JmsListener(destination = "${queueName}")
public void receiveQueueMsg(TextMessage textMessage) throws JMSException {
System.out.println("消费者监听到的队列消息:" + textMessage.getText());
}
@JmsListener(destination = "${topicName}")
public void receiveTopicMsg(TextMessage textMessage) throws JMSException {
System.out.println("消费者监听到的主题消息:" + textMessage.getText());
}
}
Start categories:
package com.chaytech;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 消费者启动类
*
* @author Chency
* @email [email protected]
* @Date 2020/03/25 21:11
*/
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
Switching the message queue message theme
We are in front of the queue to which a message that if the need to send a message theme, how to do it?
We only need to modify configuration files producers and consumers as well as the destination when sending a message directed to Topic:
# 目的地是queue还是topic, false(默认) = queue true = topic
spring.jms.pub-sub-domain: false
package com.chaytech.producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Queue;
import javax.jms.Topic;
/**
* 消息生产者
*
* @author Chency
* @email [email protected]
* @Date 2020/03/25 20:55
*/
@RestController
@RequestMapping("/producer")
public class ProducerController {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Topic topic;
/**
* 发送主题消息
*
* @param msg
*/
@GetMapping("/sendTopicMessage/{msg}")
public void sendTopicMessage(@PathVariable String msg){
jmsMessagingTemplate.convertAndSend(this.topic, msg);
}
}
Timing message delivery
Earlier we talked about are triggered message delivery, then activeMQ also supports regular message delivery, sample code as follows:
package com.chaytech.producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
import java.util.UUID;
/**
* 定时投递消息
*
* @author Chency
* @email [email protected]
* @Date 2020/03/25 21:07
*/
@Component
public class ProducerScheduled {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
/**
* 定时投递消息,间隔3秒
*/
@Scheduled(fixedDelay = 3000)
public void scheduledSendQueueMessage(){
jmsMessagingTemplate.convertAndSend(this.queue, "定时投递消息 —> " + UUID.randomUUID());
}
}
Modifying the boot type, the opening timing of delivery:
package com.chaytech;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* 生产者启动类
*
* @author Chency
* @email [email protected]
* @Date 2020/03/25 21:04
*/
@SpringBootApplication
@EnableScheduling // 开启定时投递消息
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}