前言:本文是基於Spring和ActiveMQ的一個示例文章,包括了Point-To-Point的異步隊列消息和PUB/SUB(發佈/訂閱)模型,只是作了比較簡單的實現,無任何業務方面的東西,做爲一個入門教程。javascript
適合對象:但願學習ActiveMQ的朋友,以及利用Spring將ActiveMQ集成到系統中css
目錄結構:html
Maven依賴:前端
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
|
<!-- Jar版本管理 -->
<
properties
>
<
springframework
>4.0.2.RELEASE</
springframework
>
<
log4j
>1.2.17</
log4j
>
<
activemq
>5.9.0</
activemq
>
</
properties
>
<
dependencies
>
<!-- Spring web mvc -->
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-webmvc</
artifactId
>
<
version
>${springframework}</
version
>
</
dependency
>
<!-- 提供JMS,Freemarker,Quartz集成服務 -->
<
dependency
>
<
groupId
>org.springframework</
groupId
>
<
artifactId
>spring-context-support</
artifactId
>
<
version
>${springframework}</
version
>
</
dependency
>
<!-- 集成JMS -->
<
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
>
<!-- log4j -->
<
dependency
>
<
groupId
>log4j</
groupId
>
<
artifactId
>log4j</
artifactId
>
<
version
>${log4j}</
version
>
</
dependency
>
<!-- Active MQ -->
<
dependency
>
<
groupId
>org.apache.activemq</
groupId
>
<
artifactId
>activemq-all</
artifactId
>
<
version
>${activemq}</
version
>
</
dependency
>
<!-- 單元測試 -->
<
dependency
>
<
groupId
>junit</
groupId
>
<
artifactId
>junit</
artifactId
>
<
version
>3.8.1</
version
>
<
scope
>test</
scope
>
</
dependency
>
</
dependencies
>
|
jar包截圖java
web.xmljquery
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
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!-- 經過http://java.sun.com/xml/ns/javaee/獲取最新的schemaLocation -->
<
web-app
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:web
=
"http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
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
>SpringActivemqServer</
display-name
>
<!-- WebAppRootKey -->
<
context-param
>
<
param-name
>webAppRootKey</
param-name
>
<
param-value
>example.SpringActivemqServer</
param-value
>
</
context-param
>
<!-- Log4J Start -->
<
context-param
>
<
param-name
>log4jConfigLocation</
param-name
>
<
param-value
>classpath:log4j.properties</
param-value
>
</
context-param
>
<
context-param
>
<
param-name
>log4jRefreshInterval</
param-name
>
<
param-value
>6000</
param-value
>
</
context-param
>
<!-- Spring Log4J config -->
<
listener
>
<
listener-class
>org.springframework.web.util.Log4jConfigListener</
listener-class
>
</
listener
>
<!-- Log4J End -->
<!-- Spring 編碼過濾器 start -->
<
filter
>
<
filter-name
>characterEncoding</
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
>characterEncoding</
filter-name
>
<
url-pattern
>/*</
url-pattern
>
</
filter-mapping
>
<!-- Spring 編碼過濾器 End -->
<!-- Spring Application Context Listener Start -->
<
context-param
>
<
param-name
>contextConfigLocation</
param-name
>
<
param-value
>classpath*:applicationContext.xml,classpath*:ActiveMQ.xml</
param-value
>
</
context-param
>
<
listener
>
<
listener-class
>org.springframework.web.context.ContextLoaderListener</
listener-class
>
</
listener
>
<!-- Spring Application Context Listener End -->
<!-- Spring MVC Config Start -->
<
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
>
<!-- Filter all resources -->
<
url-pattern
>/</
url-pattern
>
</
servlet-mapping
>
<!-- Spring MVC Config End -->
</
web-app
>
|
applicationContext.xmlgit
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
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ -->
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:context
=
"http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.9.0.xsd">
<
bean
class
=
"org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"
/>
<!-- 配置掃描路徑 -->
<
context:component-scan
base-package
=
"org.xdemo.example"
>
<!-- 只掃描Service,也能夠添加Repostory,可是要把Controller排除在外,Controller由spring-mvc.xml去加載 -->
<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> -->
<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> -->
<!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Component" /> -->
<
context:exclude-filter
type
=
"annotation"
expression
=
"org.springframework.stereotype.Controller"
/>
</
context:component-scan
>
</
beans
>
|
spring-mvc.xmlweb
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
|
<?
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
/>
<!-- 靜態資源文件,不會被Spring MVC攔截 -->
<
mvc:resources
location
=
"/resources/"
mapping
=
"/resources/**"
/>
<!-- 指定Sping組件掃描的基本包路徑 -->
<
context:component-scan
base-package
=
"org.xdemo.example"
>
<!-- 這裏只掃描Controller,不可重複加載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"
/>
<
property
name
=
"order"
value
=
"1"
/>
</
bean
>
</
beans
>
|
ActiveMQ.xmlajax
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
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!-- 查找最新的schemaLocation 訪問 http://www.springframework.org/schema/ -->
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:context
=
"http://www.springframework.org/schema/context"
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"
xsi:schemaLocation="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/jms
http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd">
<
amq:connectionFactory
id
=
"amqConnectionFactory"
brokerURL
=
"tcp://localhost:61616"
userName
=
"admin"
password
=
"admin"
/>
<
bean
id
=
"connectionFactory"
class
=
"org.springframework.jms.connection.CachingConnectionFactory"
>
<
constructor-arg
ref
=
"amqConnectionFactory"
/>
<
property
name
=
"sessionCacheSize"
value
=
"100"
/>
</
bean
>
<!-- ====Producer side start====-->
<!-- 定義JmsTemplate的Queue類型 -->
<
bean
id
=
"jmsQueueTemplate"
class
=
"org.springframework.jms.core.JmsTemplate"
>
<
constructor-arg
ref
=
"connectionFactory"
/>
<!-- 非pub/sub模型(發佈/訂閱),即隊列模式 -->
<
property
name
=
"pubSubDomain"
value
=
"false"
/>
</
bean
>
<!-- 定義JmsTemplate的Topic類型 -->
<
bean
id
=
"jmsTopicTemplate"
class
=
"org.springframework.jms.core.JmsTemplate"
>
<
constructor-arg
ref
=
"connectionFactory"
/>
<!-- pub/sub模型(發佈/訂閱) -->
<
property
name
=
"pubSubDomain"
value
=
"true"
/>
</
bean
>
<!-- ====Producer side end====-->
<!-- ====Consumer side start====-->
<!-- 定義Queue監聽器 -->
<
jms:listener-container
destination-type
=
"queue"
container-type
=
"default"
connection-factory
=
"connectionFactory"
acknowledge
=
"auto"
>
<
jms:listener
destination
=
"test.queue"
ref
=
"queueReceiver"
/>
<
jms:listener
destination
=
"test.queue"
ref
=
"queueReceiver2"
/>
</
jms:listener-container
>
<!-- 定義Topic監聽器 -->
<
jms:listener-container
destination-type
=
"topic"
container-type
=
"default"
connection-factory
=
"connectionFactory"
acknowledge
=
"auto"
>
<
jms:listener
destination
=
"test.topic"
ref
=
"topicReceiver"
/>
<
jms:listener
destination
=
"test.topic"
ref
=
"topicReceiver2"
/>
</
jms:listener-container
>
<!-- ====Consumer side end==== -->
</
beans
>
|
隊列消息生產者spring
QueueSender.java
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
org.xdemo.example.SpringActivemq.mq.producer.queue;
import
javax.jms.JMSException;
import
javax.jms.Message;
import
javax.jms.Session;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.beans.factory.annotation.Qualifier;
import
org.springframework.jms.core.JmsTemplate;
import
org.springframework.jms.core.MessageCreator;
import
org.springframework.stereotype.Component;
/**
* @做者 Goofy
* @郵件 252878950@qq.com
* @日期 2014-4-1上午9:40:24
* @描述 發送消息到隊列
*/
@Component
public
class
QueueSender {
@Autowired
@Qualifier
(
"jmsQueueTemplate"
)
private
JmsTemplate jmsTemplate;
//經過@Qualifier修飾符來注入對應的bean
/**
* 發送一條消息到指定的隊列(目標)
* @param queueName 隊列名稱
* @param message 消息內容
*/
public
void
send(String queueName,
final
String message){
jmsTemplate.send(queueName,
new
MessageCreator() {
@Override
public
Message createMessage(Session session)
throws
JMSException {
return
session.createTextMessage(message);
}
});
}
}
|
主題(Topic)消息生產者TopicSender.java
1
2
3
4
5
6
|