消息中間件學(xué)習(xí)之JMS(3)

本篇主要介紹PUB/SUB模式

PUB/SUB模式區(qū)別于P2P模式,PUB/SUB模式下一個(gè)消息可以被多個(gè)消費(fèi)者消息。消息生產(chǎn)者需要指定一個(gè)主題,即Topic。所有訂閱該主題的消費(fèi)者都能消費(fèi)該生產(chǎn)者發(fā)送的消息。上代碼:

session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Topic topic = new ActiveMQTopic("LzyTopic");//此處創(chuàng)建一個(gè)主題

consumer1 = session.createConsumer(topic);//消費(fèi)者1

consumer1.setMessageListener(new MessageListener() {

? ? ? ? ? ? ? @Override

? ? ? ? ? ? ? ? public void onMessage(Message arg0) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?TextMessage tm = (TextMessage) arg0;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if (tm != null) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? try {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?System.out.println("consume1 and topic is"+ tm.getText());

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } catch (JMSException e) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?e.printStackTrace();

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}

? ? ? ? ? ? ? ? ? ? ? ? ? }});///消費(fèi)者1增加消息監(jiān)聽

consumer2 = session.createConsumer(topic);//消費(fèi)者2

consumer2.setMessageListener(new MessageListener() {

? ? ? ? ? ?@Override

? ? ? ? ? ? public void onMessage(Message arg0) {

? ? ? ? ? ? ? ? ? ? ? TextMessage tm = (TextMessage) arg0;

? ? ? ? ? ? ? ? ? ? ? if (tm != null) {

? ? ? ? ? ? ? ? ? ? ? ?try {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?System.out.println("consume2 and topic is"+ tm.getText());

? ? ? ? ? ? ? ? ? ? ? ? } catch (JMSException e) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?e.printStackTrace();

? ? ? ? ? ? ? ? ? ? ? ?}

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ?}});//消費(fèi)者2增加消息監(jiān)聽

創(chuàng)建了2個(gè)消費(fèi)者,都訂閱了名字為"LzyTopic"的topic。接下來(lái)的代碼在創(chuàng)建一個(gè)生產(chǎn)者,同樣也是指定topic。

producer = session.createProducer(topic);//和消費(fèi)者同樣的topic

for (int i = 0; i <5; i++) {

? ? ? TextMessage tm=session.createTextMessage("Message"+i);

? ? ? producer.send(tm);//發(fā)送消息

}

打印結(jié)果如下:

consume1 and topic isMessage0

consume2 and topic isMessage0

consume1 and topic isMessage1

consume2 and topic isMessage1

consume1 and topic isMessage2

consume2 and topic isMessage2

consume1 and topic isMessage3

consume2 and topic isMessage3

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1 消息隊(duì)列概述 消息隊(duì)列中間件是分布式系統(tǒng)中重要的組件,主要解決應(yīng)用耦合,異步消息,流量削鋒等問題。實(shí)現(xiàn)高性能,...
    Bobby0322閱讀 11,055評(píng)論 0 24
  • 一、 消息隊(duì)列概述 消息隊(duì)列中間件是分布式系統(tǒng)中重要的組件,主要解決應(yīng)用耦合、異步消息、流量削鋒等問題。實(shí)現(xiàn)高性能...
    步積閱讀 57,442評(píng)論 10 138
  • ActiveMQ簡(jiǎn)單的示例 下載ActiveMQ 去官方網(wǎng)站下載:http://activemq.apache.o...
    花丶小偉閱讀 261評(píng)論 0 0
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,562評(píng)論 19 139
  • 《形而上學(xué)》卷七·章一至章六 卷七詳細(xì)討論了本體的概念。在卷五第七章中亞里士多德已經(jīng)將“是(存在)”劃分為了四類即...
    YUE__閱讀 1,736評(píng)論 0 2

友情鏈接更多精彩內(nèi)容