[原創]深入掌握JMS(五):實戰Topic
與Queue不同的是,Topic實現的是釋出/訂閱模型,在下面的例子中,啟動2個消費者共同監聽一個Topic,然後迴圈給這個Topic中傳送多個訊息。
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
public class TopicTest {
public static void main(String[] args) throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
Connection connection = factory.createConnection();
connection.start();
//建立一個Topic
Topic topic= new ActiveMQTopic("testTopic");
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//註冊消費者1
MessageConsumer comsumer1 = session.createConsumer(topic);
comsumer1.setMessageListener(new MessageListener(){
public void onMessage(Message m) {
try {
System.out.println("Consumer1 get " + ((TextMessage)m).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
//註冊消費者2
MessageConsumer comsumer2 = session.createConsumer(topic);
comsumer2.setMessageListener(new MessageListener(){
public void onMessage(Message m) {
try {
System.out.println("Consumer2 get " + ((TextMessage)m).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
//建立一個生產者,然後傳送多個訊息。
MessageProducer producer = session.createProducer(topic);
for(int i=0; i<10; i++){
producer.send(session.createTextMessage("Message:" + i));
}
}
}
執行後得到下面的輸出結果:
Consumer1 get Message:0
Consumer2 get Message:0
Consumer1 get Message:1
Consumer2 get Message:1
Consumer1 get Message:2
Consumer2 get Message:2
Consumer1 get Message:3
Consumer2 get Message:3
Consumer1 get Message:4
Consumer2 get Message:4
Consumer1 get Message:5
Consumer2 get Message:5
Consumer1 get Message:6
Consumer2 get Message:6
Consumer1 get Message:7
Consumer2 get Message:7
Consumer1 get Message:8
Consumer2 get Message:8
Consumer1 get Message:9
Consumer2 get Message:9
說明每一個訊息都會被所有的消費者消費。
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
public class TopicTest {
public static void main(String[] args) throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
Connection connection = factory.createConnection();
connection.start();
//建立一個Topic
Topic topic= new ActiveMQTopic("testTopic");
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//註冊消費者1
MessageConsumer comsumer1 = session.createConsumer(topic);
comsumer1.setMessageListener(new MessageListener(){
public void onMessage(Message m) {
try {
System.out.println("Consumer1 get " + ((TextMessage)m).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
//註冊消費者2
MessageConsumer comsumer2 = session.createConsumer(topic);
comsumer2.setMessageListener(new MessageListener(){
public void onMessage(Message m) {
try {
System.out.println("Consumer2 get " + ((TextMessage)m).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
//建立一個生產者,然後傳送多個訊息。
MessageProducer producer = session.createProducer(topic);
for(int i=0; i<10; i++){
producer.send(session.createTextMessage("Message:" + i));
}
}
}
執行後得到下面的輸出結果:
Consumer1 get Message:0
Consumer2 get Message:0
Consumer1 get Message:1
Consumer2 get Message:1
Consumer1 get Message:2
Consumer2 get Message:2
Consumer1 get Message:3
Consumer2 get Message:3
Consumer1 get Message:4
Consumer2 get Message:4
Consumer1 get Message:5
Consumer2 get Message:5
Consumer1 get Message:6
Consumer2 get Message:6
Consumer1 get Message:7
Consumer2 get Message:7
Consumer1 get Message:8
Consumer2 get Message:8
Consumer1 get Message:9
Consumer2 get Message:9
說明每一個訊息都會被所有的消費者消費。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10742815/viewspace-580272/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [原創]深入掌握JMS(四):實戰Queue
- [原創]深入掌握JMS(七):DeliveryMode例子
- [原創] 深入掌握JMS(一):JSM基礎JS
- [原創]深入掌握JMS(十一):TemporaryQueue和TemporaryTopic
- 深入淺出JMS(一)——JMS簡單介紹
- Flutter完整開發實戰詳解(五、 深入探索)Flutter
- 大資料時代怎樣深入淺出的掌握Scala實戰技能?大資料
- Oracle中Hint深入理解(原創)Oracle
- 【原創】彼得德魯克《管理的實踐》札記(五)
- 深入掌握service
- 創新實訓(五)
- 【原創】ORACLE 深入解析10053事件Oracle事件
- 求助:JMS的實現
- Express 實戰(五):路由Express路由
- Nginx實戰(五) 反向代理Nginx
- Hollis原創|深入分析Java的編譯原理Java編譯原理
- (原創)建立windows域—深入理解域概念薦Windows
- ItemDecoration深入解析與實戰(一)
- JMS學習(一):初識JMS
- 【原創】003 | 搭上基於SpringBoot事務思想實戰專車Spring Boot
- 【原創】【深入淺出系列】之程式碼可讀性
- Core Animation實戰五(變換)
- 深入掌握Java中的enumJava
- 實戰JBuilder7+WebLogic7(四)續JMS+Message-Driven Bean (轉)UIWebBean
- [深入理解Java虛擬機器]第五章 調優案例分析與實戰Java虛擬機
- banq的J2EE實戰開發原創教程是不是錯了?????
- 深入Django專案實戰與最佳實踐Django
- nsq topic
- Elasticsearch搜尋功能的實現(五)-- 實戰Elasticsearch
- client-go實戰之五:DiscoveryClientclientGo
- kubebuilder實戰之五:operator編碼UI
- 【原創】專案五w1r3s.v1.0
- 深入掌握K8S PodK8S
- 如何實現文章AI偽原創?AI
- 實施在系統外(原創)
- 五維空間較量還原真實戰場!《360政企安全實戰攻防演習》紀錄片即將上線
- Community Cloud零基礎學習(五)Topic(主題)管理UnityCloud
- ItemDecoration深入解析與實戰(二)—— 實際運用