SpringBoot整合ActiveMQ
SpringBoot版本号2.5.4
一、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
</dependency>
二、配置application.yml文件
spring:
activemq:
broker-url: tcp://localhost:61316
close-timeout: 5000
in-memory: false
user: admin
password: admin
queue-name: my-queue
topic-name: my-topic
pool:
enabled: true
max-connections: 10
三、创建ActiveMQ配置类
代码如下(示例):
package com.springboot.activemq;
import javax.jms.Destination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@Autowired
private Destination topic;
@Autowired
private Destination queue;
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@RequestMapping("/sendQueue")
public void sendQueue(String msg) {
System.out.println("发送队列消息");
jmsMessagingTemplate.convertAndSend(queue, msg);
}
@RequestMapping("/sendTopic")
public void sendTopic(String msg) {
System.out.println("发送主题消息:");
jmsMessagingTemplate.convertAndSend(topic, msg);
}
}
四、创建生产者
package com.springboot.activemq;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@JmsListener(destination = "${spring.activemq.queue-name}", containerFactory = "queueListenerContainerFactory")
public void receiveActiveQueue(String message) {
System.out.println("接受到:" + message);
}
@JmsListener(destination = "${spring.activemq.topic-name}", containerFactory = "topicListenerContainerFactory")
public void receiveTopic(String message) {
System.out.println("接受到:" + message);
}
@JmsListener(destination = "${spring.activemq.topic-name}", containerFactory = "topicListenerContainerFactory")
public void receiveTopic2(String message) {
System.out.println("接受到:" + message);
}
}
五、创建消费者
package com.springboot.activemq;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@JmsListener(destination = "${spring.activemq.queue-name}", containerFactory = "queueListenerContainerFactory")
//或 @JmsListener(destination = "my-queue", containerFactory = "queueListenerContainerFactory")
public void reciveActiveQueue(String message) {
System.out.println("接受到:" + message);
}
@JmsListener(destination = "my-topic", containerFactory = "topicListenerContainerFactory")
public void receiveTopic(String message) {
System.out.println("接受到:" + message);
}
@JmsListener(destination = "my-topic", containerFactory = "topicListenerContainerFactory")
public void receiveTopic2(String message) {
System.out.println("接受到:" + message);
}
}
Queue模式测试:http://localhost:8888/sendQueue?msg=queueTest
Topic模式测试:http://localhost:8888/sendTopic?msg=topicTest