JMS整合Spirng

JMS整合Spirng实现异步发送消息,简单实例:

 

1、下载ActiveMQhttp://activemq.apache.org/download.html

 

2、新建java project,添加spring功能,由于我不太信任Myeclipse添加的spring的jar包,所以我自己添加了spring的jar包,只用它给我生成的那个applicationContext.xml文件,另外加上:activemq-all-5.3.1.jar、log4j-1.2.15.jar、slf4j-api-1.5.8.jar、slf4j-log4j12-1.5.6.jar

 

3、下面贴上所有代码:

--------MessageProducer :

1.package com.starit.jmsspring; 2. 3.import org.springframework.jms.core.JmsTemplate; 4. 5./** 6. * MessageProducer 7. * @author lisanlai 8. * 9. */ 10.public class MessageProducer { 11. 12. private JmsTemplate jmsTemplate; 13. 14. public void setJmsTemplate(JmsTemplate jmsTemplate) { 15. this.jmsTemplate = jmsTemplate; 16. } 17. 18. /** 19. * 发送消息 20. * @param msg 21. */ 22. public void sendMsg(Object msg){ 23. this.jmsTemplate.convertAndSend(msg); 24. } 25.} package com.starit.jmsspring; import org.springframework.jms.core.JmsTemplate; /** * MessageProducer * @author lisanlai * */ public class MessageProducer { private JmsTemplate jmsTemplate; public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } /** * 发送消息 * @param msg */ public void sendMsg(Object msg){ this.jmsTemplate.convertAndSend(msg); } } ------------MessageListener: Java代码 1.package com.starit.jmsspring; 2. 3./** 4. * MessageListener 5. * @author lisanlai 6. * 7. */ 8.public class MessageListener { 9. /** 10. * 接收消息 11. * @param msg 12. */ 13. public void getMessage(Object msg){ 14. if(msg instanceof String){ 15. System.out.println("监听到消息:"+msg); 16. } 17. } 18.} package com.starit.jmsspring; /** * MessageListener * @author lisanlai * */ public class MessageListener { /** * 接收消息 * @param msg */ public void getMessage(Object msg){ if(msg instanceof String){ System.out.println("监听到消息:"+msg); } } } ----------applicationContext.xml: Xml代码 1.<?xml version="1.0" encoding="UTF-8"?> 2.<beans xmlns="http://www.springframework.org/schema/beans" 3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 5. 6. <!-- 属性文件的配置 --> 7. <bean id="propertyConfigurer" 8. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 9. <property name="locations"> 10. <list> 11. <value>jms_spring.properties</value> 12. </list> 13. </property> 14. </bean> 15. 16. <!-- beans配置开始 --> 17. 18. <!-- JMS ConnectionFactory --> 19. <bean id="connectionFactory" 20. class="org.apache.activemq.ActiveMQConnectionFactory"> 21. <property name="brokerURL" value="${jms.brokerURL}" /> 22. </bean> 23. 24. <!-- JMS Queue Template --> 25. <bean id="jmsTemplate" 26. class="org.springframework.jms.core.JmsTemplate102"> 27. <property name="connectionFactory" ref="connectionFactory" /> 28. <property name="timeToLive" value="${jms.timeToLive}" /> 29. <property name="defaultDestinationName" 30. value="${jms.destinationName}" /> 31. <property name="receiveTimeout" value="${jms.receiveTimeout}" /> 32. </bean> 33. 34. <!-- Message porducer 消息生产者--> 35. <bean id="msgProducer" class="com.starit.jmsspring.MessageProducer"> 36. <property name="jmsTemplate" ref="jmsTemplate" /> 37. </bean> 38. 39. 40. <!-- 消息监听器 --> 41. <bean id="messageListener" 42. class="org.springframework.jms.listener.adapter.MessageListenerAdapter"> 43. <constructor-arg> 44. <bean 45. class="com.starit.jmsspring.MessageListener" /> 46. </constructor-arg> 47. <property name="defaultListenerMethod" value="getMessage" /> 48. </bean> 49. 50. <bean id="listenerContainer" 51. class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 52. <property name="connectionFactory" ref="connectionFactory" /> 53. <property name="destinationName" 54. value="${jms.destinationName}" /> 55. <property name="messageListener" ref="messageListener" /> 56. </bean> 57. 58.</beans>

-----------jms_spring.properties:

jms.brokerURL=tcp://localhost:61616
jms.receiveTimeout=3000
jms.destinationName=starit.queue
jms.timeToLive=86400000

------------测试类Test:1.package com.starit.jmsspring; 2. 3.import org.springframework.context.support.AbstractApplicationContext; 4.import org.springframework.context.support.ClassPathXmlApplicationContext; 5. 6./** 7. * 测试类 8. * @author lisanlai 9. * 10. */ 11.public class Test { 12. 13. public static void main(String[] args) { 14. AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" }); 15. MessageProducer msgProducer = (MessageProducer) ctx.getBean("msgProducer"); 16. msgProducer.sendMsg("Hello JMS and Spring!这是一个JMS整合Spring的DEMO"); 17. for(int i=1;i<=10;i++){ 18. msgProducer.sendMsg("#这是第"+i+"条消息……"); 19. try { 20. Thread.sleep(2000); 21. } catch (InterruptedException e) { 22. e.printStackTrace(); 23. } 24. } 25. msgProducer.sendMsg("消息成功 发送完成!"); 26. } 27. 28.}

你可能感兴趣的:(spring,bean,object,MyEclipse,jms,Class)