1、springboot和activemq的使用相對來說比較友善了,我在網上看了很多其他的資料,但是自己寫出來總是有點問題是以,這裡重點描述一下遇到的一些問題。
2、至于activemq的搭建和springmvc的搭建可以參考:
http://www.cnblogs.com/ll409546297/p/6898155.html3、項目的搭建和使用
1)目錄結構
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcukTYxUmNwITZyUDOwEGM5kDM3cDN1gjM1UmY0AzYllDNfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.png)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.troy</groupId>
<artifactId>springbootactivemq</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>
</dependencies>
</project>
3)基本的配置application.yml
server:
port: 8090
spring:
activemq:
broker-url: tcp://192.168.5.10:61616
user: admin
password: admin
jms:
pub-sub-domain: true
說明:jms:pub-sub-domain:true(為true時是topic模式,為false是queue模式)
4)這裡介紹兩種發送資料的方式
(1)确定發送方式,這種需要做配置
topic模式:ActiveMqTopicConfig.class
@Configuration
public class ActiveMqTopicConfig {
@Bean
public Topic topic(){
return new ActiveMQTopic("topic");
}
}
queue模式:ActiveMqQueueConfig.class
@Configuration
public class ActiveMqQueueConfig {
@Bean
public Queue queue(){
return new ActiveMQQueue("queue");
}
}
發送:ActiveMqClient.class
@RestController
@RequestMapping("/api")
public class ActiveMqClient {
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
private Topic topic;
@Autowired
private Queue queue;
@RequestMapping("/send")
public void send(){
jmsTemplate.convertAndSend(this.topic,"發送的topic資料!");
jmsTemplate.convertAndSend(this.queue,"發送的queue資料!");
}
}
注:這裡我友善測試,就直接寫的調用的方式!
(2)直接發送的方式:ActiveMqClient.class
@RestController
@RequestMapping("/api")
public class ActiveMqClient {
@Autowired
private JmsTemplate jmsTemplate;
@RequestMapping("/send")
public void send(){
jmsTemplate.send("topic", new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage textMessage = session.createTextMessage();
textMessage.setText("send data");
return textMessage;
}
});
}
}
5)接收資料:ActiveMqServer.class
@Component
public class ActiveMqServer {
@JmsListener(destination = "topic")
public void receiveTopic(String message) {
System.out.println("監聽topic=============監聽topic");
System.out.println(message);
}
@JmsListener(destination = "queue")
public void receiveQueue(String message) {
System.out.println("監聽queue=============監聽queue");
System.out.println(message);
}
}
注:也可以實作MessageListener接口來接收資料,但是需要配置的東西和springmvc差不多,這裡不做介紹。