一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - ActiveMQ結合Spring收發消息的示例代碼

ActiveMQ結合Spring收發消息的示例代碼

2021-06-03 11:23zy_lebron Java教程

這篇文章主要介紹了ActiveMQ結合Spring收發消息的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

activemq 結合 spring 收發消息

直接使用 activemq 的方式需要重復寫很多代碼,且不利于管理,spring 提供了一種更加簡便的方式————spring jms ,通過它可以更加方便地使用 activemq。

maven 依賴

結合spring使用activemq的依賴如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- spring jms -->
<dependency>
  <groupid>org.springframework</groupid>
  <artifactid>spring-jms</artifactid>
  <version>${spring.version}</version>
</dependency>
<!-- xbean 如<amq:connectionfactory /> -->
<dependency>
  <groupid>org.apache.xbean</groupid>
  <artifactid>xbean-spring</artifactid>
  <version>3.16</version>
</dependency>
<!-- actiivemq -->
<dependency>
  <groupid>org.apache.activemq</groupid>
  <artifactid>activemq-core</artifactid>
  <version>5.7.0</version>
</dependency>
<dependency>
  <groupid>org.apache.activemq</groupid>
  <artifactid>activemq-pool</artifactid>
  <version>5.7.0</version>
</dependency>

activemq.xml 文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
    xmlns:amq="http://activemq.apache.org/schema/core"
    xsi:schemalocation="http://www.springframework.org/schema/beans
 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 
 
http://activemq.apache.org/schema/core
 
 
http://activemq.apache.org/schema/core/activemq-core-5.12.1.xsd">
 
  <!-- activemq 連接工廠 -->
  <amq:connectionfactory id="amqconnectionfactory"
              brokerurl="tcp://localhost:61616"
              username="admin"
              password="admin" />
  <!-- 提高效率,配置jms連接工廠 -->
  <bean id="connectionfactory" class="org.springframework.jms.connection.cachingconnectionfactory">
    <constructor-arg ref="amqconnectionfactory" />
    <property name="sessioncachesize" value="100" />
  </bean>
  <!-- 定義消息隊列(queue)-->
  <!-- <bean id="queuedestination" class="org.apache.activemq.command.activemqqueue">
    <!– 設置消息隊列的名字 –>
    <constructor-arg value="queue-zy"/>
  </bean>-->
  <!--定義主題(topic)-->
  <bean id="topicdestination" class="org.apache.activemq.command.activemqtopic">
    <constructor-arg value="topic-zy"/>
  </bean>
  <!-- 配置jms模板(queue),spring提供的jms工具類,利用它發送、接收消息。 -->
  <bean id="jmstemplate" class="org.springframework.jms.core.jmstemplate">
    <property name="connectionfactory" ref="connectionfactory" />
    <property name="defaultdestination" ref="topicdestination" />
    <property name="receivetimeout" value="10000" />
    <!-- true是topic,false是queue,默認是false -->
    <property name="pubsubdomain" value="true" />
  </bean>
  <!-- 配置消息隊列監聽者(queue or topic) -->
  <bean id="messagelistener" class="com.service.topicmessagelistener" />
  <!-- 顯示注入消息監聽容器,配置連接工廠,監聽的目標是queuedestination,監聽器是上面定義的監聽器 -->
  <bean id="listenercontainer"
     class="org.springframework.jms.listener.defaultmessagelistenercontainer">
    <property name="connectionfactory" ref="connectionfactory" />
    <property name="destination" ref="topicdestination" />
    <property name="messagelistener" ref="messagelistener" />
  </bean>
</beans>

配置 connectionfactory

connectionfactory 是 spring 用于創建到 jms 服務器鏈接的,spring 提供了多種 connectionfactory。

?
1
2
3
4
5
6
7
8
9
10
<!-- activemq 連接工廠 -->
<amq:connectionfactory id="amqconnectionfactory"
            brokerurl="tcp://localhost:61616"
            username="admin"
            password="admin" />
<!-- 提高效率,配置jms連接工廠 -->
<bean id="connectionfactory" class="org.springframework.jms.connection.cachingconnectionfactory">
  <constructor-arg ref="amqconnectionfactory" />
  <property name="sessioncachesize" value="100" />
</bean>

配置queue

