读书人

Spring JMS 调整Tomcat和ActiveMQ

发布时间: 2012-08-03 00:12:14 作者: rapoo

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?

Spring JMS 调整Tomcat和ActiveMQ?

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

Spring JMS 调整Tomcat和ActiveMQ?

加入以下代码?
Xml代码??Spring JMS 调整Tomcat和ActiveMQ

  1. <Resource?name="jms/ConnectionFactory"??
  2. ???????auth="Container"??
  3. ???????type="org.apache.activemq.ActiveMQConnectionFactory"??
  4. ???????description="JMS?Connection?Factory"??
  5. ???????factory="org.apache.activemq.jndi.JNDIReferenceFactory"??
  6. ???????brokerURL="tcp://localhost:61616"??
  7. ???????brokerName="LocalActiveMQBroker"?/>??
  8. ?????
  9. ???<Resource?name="jms/Queue"??
  10. ???????auth="Container"??
  11. ???????type="org.apache.activemq.command.ActiveMQQueue"??
  12. ???????description="my?Queue"??
  13. ???????factory="org.apache.activemq.jndi.JNDIReferenceFactory"??
  14. ???????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?

Xml代码??Spring JMS 调整Tomcat和ActiveMQ
  1. <beans?xmlns="http://www.springframework.org/schema/beans"??
  2. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"???
  3. ????xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">??
  4. ??
  5. ????<!--?Connection?factory?-->??
  6. ????<bean?id="jmsConnectionFactory"?class="org.springframework.jndi.JndiObjectFactoryBean">??
  7. ????????<property?name="jndiName"?value="java:comp/env/jms/ConnectionFactory"></property>??
  8. ????</bean>??
  9. ??
  10. ????<!--?Queue?-->??
  11. ????<bean?id="jmsQueue"?class="org.springframework.jndi.JndiObjectFactoryBean"??
  12. ????????lazy-init="true">??
  13. ????????<property?name="jndiName"?value="java:comp/env/jms/Queue"></property>??
  14. ????</bean>??
  15. ??
  16. ????<!--?Spring?JMS?Template?-->??
  17. ????<bean?id="jmsTemplate"?class="org.springframework.jms.core.JmsTemplate"??
  18. ????????lazy-init="true">??
  19. ????????<property?name="connectionFactory"?ref="jmsConnectionFactory"></property>??
  20. ????????<property?name="defaultDestination"?ref="jmsQueue"></property>??
  21. ????</bean>??
  22. ??
  23. ????<!--?Sender?-->??
  24. ????<bean?id="jmsSender"?class="com.xp.MessageQueueSender"??
  25. ????????lazy-init="true">??
  26. ????????<property?name="jmsTemplate"?ref="jmsTemplate"></property>??
  27. ????</bean>??
  28. ??
  29. ????<!--?Receiver?-->??
  30. ????<bean?id="jmsReceiver"?class="com.xp.MessageQueueReceiver">??
  31. ????</bean>??
  32. ??
  33. ????<!--?Message?Listener?-->??
  34. ????<bean?id="listenerContainer"??
  35. ????????class="org.springframework.jms.listener.DefaultMessageListenerContainer">??
  36. ????????<property?name="connectionFactory"?ref="jmsConnectionFactory"></property>??
  37. ????????<property?name="destination"?ref="jmsQueue"></property>??
  38. ????????<property?name="messageListener"?ref="jmsReceiver"></property>??
  39. ????????<property?name="autoStartup"?value="true"?/>??
  40. ????</bean>??
  41. ??
  42. </beans>??


6.启动tomcat测试看效果?

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

Spring JMS 调整Tomcat和ActiveMQ?

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

Spring JMS 调整Tomcat和ActiveMQ

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

Spring JMS 调整Tomcat和ActiveMQ?

随便打入一点消息?

Spring JMS 调整Tomcat和ActiveMQ?

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

Spring JMS 调整Tomcat和ActiveMQ

附件是eclipse工程代码

读书人网 >开源软件

热点推荐