1、概述
首先和大家一起回顧一下Java 消息服務(wù),在我之前的博客《Java消息隊(duì)列-JMS概述》中,我為大家分析了:
1.消息服務(wù):一個(gè)中間件,用于解決兩個(gè)活多個(gè)程序之間的耦合,底層由Java 實(shí)現(xiàn)。
2.優(yōu)勢(shì):異步、可靠
3.消息模型:點(diǎn)對(duì)點(diǎn),發(fā)布/訂閱
4.JMS中的對(duì)象
然后在另一篇博客《Java消息隊(duì)列-ActiveMq實(shí)戰(zhàn)》中,和大家一起從0到1的開啟了一個(gè)ActiveMq 的項(xiàng)目,在項(xiàng)目開發(fā)的過程中,我們對(duì)ActiveMq有了一定的了解:
1.多種語言和協(xié)議編寫客戶端。語言: Java, C, C++, C#, Ruby, Perl, Python, PHP。應(yīng)用協(xié)議: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
2.完全支持JMS1.1和J2EE 1.4規(guī)范 (持久化,XA消息,事務(wù))
3.對(duì)Spring的支持,ActiveMQ可以很容易內(nèi)嵌到使用Spring的系統(tǒng)里面去,而且也支持Spring2.0的特性
4.通過了常見J2EE服務(wù)器(如 Geronimo,JBoss 4, GlassFish,WebLogic)的測試,其中通過JCA 1.5 resource adaptors的配置,可以讓ActiveMQ可以自動(dòng)的部署到任何兼容J2EE 1.4 商業(yè)服務(wù)器上
5.支持多種傳送協(xié)議:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
6.支持通過JDBC和journal提供高速的消息持久化
7.從設(shè)計(jì)上保證了高性能的集群,客戶端-服務(wù)器,點(diǎn)對(duì)點(diǎn)
8.支持Ajax
9.支持與Axis的整合
10.可以很容易得調(diào)用內(nèi)嵌JMS provider,進(jìn)行測試
在接下來的這篇博客中,我會(huì)和大家一起來整合Spring 和ActiveMq,這篇博文,我們基于Spring+JMS+ActiveMQ+Tomcat,實(shí)現(xiàn)了Point-To-Point的異步隊(duì)列消息和PUB/SUB(發(fā)布/訂閱)模型,簡單實(shí)例,不包含任何業(yè)務(wù)。
2、目錄結(jié)構(gòu)
2.1 項(xiàng)目目錄
IDE選擇了IDEA(建議大家使用),為了避免下載jar 的各種麻煩,底層使用maven搭建了一個(gè)項(xiàng)目,整合了Spring 和ActiveMq
2.2 pom.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
< project xmlns = " http://maven.apache.org/POM/4.0.0 " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation = " http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd " > < modelVersion >4.0.0</ modelVersion > < groupId >Crawl-Page</ groupId > < artifactId >Crawl-Page</ artifactId > < packaging >war</ packaging > < version >1.0-SNAPSHOT</ version > < name >Crawl-Page Maven Webapp</ name > < url > http://maven.apache.org </ url > <!-- 版本管理 --> < properties > < springframework >4.1.8.RELEASE</ springframework > </ properties > < dependencies > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.10</ version > < scope >test</ scope > </ dependency > <!-- JSP相關(guān) --> < dependency > < groupId >jstl</ groupId > < artifactId >jstl</ artifactId > < version >1.2</ version > </ dependency > < dependency > < groupId >javax.servlet</ groupId > < artifactId >servlet-api</ artifactId > < scope >provided</ scope > < version >2.5</ version > </ dependency > <!-- spring --> < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-core</ artifactId > < version >${springframework}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >${springframework}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-tx</ artifactId > < version >${springframework}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-webmvc</ artifactId > < version >${springframework}</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-jms</ artifactId > < version >${springframework}</ version > </ dependency > <!-- xbean 如<amq:connectionFactory /> --> < dependency > < groupId >org.apache.xbean</ groupId > < artifactId >xbean-spring</ artifactId > < version >3.16</ version > </ dependency > <!-- activemq --> < 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.12.1</ version > </ dependency > <!-- 自用jar包,可以忽略--> < dependency > < groupId >commons-httpclient</ groupId > < artifactId >commons-httpclient</ artifactId > < version >3.1</ version > </ dependency > </ dependencies > < build > < finalName >Crawl-Page</ finalName > < plugins > < plugin > < groupId >org.apache.tomcat.maven</ groupId > < artifactId >tomcat7-maven-plugin</ artifactId > < configuration > < port >8080</ port > < path >/</ path > </ configuration > </ plugin > </ plugins > </ build > </ project > |
因?yàn)檫@里pom.xml 文件有點(diǎn)長,就不展開了。
我們可以看到其實(shí)依賴也就幾個(gè),1、Spring 核心依賴 2、ActiveMq core和pool(這里如果同學(xué)們選擇導(dǎo)入jar,可以直接導(dǎo)入我們上一篇博客中說道的那個(gè)activemq-all 這個(gè)jar包)3、java servlet 相關(guān)依賴
這里面我們選擇的ActiveMq pool 的依賴版本會(huì)和之后的dtd 有關(guān)系,需要版本對(duì)應(yīng),所以同學(xué)們等下配置activemq 文件的時(shí)候,需要注意dtd 版本選擇
2.3 web.xml
web.xml 也大同小異,指定Spring 配置文件,springMvc 命名,編碼格式
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
52
53
54
|
<? xml version = "1.0" encoding = "UTF-8" ?> < web-app xmlns = " http://java.sun.com/xml/ns/javaee " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd " version = "3.0" > < display-name >Archetype Created Web Application</ display-name > <!-- 加載spring的配置文件,例如hibernate、jms等集成 --> < context-param > < param-name >contextConfigLocation</ param-name > < param-value > classpath:applicationContext*.xml; </ param-value > </ context-param > < listener > < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener > < servlet > < servlet-name >springMVC</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < init-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:spring-mvc.xml</ param-value > </ init-param > < load-on-startup >1</ load-on-startup > </ servlet > < servlet-mapping > < servlet-name >springMVC</ servlet-name > < url-pattern >/</ url-pattern > </ servlet-mapping > <!-- 處理編碼格式 --> < filter > < filter-name >characterEncodingFilter</ filter-name > < filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class > < init-param > < param-name >encoding</ param-name > < param-value >UTF-8</ param-value > </ init-param > < init-param > < param-name >forceEncoding</ param-name > < param-value >true</ param-value > </ init-param > </ filter > < filter-mapping > < filter-name >characterEncodingFilter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > </ web-app > |
2.4 SpringMvc 和applicationContext.xml
這里面的SpringMVC沒什么特別,有需要的同學(xué)可以參考一下:
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
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ --> < beans xmlns = " http://www.springframework.org/schema/beans " xmlns:aop = " http://www.springframework.org/schema/aop " xmlns:context = " http://www.springframework.org/schema/context " xmlns:mvc = " http://www.springframework.org/schema/mvc " xmlns:tx = " http://www.springframework.org/schema/tx " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> <!-- 啟用MVC注解 --> < mvc:annotation-driven /> <!-- 指定Sping組件掃描的基本包路徑 --> < context:component-scan base-package = "com.Jayce" > <!-- 這里只掃描Controller,不可重復(fù)加載Service --> < context:include-filter type = "annotation" expression = "org.springframework.stereotype.Controller" /> </ context:component-scan > <!-- JSP視圖解析器--> < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name = "prefix" value = "/WEB-INF/views/" /> < property name = "suffix" value = ".jsp" /> <!-- 定義其解析視圖的order順序?yàn)? --> < property name = "order" value = "1" /> </ bean > </ beans > |
applicationContext.xml 主要使用來裝載Bean,我們項(xiàng)目中并沒有什么特別的Java Bean,因此只用來指出包掃描路徑:
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
|
<? 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 " xmlns:jms = " http://www.springframework.org/schema/jms " xmlns:context = " http://www.springframework.org/schema/context " xmlns:mvc = " http://www.springframework.org/schema/mvc " xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.1.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.14.3.xsd "> < bean class = "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <!-- 配置掃描路徑 --> < context:component-scan base-package = "com.Jayce" > <!-- 只掃描Service,也可以添加Repostory,但是要把Controller排除在外,Controller由spring-mvc.xml去加載 --> < context:exclude-filter type = "annotation" expression = "org.springframework.stereotype.Controller" /> </ context:component-scan > </ beans > |
2.5 applicationContext-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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
<? 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 " xmlns:jms = " http://www.springframework.org/schema/jms " xmlns:context = " http://www.springframework.org/schema/context " xmlns:mvc = " http://www.springframework.org/schema/mvc " xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.1.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.12.1.xsd " > < context:component-scan base-package = "com.Jayce" /> < mvc:annotation-driven /> < amq:connectionFactory id = "amqConnectionFactory" brokerURL = " tcp://192.168.148.128: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 = "demoQueueDestination" class = "org.apache.activemq.command.ActiveMQQueue" > <!-- 設(shè)置消息隊(duì)列的名字 --> < constructor-arg > < value >Jaycekon</ value > </ constructor-arg > </ bean > <!-- 配置JMS模板(Queue),Spring提供的JMS工具類,它發(fā)送、接收消息。 --> < bean id = "jmsTemplate" class = "org.springframework.jms.core.JmsTemplate" > < property name = "connectionFactory" ref = "connectionFactory" /> < property name = "defaultDestination" ref = "demoQueueDestination" /> < property name = "receiveTimeout" value = "10000" /> <!-- true是topic,false是queue,默認(rèn)是false,此處顯示寫出false --> < property name = "pubSubDomain" value = "false" /> </ bean > <!-- 配置消息隊(duì)列監(jiān)聽者(Queue) --> < bean id = "queueMessageListener" class = "com.Jayce.Filter.QueueMessageListener" /> <!-- 顯示注入消息監(jiān)聽容器(Queue),配置連接工廠,監(jiān)聽的目標(biāo)是demoQueueDestination,監(jiān)聽器是上面定義的監(jiān)聽器 --> < bean id = "queueListenerContainer" class = "org.springframework.jms.listener.DefaultMessageListenerContainer" > < property name = "connectionFactory" ref = "connectionFactory" /> < property name = "destination" ref = "demoQueueDestination" /> < property name = "messageListener" ref = "queueMessageListener" /> </ bean > </ beans > |
這里和大家講解一下這個(gè)配置文件,如果大家能夠從上述配置文件中看懂,可以跳過。同學(xué)們也可以在ActiveMQ官網(wǎng)中的查看。
1、ActiveMq 中的DTD,我們?cè)诼暶飨嚓P(guān)配置之前,我們需要先導(dǎo)入ActiveMq 中的DTD,不然Spring 并不理解我們的標(biāo)簽是什么意思。
我們?cè)趐om.xml 文件中有配置了activemq 的版本依賴我們這里的版本,需要和依賴的版本一樣,不然是找不到相關(guān)的dtd
2、amq:connectionFactory:很直白的一個(gè)配置項(xiàng),用于配置我們鏈接工廠的地址和用戶名密碼,這里需要注意的是選擇tcp連接而不是http連接
3、jmsTemplate:比較重要的一個(gè)配置,這里指定了連接工廠,默認(rèn)消息發(fā)送目的地,還有連接時(shí)長,發(fā)布消息的方式
3、項(xiàng)目結(jié)構(gòu)
3.1 ProducerService
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
|
package com.Jayce.Service; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; /** * Created by Administrator on 2017/1/5. */ @Service public class ProducerService { @Resource (name= "jmsTemplate" ) private JmsTemplate jmsTemplate; public void sendMessage(Destination destination, final String msg){ System.out.println(Thread.currentThread().getName()+ " 向隊(duì)列" +destination.toString()+ "發(fā)送消息---------------------->" +msg); jmsTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(msg); } }); } public void sendMessage( final String msg){ String destination = jmsTemplate.getDefaultDestinationName(); System.out.println(Thread.currentThread().getName()+ " 向隊(duì)列" +destination+ "發(fā)送消息---------------------->" +msg); jmsTemplate.send( new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(msg); } }); } } |
將消息生產(chǎn)者做成一個(gè)服務(wù),當(dāng)我們需要發(fā)送消息的時(shí)候,只需要調(diào)用ProducerService實(shí)例中的sendMessage 方法就可以向默認(rèn)目的發(fā)送一個(gè)消息。
這里提供了兩個(gè)發(fā)送方式,一個(gè)是發(fā)送到默認(rèn)的目的地,一個(gè)是根據(jù)目的地發(fā)送消息。
3.2 ConsumerService
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
|
package com.Jayce.Service; import org.springframework.jms.core.JmsTemplate; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.TextMessage; /** * Created by Administrator on 2017/1/5. */ @Service public class ConsumerService { @Resource (name= "jmsTemplate" ) private JmsTemplate jmsTemplate; public TextMessage receive(Destination destination){ TextMessage textMessage = (TextMessage) jmsTemplate.receive(destination); try { System.out.println( "從隊(duì)列" + destination.toString() + "收到了消息:\t" + textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } return textMessage; } } |
因?yàn)槲覀冺?xiàng)目中并沒有什么業(yè)務(wù),所以的話對(duì)消息的處理也就是打印輸出。我們只需要調(diào)用jmsTemplate中的 receive 方法,就可以從里面獲取到一條消息。
再和我們上一篇博客對(duì)比一下,上一篇博客中,我們接受到信息之后需要手動(dòng)確認(rèn)事務(wù),這樣ActiveMQ中才會(huì)確定這條消息已經(jīng)被正確讀取了。而整合了Spring之后,事務(wù)將由Spring 來管理。
3.3 MessageController
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
|
package com.Jayce.Controller; import com.Jayce.Service.ConsumerService; import com.Jayce.Service.ProducerService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import javax.jms.Destination; import javax.jms.TextMessage; /** * Created by Administrator on 2017/1/5. */ @Controller public class MessageController { private Logger logger = LoggerFactory.getLogger(MessageController. class ); @Resource (name = "demoQueueDestination" ) private Destination destination; //隊(duì)列消息生產(chǎn)者 @Resource (name = "producerService" ) private ProducerService producer; //隊(duì)列消息消費(fèi)者 @Resource (name = "consumerService" ) private ConsumerService consumer; @RequestMapping (value = "/SendMessage" , method = RequestMethod.POST) @ResponseBody public void send(String msg) { logger.info(Thread.currentThread().getName()+ "------------send to jms Start" ); producer.sendMessage(msg); logger.info(Thread.currentThread().getName()+ "------------send to jms End" ); } @RequestMapping (value= "/ReceiveMessage" ,method = RequestMethod.GET) @ResponseBody public Object receive(){ logger.info(Thread.currentThread().getName()+ "------------receive from jms Start" ); TextMessage tm = consumer.receive(destination); logger.info(Thread.currentThread().getName()+ "------------receive from jms End" ); return tm; } } |
控制層里面需要注入我們的生產(chǎn)者和消費(fèi)者(實(shí)際開發(fā)中,生產(chǎn)者和消費(fèi)者肯定不會(huì)在同一個(gè)項(xiàng)目中的,不然就消息服務(wù)這個(gè)東西就沒有意義了)。
現(xiàn)在服務(wù)層和控制層都好了,接下來我們就進(jìn)行一個(gè)簡單的測試
4、項(xiàng)目測試
4.1 啟動(dòng)ActiveMq
先確定你的ActiveMQ服務(wù)已經(jīng)開啟。
4.2 啟動(dòng)項(xiàng)目
項(xiàng)目使用了Tomcat 插件,避免了本地再下載Tomcat的麻煩,有需要的同學(xué)可以使用一下。
1
|
2
3
4
5
6
7
8
9
10
|
< plugins > < plugin > < groupId >org.apache.tomcat.maven</ groupId > < artifactId >tomcat7-maven-plugin</ artifactId > < configuration > < port >8080</ port > < path >/</ path > </ configuration > </ plugin > </ plugins > |
4.3 發(fā)送消息
這里用了Chrome 的一個(gè)插件PostMan 有興趣的同學(xué)可以了解一下,在Chrome 拓展程序中可以找到,避免了后端的同學(xué)去弄頁面!
我們發(fā)送了一個(gè)post 請(qǐng)求之后,看一下服務(wù)器的效果:
我們可以看到,已經(jīng)向隊(duì)列發(fā)送了一條消息。我們看一下ActiveMq現(xiàn)在的狀態(tài):
我們可以看到,一條消息已經(jīng)成功發(fā)送到了ActiveMq中。
4.4 接收消息
使用get請(qǐng)求訪問服務(wù)器后臺(tái):
服務(wù)的輸出:
ActiveMq服務(wù)器狀態(tài):
我們可以看到,消費(fèi)者已經(jīng)消費(fèi)了一條信息,并且沒有斷開與ActiveMq之間的鏈接。
4.5 監(jiān)聽器
在實(shí)際項(xiàng)目中,我們很少會(huì)自己手動(dòng)去獲取消息,如果需要手動(dòng)去獲取消息,那就沒有必要使用到ActiveMq了,可以用一個(gè)Redis 就足夠了。
不能手動(dòng)去獲取消息,那么我們就可以選擇使用一個(gè)監(jiān)聽器來監(jiān)聽是否有消息到達(dá),這樣子可以很快的完成對(duì)消息的處理。
4.5.1 applicationContext-ActiveMQ.xml 配置
在上面的配置文件中,我們已經(jīng)默認(rèn)的添加了這段監(jiān)聽器的配置文件,如果同學(xué)們不想使用這個(gè)監(jiān)聽器,可以直接注釋掉。
1
|
2
3
4
5
6
7
8
9
10
|
<!-- 配置消息隊(duì)列監(jiān)聽者(Queue) --> < bean id = "queueMessageListener" class = "com.Jayce.Filter.QueueMessageListener" /> <!-- 顯示注入消息監(jiān)聽容器(Queue),配置連接工廠,監(jiān)聽的目標(biāo)是demoQueueDestination,監(jiān)聽器是上面定義的監(jiān)聽器 --> < bean id = "queueListenerContainer" class = "org.springframework.jms.listener.DefaultMessageListenerContainer" > < property name = "connectionFactory" ref = "connectionFactory" /> < property name = "destination" ref = "demoQueueDestination" /> < property name = "messageListener" ref = "queueMessageListener" /> </ bean > |
4.5.2 MessageListener
我們需要?jiǎng)?chuàng)建一個(gè)類實(shí)現(xiàn)MessageListener 接口:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.Jayce.Filter; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; /** * Created by Administrator on 2017/1/5. */ public class QueueMessageListener implements MessageListener { public void onMessage(Message message) { TextMessage tm = (TextMessage) message; try { System.out.println( "QueueMessageListener監(jiān)聽到了文本消息:\t" + tm.getText()); //do something ... } catch (JMSException e) { e.printStackTrace(); } } } |
實(shí)現(xiàn)接口的onMessage 方法,我們將需要的業(yè)務(wù)操作在里面解決,這樣子,就完成了我們生產(chǎn)者-中間件-消費(fèi)者,這樣一個(gè)解耦的操作了。
4.5.3 測試
和上面一樣,使用postMan 發(fā)送post請(qǐng)求,我們可以看到控制臺(tái)里面,消息馬上就能打印出來:
再看看ActiveMQ服務(wù)器的狀態(tài):
我們可以看到,使用監(jiān)聽器的效果,和手動(dòng)接收消息的效果是一樣的。
這樣子一整個(gè)項(xiàng)目下來,我們已經(jīng)成功的整合了Spring和ActiveMQ。
4.6 壓力測試
這里其實(shí)也算不上什么壓力測試,在配置pom.xml文件的時(shí)候,大家有看到一個(gè) commons-httpclient 的依賴,接下來我們使用httpClient 不停的想服務(wù)器發(fā)送消息,看一下服務(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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package com.Jaycekon.test; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.junit.Test; import java.io.IOException; import java.util.concurrent.atomic.AtomicInteger; /** * Created by Administrator on 2017/1/5. */ public class Client { @Test public void test() { HttpClient httpClient = new HttpClient(); new Thread( new Sender(httpClient)).start(); } } class Sender implements Runnable { public static AtomicInteger count = new AtomicInteger( 0 ); HttpClient httpClient; public Sender(HttpClient client) { httpClient = client; } public void run() { try { System.out.println(Thread.currentThread().getName()+ "---Send message-" +count.getAndIncrement()); PostMethod post = new PostMethod( " http://127.0.0.1:8080/SendMessage " ); post.addParameter( "msg" , "Hello world!" ); httpClient.executeMethod(post); System.out.println(Thread.currentThread().getName()+ "---Send message Success-" +count.getAndIncrement()); } catch (IOException e) { e.printStackTrace(); } } } |
這里面用了HttpClient 來向服務(wù)器發(fā)送Post 請(qǐng)求,然后計(jì)數(shù)輸出,有興趣的同學(xué)可以自己測試一下,可以多開幾個(gè)線程,這里只開了一個(gè)線程。
5、項(xiàng)目源碼:Crawl-Page.rar
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/jaycekon/p/ActiveMq.html