?
1
2
3
4
<bean id="queuedestination" class="org.apache.activemq.command.activemqqueue">
    <!-- 設置消息隊列的名字 -->
    <constructor-arg value="queue-zy"/>
</bean>

配置topic

?
1
2
3
<bean id="topicdestination" class="org.apache.activemq.command.activemqtopic">
    <constructor-arg value="topic-zy"/>
</bean>

配置jms消息模板——jmstemplate

?
1
2
3
4
5
6
7
8
<!-- 配置jms模板,spring提供的jms工具類,利用它發送、接收消息-->
<bean id="jmstemplate" class="org.springframework.jms.core.jmstemplate">
  <property name="connectionfactory" ref="connectionfactory" />
  <property name="defaultdestination" ref="queuedestination" />
  <!--<property name="defaultdestination" ref="topicdestination" />-->
  <property name="receivetimeout" value="10000" />
  <property name="pubsubdomain" value="false" /><!-- true是topic,false是queue,默認是false -->
</bean>

最后,在 applicationcontext.xml 中引入配置好的 activemq.xml

?
1
<import resource="activemq.xml" />

以上就是配置文件相關的,下面是具體的業務代碼。

消息生產者服務

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@service
public class producerservice {
  @autowired
  private jmstemplate jmstemplate;
  //使用默認目的地
  public void sendmessagedefault(final string msg){
    destination destination = jmstemplate.getdefaultdestination();
    system.out.println("向隊列: " + destination + " 成功發送一條消息");
    jmstemplate.send(new messagecreator() {
      public message createmessage(session session) throws jmsexception {
        return session.createtextmessage(msg);
      }
    });
  }
  //可指定目的地
  public void sendmessage(destination destination,final string msg){
    jmstemplate.send(destination, new messagecreator() {
      public message createmessage(session session) throws jmsexception {
        return session.createtextmessage(msg);
      }
    });
  }
}

消息消費者服務

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@service
public class consumerservice {
  @autowired
  private jmstemplate jmstemplate;
  //從指定的destination接收消息
  public textmessage recive(destination destination){
    textmessage message = (textmessage) jmstemplate.receive(destination);
    try {
      system.out.println("從隊列" + destination.tostring() + "收到了消息" + message.gettext());
    } catch (jmsexception e) {
      e.printstacktrace();
    }
    return message;
  }
  //從默認的destination接收消息
  public void recivedefault(){
 
    destination destination = jmstemplate.getdefaultdestination();
    jmstemplate.setreceivetimeout(5000);
    while(true){
      textmessage message = (textmessage) jmstemplate.receive(destination);
      try {
        //這里還是同一個消費者
        system.out.println("消費者 從目的地 " + destination.tostring() + " 收到了消息" + message.gettext());
      } catch (jmsexception e) {
        e.printstacktrace();
      }
    }
  }
}

生產者

直接在 main 方法中獲取 applicationcontext 運行,便于測試。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@component
public class msgproducer {
  @autowired
  private producerservice producerservice;
  public void send(){
    system.out.println("生產者開始發送消息:");
    for(int i = 1; i < 11; i++){
      string msg = "生產者發出的消息";
      producerservice.sendmessagedefault(msg + "-----" + i);
    }
  }
  public static void main(string[] args) {
    applicationcontext context = new classpathxmlapplicationcontext("classpath:/applicationcontext.xml");
    msgproducer msgproducer = context.getbean(msgproducer.class);
    msgproducer.send();
  }
}

消費者

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@component
public class msgconsumer {
  @autowired
  private consumerservice consumerservice;
  public void recive(){
    system.out.println("消費者 1 開始接收消息:");
    consumerservice.recivedefault();
  }
  public static void main(string[] args) {
    applicationcontext context = new classpathxmlapplicationcontext("classpath:/applicationcontext.xml");
    msgconsumer msgconsumer = context.getbean(msgconsumer.class);
    msgconsumer.recive();
  }
}

接下來就可以啟動項目。同樣是使用兩種方式測試。

第一種方式————點對點(queue)

同步的方式

先啟動生產者發送10條消息, 再啟動消費者,可以看到控制臺顯示成功收到10條消息。

ActiveMQ結合Spring收發消息的示例代碼

ActiveMQ結合Spring收發消息的示例代碼

異步監聽的方式

