springboot2.0x全系列一springboot2.0x整合ActiveMQ(簡單整合與應用)
ActiveMQ
ActiveMQ 是Apache出品,最流行的,能力強勁的開源訊息匯流排。ActiveMQ 是一個完全支援JMS1.1和J2EE 1.4規範的 JMS Provider實現,儘管JMS規範出臺已經是很久的事情了,但是JMS在當今的J2EE應用中間仍然扮演著特殊的地位。
特性
多種語言和協議編寫客戶端。語言: Java,C,C++,C#,Ruby,Perl,Python,PHP。應用協議: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
完全支援JMS1.1和J2EE 1.4規範 (持久化,XA訊息,事務)
對Spring的支援,ActiveMQ可以很容易內嵌到使用Spring的系統裡面去,而且也支援Spring2.0的特性
通過了常見J2EE伺服器(如 Geronimo,JBoss 4,GlassFish,WebLogic)的測試,其中通過JCA 1.5 resource adaptors的配置,可以讓ActiveMQ可以自動的部署到任何相容J2EE 1.4 商業伺服器上
支援多種傳送協議:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
支援通過JDBC和journal提供高速的訊息持久化
從設計上保證了高效能的叢集,客戶端-伺服器,點對點
支援Ajax
支援與Axis的整合
可以很容易的呼叫內嵌JMS provider,進行測試
更多關於 ActiveMQ 的內容可以點選這裡。
首先新增依賴:
<!--ActiveMq依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <!-- 啟用JMS 的池化, 就一定要加上這個 jar--> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> </dependency> <!--ActiveMq依賴結束-->
application.yml新增如下內容
# activemq
activemq:
broker-url: tcp://localhost:61616
user: admin
password: admin
這裡的埠號:61616 是預設的埠號,有些類似於es的和網頁訪問不一樣網頁訪問的預設埠號:8161,寫錯誤了會報47號錯誤
配置中還可以新增很多的東西 有興趣的可以自行研究拓展下
新增
ActivemqConfig config類
package com.springboot2.config; import javax.jms.ConnectionFactory; import javax.jms.Destination; import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQTopic; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.config.JmsListenerContainerFactory; @Configuration public class ActivemqConfig { /** * 自定義了4個Destination,兩個queue,兩個topic */ @Bean public Destination queue1() { return new ActiveMQQueue("queue-1"); } @Bean public Destination queue2() { return new ActiveMQQueue("queue-2"); } @Bean public Destination topic1() { return new ActiveMQTopic("topic-1"); } @Bean public Destination topic2() { return new ActiveMQTopic("topic-2"); } /** * JmsListener註解預設只接收queue訊息,如果要接收topic訊息,需要設定containerFactory */ @Bean public JmsListenerContainerFactory<?> topicListenerContainer(ConnectionFactory activeMQConnectionFactory) { DefaultJmsListenerContainerFactory topicListenerContainer = new DefaultJmsListenerContainerFactory(); topicListenerContainer.setPubSubDomain(true); topicListenerContainer.setConnectionFactory(activeMQConnectionFactory); return topicListenerContainer; } }
注意這裡bean下面的內容非常重要,不寫的話接受普通的queue訊息沒有問題,但是接收不到topic的訊息
下面寫生產類,也就是訊息的產生者傳送類:
ProviderController
package com.springboot2.controller;
import javax.annotation.Resource;
import javax.jms.Destination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 通過頁面訪問的方式生產訊息
*/
@RestController
public class ProviderController {
@Autowired
private JmsTemplate jmsTemplate;
@Resource(name = "queue1")
private Destination queue1;
@Resource(name = "topic1")
private Destination topic1;
@GetMapping("/send/queue1")
public String send1() {
jmsTemplate.convertAndSend(queue1, "hello queue-1");
return "ok";
}
@GetMapping("/send/topic1")
public String send2() {
jmsTemplate.convertAndSend(topic1, "hello topic-1");
return "ok";
}
}
下面是消費類,也就是接收訊息類
MessageHandler
package com.springboot2.controller;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* 接收處理訊息
*/
@Component
public class MessageHandler {
@JmsListener(destination="queue-1")
public void recieve1(String message) {
System.out.println("###################" + message + "###################");
}
@JmsListener(destination="topic-1", containerFactory="topicListenerContainer")
public void recieve2(String message) {
System.out.println("###################" + message + "###################");
}
}
可以看到上面的接收queue的訊息和下面接收topic的訊息有些不同
啟動專案後先去生產訊息,這裡的jmslister會自動去列印訊息
這裡其實還有很多的拓展,例如:接收到訊息後可以返回訊息到mq中等等,可以根據自己的專案需求進行相關的修改,mq在mac下的安裝方法已經在我的部落格中了,有需要可以自取
專案地址:https://download.csdn.net/download/qq_34077993/10751216