ITeye博客

Spring JMS 整合Tomcat和ActiveMQ

gowithbutton · 2011-12-21 14:34 · java · Spring JMS 整合Tomcat和ActiveMQ · Markdown

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

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

加入以下代码


	<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

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

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

随便打入一点消息

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

附件是eclipse工程代码

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

      
      大小: 24.5 KB

    </ul>
  

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

      
      大小: 14.3 KB

    </ul>
  

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

      
      大小: 11 KB

    </ul>
  

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

      
      大小: 63.8 KB

    </ul>
  

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

      
      大小: 27.9 KB

    </ul>
  

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

      
      大小: 56.7 KB

    </ul>
  

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

      
      大小: 65.9 KB

    </ul>
  

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

      
      下载次数: 5

    
  


  
    [查看图片附件](#)

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。