activemq 結(jié)合 spring 收發(fā)消息
直接使用 activemq 的方式需要重復(fù)寫很多代碼,且不利于管理,spring 提供了一種更加簡便的方式————spring jms ,通過它可以更加方便地使用 activemq。
maven 依賴
結(jié)合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> <!-- 定義消息隊(duì)列(queue)--> <!-- <bean id= "queuedestination" class = "org.apache.activemq.command.activemqqueue" > <!– 設(shè)置消息隊(duì)列的名字 –> <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工具類,利用它發(fā)送、接收消息。 --> <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,默認(rèn)是 false --> <property name= "pubsubdomain" value= "true" /> </bean> <!-- 配置消息隊(duì)列監(jiān)聽者(queue or topic) --> <bean id= "messagelistener" class = "com.service.topicmessagelistener" /> <!-- 顯示注入消息監(jiān)聽容器,配置連接工廠,監(jiān)聽的目標(biāo)是queuedestination,監(jiān)聽器是上面定義的監(jiān)聽器 --> <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 用于創(chuàng)建到 jms 服務(wù)器鏈接的,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" > <!-- 設(shè)置消息隊(duì)列的名字 --> <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工具類,利用它發(fā)送、接收消息--> <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,默認(rèn)是 false --> </bean> |
最后,在 applicationcontext.xml 中引入配置好的 activemq.xml
1
|
< import resource= "activemq.xml" /> |
以上就是配置文件相關(guān)的,下面是具體的業(yè)務(wù)代碼。
消息生產(chǎn)者服務(wù)
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; //使用默認(rèn)目的地 public void sendmessagedefault( final string msg){ destination destination = jmstemplate.getdefaultdestination(); system.out.println( "向隊(duì)列: " + destination + " 成功發(fā)送一條消息" ); 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); } }); } } |
消息消費(fèi)者服務(wù)
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( "從隊(duì)列" + destination.tostring() + "收到了消息" + message.gettext()); } catch (jmsexception e) { e.printstacktrace(); } return message; } //從默認(rèn)的destination接收消息 public void recivedefault(){ destination destination = jmstemplate.getdefaultdestination(); jmstemplate.setreceivetimeout( 5000 ); while ( true ){ textmessage message = (textmessage) jmstemplate.receive(destination); try { //這里還是同一個消費(fèi)者 system.out.println( "消費(fèi)者 從目的地 " + destination.tostring() + " 收到了消息" + message.gettext()); } catch (jmsexception e) { e.printstacktrace(); } } } } |
生產(chǎn)者
直接在 main 方法中獲取 applicationcontext 運(yùn)行,便于測試。
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( "生產(chǎn)者開始發(fā)送消息:" ); for ( int i = 1 ; i < 11 ; i++){ string msg = "生產(chǎn)者發(fā)出的消息" ; 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(); } } |
消費(fèi)者
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( "消費(fèi)者 1 開始接收消息:" ); consumerservice.recivedefault(); } public static void main(string[] args) { applicationcontext context = new classpathxmlapplicationcontext( "classpath:/applicationcontext.xml" ); msgconsumer msgconsumer = context.getbean(msgconsumer. class ); msgconsumer.recive(); } } |
接下來就可以啟動項(xiàng)目。同樣是使用兩種方式測試。
第一種方式————點(diǎn)對點(diǎn)(queue)
同步的方式
先啟動生產(chǎn)者發(fā)送10條消息, 再啟動消費(fèi)者,可以看到控制臺顯示成功收到10條消息。
異步監(jiān)聽的方式
通過監(jiān)聽器即可實(shí)現(xiàn)異步接收消息的效果,而不是像上面使用 while() 輪詢同步的方式。
項(xiàng)目中一般都是使用異步監(jiān)聽的方式,在 a 服務(wù)中發(fā)送了一條消息,b 服務(wù)可以利用消息監(jiān)聽器監(jiān)聽,當(dāng)收到消息后,進(jìn)行相應(yīng)的操作。
消息監(jiān)聽器(3種)
通過繼承 jms 中的 messagelistener 接口,實(shí)現(xiàn) onmessage() 方法,就可以自定義監(jiān)聽器。這是最基本的監(jiān)聽器。(可根據(jù)業(yè)務(wù)實(shí)現(xiàn)自定義的功能)
另外spring也給我們提供了其他類型的消息監(jiān)聽器,比如 sessionawaremessagelistener,它的作用不僅可以接收消息,還可以發(fā)送一條消息通知對方表示自己收到了消息。(還有一種是 messagelisteneradapter)
一個簡單的自定義監(jiān)聽器如下:收到消息后打印消息
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( "監(jiān)聽器監(jiān)聽消息:" +tmessage.gettext()); } } catch (jmsexception e) { e.printstacktrace(); } } } |
在 activemq.xml 中引入消息監(jiān)聽器:
1
2
3
4
5
6
7
8
9
10
11
|
<!-- 配置消息隊(duì)列監(jiān)聽者(queue) --> <bean id= "queuemessagelistener" class = "com.service.queuemessagelistener" /> <!-- 顯示注入消息監(jiān)聽容器,配置連接工廠,監(jiān)聽的目標(biāo)是queuedestination 或 topicdestination,監(jiān)聽器是上面自定義的監(jiān)聽器 --> <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> |
可以看到,當(dāng)使用消息監(jiān)聽器之后,每發(fā)送一條消息立馬就會被監(jiān)聽到:
第二種方式————發(fā)布/訂閱(topic)
同步的方式
類似點(diǎn)對點(diǎn)中同步的方式,只是每個消費(fèi)者都能收到生產(chǎn)者發(fā)出的全部消息,不再贅述。
異步監(jiān)聽的方式
啟動兩個監(jiān)聽器(兩個消費(fèi)者),對消息進(jìn)行異步監(jiān)聽。看是否各自能收到生產(chǎn)者發(fā)送的消息。
1
2
3
|
<!-- 配置兩個監(jiān)聽器 --> <bean id= "messagelistener" class = "com.service.topicmessagelistener" /> <bean id= "messagelistener2" class = "com.service.topicmessagelistener2" /> |
可以看到,每個監(jiān)聽器各自都收到了生產(chǎn)者發(fā)送的10條消息。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.importnew.com/30159.html