Spring JMS 整合Tomcat和ActiveMQ

1.Active MQ安装配置

1.1.下载并解压Active MQ
1.2.双击bin/activemq.bat启动Active MQ
1.3.浏览器输入http://localhost:8161/admin/ ,可以图形化界面管理activemq
1.4.点击Queue,新建一个队列名字叫TestQueue1

[img]http://dl.iteye.com/upload/attachment/598984/fa106a47-0428-3986-8c6b-4d8ddbf2519b.jpg[/img]

2.在Eclipse里面配置Tomcat的context.xml,这样Spring就能以JNDI方式访问ActiveMQ了

[img]http://dl.iteye.com/upload/attachment/598986/91153192-a5c8-3486-9ddb-bbbc27eb16a5.jpg[/img]

加入以下代码

<Resource name="jms/ConnectionFactory"
auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="tcp://localhost:61616"
brokerName="LocalActiveMQBroker" />

<Resource name="jms/Queue"
auth="Container"
type="org.apache.activemq.command.ActiveMQQueue"
description="my Queue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="TestQueue1" />


3.导入以下Jar包
activemq-all-5.4.1.jar
commons-logging-1.1.1.jar
javax.jms_1.1.1.jar
spring.asm-3.0.0.RELEASE.jar
spring.beans-3.0.0.RELEASE.jar
spring.context-3.0.0.RELEASE.jar
spring.core-3.0.0.RELEASE.jar
spring.expression-3.0.0.RELEASE.jar
spring.jms-3.0.0.RELEASE.jar
spring.transaction-3.0.0.RELEASE.jar
spring.web-3.0.0.RELEASE.jar

4.编写2个类,一个MessageQueueSender发消息,一个MessageQueueReceiver收消息,为了方便测试,我们还写了一个Servlet调用MessageQueueSender,通过JSP来调用Servlet

5.写Spring的配置文件,假设存在这个文件里:jms-activemq.xml


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Connection factory -->
<bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jms/ConnectionFactory"></property>
</bean>

<!-- Queue -->
<bean id="jmsQueue" class="org.springframework.jndi.JndiObjectFactoryBean"
lazy-init="true">
<property name="jndiName" value="java:comp/env/jms/Queue"></property>
</bean>

<!-- Spring JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"
lazy-init="true">
<property name="connectionFactory" ref="jmsConnectionFactory"></property>
<property name="defaultDestination" ref="jmsQueue"></property>
</bean>

<!-- Sender -->
<bean id="jmsSender" class="com.xp.MessageQueueSender"
lazy-init="true">
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean>

<!-- Receiver -->
<bean id="jmsReceiver" class="com.xp.MessageQueueReceiver">
</bean>

<!-- Message Listener -->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsConnectionFactory"></property>
<property name="destination" ref="jmsQueue"></property>
<property name="messageListener" ref="jmsReceiver"></property>
<property name="autoStartup" value="true" />
</bean>

</beans>


6.启动tomcat测试看效果

6.1.浏览器输入http://localhost:8080/JMSTest/,随便输入消息比如"Hello",点击submit

[img]http://dl.iteye.com/upload/attachment/598991/b90bd9ed-f9ea-3b24-944b-a9daa69730d2.jpg[/img]

6.2.可在控制台看到Hello的信息,表明发送和接受者都已经OK

[img]http://dl.iteye.com/upload/attachment/598993/da65dd1b-df67-382c-8d24-3e6f5c94f9d9.jpg[/img]

6.3.还可以在ActiveMQ管理台创建一条消息,点击"Send To"

[img]http://dl.iteye.com/upload/attachment/598995/ec21ce44-ddf0-3579-b54a-b56f7d4f7aef.jpg[/img]

随便打入一点消息

[img]http://dl.iteye.com/upload/attachment/598997/c10e76a6-f489-3d19-88f7-40c3833339b8.jpg[/img]

6.4.可在控制台看到接受者能收到此消息

[img]http://dl.iteye.com/upload/attachment/598999/1dcdb45b-f051-379b-aa91-d47d6b6b7913.jpg[/img]

附件是eclipse工程代码



<ul style="display:none;">
[*][url=http://dl.iteye.com/upload/attachment/598984/fa106a47-0428-3986-8c6b-4d8ddbf2519b.jpg]<img src='http://dl.iteye.com/upload/attachment/598984/fa106a47-0428-3986-8c6b-4d8ddbf2519b-thumb.jpg' class='magplus' title='点击查看原始大小图片' />[/url]


[*]大小: 24.5 KB

</ul>



<ul style="display:none;">
[*][url=http://dl.iteye.com/upload/attachment/598986/91153192-a5c8-3486-9ddb-bbbc27eb16a5.jpg]<img src='http://dl.iteye.com/upload/attachment/598986/91153192-a5c8-3486-9ddb-bbbc27eb16a5-thumb.jpg' class='magplus' title='点击查看原始大小图片' />[/url]


[*]大小: 14.3 KB

</ul>



<ul style="display:none;">
[*][url=http://dl.iteye.com/upload/attachment/598991/b90bd9ed-f9ea-3b24-944b-a9daa69730d2.jpg]<img src='http://dl.iteye.com/upload/attachment/598991/b90bd9ed-f9ea-3b24-944b-a9daa69730d2-thumb.jpg' class='magplus' title='点击查看原始大小图片' />[/url]


[*]大小: 11 KB

</ul>



<ul style="display:none;">
[*][url=http://dl.iteye.com/upload/attachment/598993/da65dd1b-df67-382c-8d24-3e6f5c94f9d9.jpg]<img src='http://dl.iteye.com/upload/attachment/598993/da65dd1b-df67-382c-8d24-3e6f5c94f9d9-thumb.jpg' class='magplus' title='点击查看原始大小图片' />[/url]


[*]大小: 63.8 KB

</ul>



<ul style="display:none;">
[*][url=http://dl.iteye.com/upload/attachment/598995/ec21ce44-ddf0-3579-b54a-b56f7d4f7aef.jpg]<img src='http://dl.iteye.com/upload/attachment/598995/ec21ce44-ddf0-3579-b54a-b56f7d4f7aef-thumb.jpg' class='magplus' title='点击查看原始大小图片' />[/url]


[*]大小: 27.9 KB

</ul>



<ul style="display:none;">
[*][url=http://dl.iteye.com/upload/attachment/598997/c10e76a6-f489-3d19-88f7-40c3833339b8.jpg]<img src='http://dl.iteye.com/upload/attachment/598997/c10e76a6-f489-3d19-88f7-40c3833339b8-thumb.jpg' class='magplus' title='点击查看原始大小图片' />[/url]


[*]大小: 56.7 KB

</ul>



<ul style="display:none;">
[*][url=http://dl.iteye.com/upload/attachment/598999/1dcdb45b-f051-379b-aa91-d47d6b6b7913.jpg]<img src='http://dl.iteye.com/upload/attachment/598999/1dcdb45b-f051-379b-aa91-d47d6b6b7913-thumb.jpg' class='magplus' title='点击查看原始大小图片' />[/url]


[*]大小: 65.9 KB

</ul>



[list]
[*][url=http://dl.iteye.com/topics/download/ecf37ba7-ca95-3039-af6c-b324560f71c5]JMSTest.zip[/url] (8.9 KB)


[*]下载次数: 5

[/list]



[list]
[*][url=#]查看图片附件[/url]

[/list]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值