springboot引入activiemq(一)
阿新 • • 發佈:2018-11-01
首先安裝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,的啟發