通過監聽器即可實現異步接收消息的效果,而不是像上面使用 while() 輪詢同步的方式。

項目中一般都是使用異步監聽的方式,在 a 服務中發送了一條消息,b 服務可以利用消息監聽器監聽,當收到消息后,進行相應的操作。

消息監聽器(3種)

通過繼承 jms 中的 messagelistener 接口,實現 onmessage() 方法,就可以自定義監聽器。這是最基本的監聽器。(可根據業務實現自定義的功能)

另外spring也給我們提供了其他類型的消息監聽器,比如 sessionawaremessagelistener,它的作用不僅可以接收消息,還可以發送一條消息通知對方表示自己收到了消息。(還有一種是 messagelisteneradapter)

一個簡單的自定義監聽器如下:收到消息后打印消息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class queuemessagelistener implements messagelistener {
  public void onmessage(message message) {
    //如果有消息
    textmessage tmessage = (textmessage) message;
    try {
      if(tmessage != null){
        system.out.println("監聽器監聽消息:"+tmessage.gettext());
      }
    } catch (jmsexception e) {
      e.printstacktrace();
    }
  }
}

在 activemq.xml 中引入消息監聽器:

?
1
2
3
4
5
6
7
8
9
10
11
<!-- 配置消息隊列監聽者(queue) -->
  <bean id="queuemessagelistener" class="com.service.queuemessagelistener" />
 
 <!-- 顯示注入消息監聽容器,配置連接工廠,監聽的目標是queuedestination 或 topicdestination,監聽器是上面自定義的監聽器 -->
  <bean id="queuelistenercontainer"
     class="org.springframework.jms.listener.defaultmessagelistenercontainer">
    <property name="connectionfactory" ref="connectionfactory" />
    <property name="destination" ref="queuedestination" />
    <!--<property name="destination" ref="topicdestination" />-->
    <property name="messagelistener" ref="queuemessagelistener" />
  </bean>

可以看到,當使用消息監聽器之后,每發送一條消息立馬就會被監聽到:

ActiveMQ結合Spring收發消息的示例代碼

第二種方式————發布/訂閱(topic)

同步的方式

類似點對點中同步的方式,只是每個消費者都能收到生產者發出的全部消息,不再贅述。

異步監聽的方式

啟動兩個監聽器(兩個消費者),對消息進行異步監聽。看是否各自能收到生產者發送的消息。

?
1
2
3
<!-- 配置兩個監聽器 -->
<bean id="messagelistener" class="com.service.topicmessagelistener" />
<bean id="messagelistener2" class="com.service.topicmessagelistener2" />

ActiveMQ結合Spring收發消息的示例代碼

可以看到,每個監聽器各自都收到了生產者發送的10條消息。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.importnew.com/30159.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 万域之王动漫在线观看全集免费播放 | 99精品热线在线观看免费视频 | 国产乱子伦真实china | 婷婷麻豆 | 91精品国产在线 | 欧美久草在线 | 国产在线观看网站 | 欧美日韩亚洲综合在线一区二区 | 射逼网站 | 国产美女屁股直流白浆视频无遮挡 | 99资源在线观看 | 操双性人 | 天天色影视综合网 | 美女黄板视频 | 青草青青在线 | 欧美日韩中文字幕一区二区高清 | 精品AV亚洲乱码一区二区 | 美女口述又粗又大感觉 | 国产女主播在线播放一区二区 | 福利视频一区二区三区 | 2020韩国r级理论片在线观看 | 日本五十路六十30人8时间 | 91久久碰国产 | 亚洲羞羞裸色私人影院 | 桥本有菜在线四虎福利网 | 欧美一区二区三区不卡视频 | 亚洲国产区中文在线观看 | 韩国情事伦理片观看地址 | 精品综合一区二区三区 | 国产精品国产三级国产专区不 | 精品国产欧美精品v | 免费一级毛片在线播放 | 成人高辣h视频一区二区在线观看 | 四虎影视免费观看免费观看 | ai换脸明星造梦工厂忘忧草 | 免费永久观看美女视频网站网址 | 国产无限制自拍 | 91视在线国内在线播放酒店 | 天堂在线中文无弹窗全文阅读 | 亚洲第一区在线观看 | 欧美一级欧美三级在线 |