首先安装activiemq服务器 下载地址如下:http://activemq.apache.org/download.html,笔者安装的windows版本的,解压一下就可以,然后到这个目录下,双击activemq.bat,即 mq服务启动,访问http://localhost:8161/admin/,如果能出现如下监控平台,则代表安装成功
下面我们就一步一步的集成activemq。
首先简历springboot工程
pom文件引入jar包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</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>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
</dependencies>
重写 bean依赖, 以及默认springboot直接收queue消息,如果要接收topic,需要设置containerfactory
/**
* 点对点
* @return
*/
@Bean
public Queue queue(){
return new ActiveMQQueue("zh-queue");
}
/**
* 发布/订阅
* @return
*/
@Bean
public Topic topic(){
return new ActiveMQTopic("zh-topic");
}
/**TODO
* * JmsListener注解默认只接收queue消息,如果要接收topic消息,需要设置containerFactory
* */
@Bean
public JmsListenerContainerFactory<?> topicListenerContainer(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory topicListenerContainer = new DefaultJmsListenerContainerFactory();
topicListenerContainer.setPubSubDomain(true);
topicListenerContainer.setConnectionFactory(activeMQConnectionFactory);
return topicListenerContainer;
}
2 然后sevice层负责发送消息封装了发送消息方法:
@Override
public void sendMessage(Destination destination, String message) {
jmsMessagingTemplate.convertAndSend(destination, message);
}
以及一个监听消息方法
@JmsListener(destination = "return-queue", containerFactory="topicListenerContainer")
public void Message(String message) {
System.out.println("product收到参数了:" + message);
}
然后就是 消费接收消息方法 1) 队列消息
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "zh-queue")
public void Message(String message) {
System.out.println("Consumer收到:" + message);
}
2)topic消息
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "zh-topic", containerFactory="topicListenerContainer")
//会将接收到的消息发送到指定的路由目的地,所有订阅该消息的用户都能收到,属于广播。
@SendTo("return-queue")
public String receiveQueue(String text) {
System.out.println("Consumer2收到:"+text);
return "Consumer2收到!";
}
然后就是contontroller层注入消息发送类
import com.mp.activemqdemo.activemq.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
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.Topic;
import javax.jms.Queue;
@RestController
@RequestMapping("/activeMq")
public class ActiveMqController {
@Autowired
private Queue queue;
@Autowired
private Topic topic;
@Autowired
private ProductService productService;
@GetMapping("/queue/{msg}")
public void sendQueue(@PathVariable("msg") String msg) {
productService.sendMessage(this.queue, msg);
}
@GetMapping("/topic/{msg}")
public void sendTopic(@PathVariable("msg") String msg) {
productService.sendMessage(this.topic, msg);
}
}
然,application.properties
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
spring.activemq.pool.enabled=true
#默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置
#spring.jms.pub-sub-domain=true
这样,我们基本已经集成了。
然后我们就可以启动项目,浏览器访问,对应contoroller的url
localhost:8080/activeMq/topic/nihao
localhost:8080/activeMq/queue/nihao
这样基本就成功了。 源码地址详情见git
再此,感谢
https://blog.csdn.net/eumenides_/article/details/78356170,的启发