ActiveMQ是Apache提供的一個開源的消息系統,徹底採用Java來實現,所以它能很好地支持JMS(Java Message Service,即Java消息服務)規範;本文將詳細介紹下ActiveMq的安裝、與SpringBoot整合發送隊列消息、發送主題消息的的過程。java
1、Linux下ActiveMQ安裝1.下載並解壓2.運行3.進入管理界面2、發送隊列消息3、發送主題消息linux
wget https://mirrors.tuna.tsinghua.edu.cn/apache//activemq/5.15.9/apache-activemq-5.15.9-bin.tar.gz
tar zxvf apache-activemq-5.15.9-bin.tar.gz
cd bin/
./activemq start
瀏覽器訪問192.168.0.1:8161/admin/,默認用戶名和密碼爲:admin/admin,控制檯截圖以下:web
列表中信息含義以下:spring
Number Of Pending Messages:等待消費的消息 這個是當前未出隊列的數量。apache
Number Of Consumers:消費者 這個是消費者端的消費者數量瀏覽器
Messages Enqueued:進入隊列的消息 進入隊列的總數量,包括出隊列的。ruby
Messages Dequeued:出了隊列的消息 能夠理解爲是消費這消費掉的數量。bash
隊列模式特色:併發
application.properties配置以下:app
#鏈接地址
spring.activemq.broker-url=tcp://192.168.0.1:61616
#若是是點對點(queue),那麼此處默認應該是false,若是發佈訂閱,那麼必定設置爲true
spring.jms.pub-sub-domain=false
ActivemqConfig.java配置:
/**
* 點對點
*/
@Bean
public Queue queue() {
return new ActiveMQQueue("active.queue");
}
消息生產者SendController.java發送代碼以下:
/*
* 發送 隊列消息
*/
@RequestMapping("/sendQueue")
public String sendQueue() {
String message = UUID.randomUUID().toString();
// 指定消息發送的目的地及內容
this.jmsMessagingTemplate.convertAndSend(this.queue, message);
return "消息發送成功!message=" + message;
}
消息消費者QueueCustomerController.java發送代碼以下:
@RestController
public class QueueCustomerController {
/*
* 監聽和接收 隊列消息
*/
@JmsListener(destination="active.queue")
public void readActiveQueue(String message) {
System.out.println("接受到:" + message);
}
}
瀏覽器連續訪問:http://localhost:8080/sendQueue,消息發送成功,消費者接收消息後打印的日誌以下:
接受到:51d85d31-002d-4c9b-87df-a5ea64e8d6da
接受到:1c9dab0c-1d47-4556-95dc-601c8add44fe
接受到:d199ff29-d6ff-41d2-ada0-921d636f7ed1
接受到:4d50ba07-a48a-42b6-a67e-805cdeea662c
接受到:31fc16a9-8aec-4ee6-bbb3-a0ca22c19686
主題模式特色:
application.properties中修改屬性:
spring.jms.pub-sub-domain=true
ActivemqConfig.java配置:
/**
* 發佈/訂閱
*/
@Bean
public Topic topic() {
return new ActiveMQTopic("active.topic");
}
消息生產者SendController.java發送代碼以下:
/*
* 發送 主題消息
*/
@RequestMapping("/sendTopic")
public String sendTopic() {
String message = UUID.randomUUID().toString();
// 指定消息發送的目的地及內容
this.jmsMessagingTemplate.convertAndSend(this.topic, message);
return "消息發送成功!message=" + message;
}
添加兩個消息消費者,TopicCustomerController.java代碼以下:
/*
* 監聽和接收 主題消息1
*/
@JmsListener(destination = "active.topic")
public void readActiveTopic1(String message) {
System.out.println("Customer1接受到:" + message);
}
/*
* 監聽和接收 主題消息2
*/
@JmsListener(destination = "active.topic")
public void readActiveTopic2(String message) {
System.out.println("Customer2接受到:" + message);
}
瀏覽器連續訪問:http://localhost:8080/sendTopic,消息發送成功,兩個消費者接收消息後打印的日誌以下:
Customer1接受到:96c674b7-a310-487b-8088-2c5d049cfabf
Customer2接受到:96c674b7-a310-487b-8088-2c5d049cfabf
Customer1接受到:fee4fde8-6cbe-4d08-b179-e9462f6f1e74
Customer2接受到:fee4fde8-6cbe-4d08-b179-e9462f6f1e74
Customer1接受到:05d79335-ff33-41ec-ba13-a6f5c9d5bba0
Customer2接受到:05d79335-ff33-41ec-ba13-a6f5c9d5bba0
Customer1接受到:f8244df3-5504-478b-b86e-77823ab34dac
Customer2接受到:f8244df3-5504-478b-b86e-77823ab34dac
推薦閱讀
1.編碼神器Lombok,學會後開發效率至少提升一倍!
2.Spring Boot配置過濾器的兩種方式
3.Spring Boot統一異常處理實戰
4.從技術的角度分析下爲何不要在網上發「原圖」
5.Spring Boot之Profile--快速搞定多環境使用與切換
限時領取免費Java相關資料,涵蓋了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo/Kafka、Hadoop、Hbase、Flink等高併發分佈式、大數據、機器學習等技術。
關注下方公衆號便可免費領取: