Spring集成JMS(二).

来源:互联网 发布:域名是世界性的吗 编辑:程序博客网 时间:2024/05/28 16:29

一.:要在spring下集成ActiveMQ,那么就需要将jar包导入项目,所需jar包如下:

这些jar包来自“apache-activemq-5.6.0\lib”中。

二:相关的实例

1.实例--1

1):.配置Spring与ActiveMQ的配置文件:

[html] view plaincopyprint?
  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" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  6.         http://www.springframework.org/schema/context   
  7.         http://www.springframework.org/schema/context/spring-context-2.5.xsd"  
  8.     default-autowire="byName">  
  9.       
  10.     <!-- 配置connectionFactory -->  
  11.     <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"  
  12.         destroy-method="stop">  
  13.         <property name="connectionFactory">  
  14.             <bean class="org.apache.activemq.ActiveMQConnectionFactory">  
  15.                 <property name="brokerURL">  
  16.                     <value>tcp://127.0.0.1:61616</value>  
  17.                 </property>  
  18.             </bean>  
  19.         </property>  
  20.         <property name="maxConnections" value="100"></property>  
  21.     </bean>  
  22.   
  23.     <!-- Spring JMS Template -->  
  24.     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
  25.         <property name="connectionFactory">  
  26.             <ref local="jmsFactory" />  
  27.         </property>  
  28.         <property name="defaultDestinationName" value="subject" />  
  29.         <!-- 区别它采用的模式为false是p2p为true是订阅 -->  
  30.         <property name="pubSubDomain" value="true" />  
  31.     </bean>  
  32.   
  33.     <!-- 发送消息的目的地(一个Topic) -->  
  34.     <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">  
  35.         <!-- 设置消息队列的名字 -->  
  36.         <constructor-arg index="0" value="subject" />  
  37.     </bean>  
  38.     <bean id="messageReceiver" class="com.pwc.spring.jms.example.JMSReceiver">  
  39.         <property name="jmsTemplate" ref="jmsTemplate"></property>  
  40.     </bean>  
  41.       
  42. </beans>  
2):.消息的发送者:

[java] view plaincopyprint?
  1. package com.pwc.spring.jms.example;  
  2.   
  3. import javax.jms.Destination;  
  4. import javax.jms.JMSException;  
  5. import javax.jms.Message;  
  6. import javax.jms.Session;  
  7.   
  8. import org.springframework.context.ApplicationContext;  
  9. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  10. import org.springframework.jms.core.JmsTemplate;  
  11. import org.springframework.jms.core.MessageCreator;  
  12.   
  13. public class JMSSender {  
  14.   
  15.     /** 
  16.      * @param args 
  17.      */  
  18.     public static void main(String[] args) {  
  19.         // TODO Auto-generated method stub  
  20.         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext-jms.xml");  
  21.         JmsTemplate template=(JmsTemplate)applicationContext.getBean("jmsTemplate");  
  22.         Destination destination=(Destination)applicationContext.getBean("destination");  
  23.         template.send(destination, new MessageCreator(){  
  24.   
  25.             @Override  
  26.             public Message createMessage(Session session) throws JMSException {  
  27.                 // TODO Auto-generated method stub  
  28.                 return session.createTextMessage("Send Message:Hello ActiveMQ Text Message!");  
  29.             }  
  30.               
  31.         });  
  32.         System.out.println("Send the JMS Message Successfully!");  
  33.     }  
  34.   
  35. }  
3):.消息的接收者:

A:消息接收的入口:

[java] view plaincopyprint?
  1. package com.pwc.spring.jms.example;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class JMSReceiverMain {  
  7.   
  8.     /** 
  9.      * @param args 
  10.      */  
  11.     public static void main(String[] args) {  
  12.         // TODO Auto-generated method stub  
  13.         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext-jms.xml");  
  14.         JMSReceiver proxyJMSConsumer=(JMSReceiver)applicationContext.getBean("messageReceiver");  
  15.         proxyJMSConsumer.receive();  
  16.         System.out.println("Initial Consumer End! ");  
  17.     }  
  18.   
  19. }  
B:消息接收的具体方法:

[java] view plaincopyprint?
  1. package com.pwc.spring.jms.example;  
  2.   
  3. import javax.jms.Destination;  
  4. import javax.jms.JMSException;  
  5. import javax.jms.TextMessage;  
  6.   
  7. import org.springframework.context.ApplicationContext;  
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  9. import org.springframework.jms.JmsException;  
  10. import org.springframework.jms.core.JmsTemplate;  
  11.   
  12. public class JMSReceiver {  
  13.   
  14.     public JMSReceiver(){  
  15.           
  16.     }  
  17.     private JmsTemplate jmsTemplate;  
  18.     public JmsTemplate getJmsTemplate() {  
  19.         return jmsTemplate;  
  20.     }  
  21.     public void setJmsTemplate(JmsTemplate jmsTemplate) {  
  22.         this.jmsTemplate = jmsTemplate;  
  23.     }  
  24.       
  25.     public void receive(){  
  26.         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext-jms.xml");  
  27.         Destination destination=(Destination)applicationContext.getBean("destination");  
  28.         while(true){  
  29.             try {  
  30.                 TextMessage txtmsg=(TextMessage)jmsTemplate.receive(destination);  
  31.                 if(null!=txtmsg){  
  32.                     System.out.println("DB Proxy: "+txtmsg);  
  33.                     System.out.println("DB Proxy Message: "+txtmsg.getText());  
  34.                 }else{  
  35.                                    break;  
  36.                                 }  
  37.             } catch (JmsException e) {  
  38.                 e.printStackTrace();  
  39.             } catch (JMSException e) {  
  40.                 e.printStackTrace();  
  41.             }  
  42.         }  
  43.     }  
  44. }  
先运行消息的接收者,再运行消息的发送者;

运行结果:


2. 实例--2

1)Spring与ActiveMQ配置文件信息:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.       
  7.     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  8.     <!--创建连接工厂-->  
  9.     <bean id="connectionFactory"  
  10.         class="org.apache.activemq.ActiveMQConnectionFactory">  
  11.         <property name="brokerURL" value="tcp://localhost:61616"></property>  
  12.     </bean>  
  13.     <!-- 声明ActiveMQ消息目标,目标可以是一个队列,也可以是一个主题ActiveMQTopic-->  
  14.     <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">  
  15.         <constructor-arg index="0" value="springactivemq"></constructor-arg>  
  16.     </bean>  
  17.     <!---->  
  18.     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
  19.         <property name="connectionFactory" ref="connectionFactory"></property>  
  20.         <property name="defaultDestination" ref="destination"></property>  
  21.         <property name="receiveTimeout" value="600"></property>  
  22.   
  23.     </bean>  
  24.     <bean id="sender" class="com.pwc.spring.jms.further.Sender">  
  25.         <property name="jmsTemplate" ref="jmsTemplate"></property>  
  26.   
  27.     </bean>  
  28.     <bean id="receiver" class="com.pwc.spring.jms.further.Receiver">  
  29.         <property name="jmsTemplate" ref="jmsTemplate"></property>  
  30.     </bean>  
  31. </beans>  
2. 发送者发送消息

A:发送消息的入口

[java] view plaincopyprint?
  1. package com.pwc.spring.jms.further;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class SenderMain {  
  7.   
  8.     /** 
  9.      * @param args 
  10.      */  
  11.     public static void main(String[] args) {  
  12.         // TODO Auto-generated method stub  
  13.         ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");  
  14.         Sender sender=(Sender)context.getBean("sender");  
  15.         sender.sendInfo();  
  16.     }  
  17.   
  18. }  
B:发送消息的具体方法

[java] view plaincopyprint?
  1. package com.pwc.spring.jms.further;  
  2.   
  3. import javax.jms.JMSException;  
  4. import javax.jms.MapMessage;  
  5. import javax.jms.Message;  
  6. import javax.jms.Session;  
  7.   
  8. import org.springframework.jms.core.JmsTemplate;  
  9. import org.springframework.jms.core.MessageCreator;  
  10.   
  11. public class Sender {  
  12.   
  13.     private JmsTemplate jmsTemplate;  
  14.   
  15.     public JmsTemplate getJmsTemplate() {  
  16.         return jmsTemplate;  
  17.     }  
  18.   
  19.     public void setJmsTemplate(JmsTemplate jmsTemplate) {  
  20.         this.jmsTemplate = jmsTemplate;  
  21.     }  
  22.       
  23.     public void sendInfo(){  
  24.         jmsTemplate.send(new MessageCreator(){  
  25.   
  26.             @Override  
  27.             public Message createMessage(Session session) throws JMSException {  
  28.                 MapMessage message=session.createMapMessage();  
  29.                 message.setString("lastName""Leio");  
  30.                 return message;  
  31.             }  
  32.               
  33.         });  
  34.     }  
  35. }  
3. 接收者接收消息

A:接收消息入口:

[java] view plaincopyprint?
  1. package com.pwc.spring.jms.further;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class ReceiverMain {  
  7.   
  8.     /** 
  9.      * @param args 
  10.      */  
  11.     public static void main(String[] args) {  
  12.         // TODO Auto-generated method stub  
  13.         ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");  
  14.         Receiver receiver=(Receiver) context.getBean("receiver");  
  15.         System.out.println(receiver.receiveMessage());  
  16.     }  
  17.   
  18. }  
B:接收消息方法:

[java] view plaincopyprint?
  1. package com.pwc.spring.jms.further;  
  2.   
  3. import javax.jms.JMSException;  
  4. import javax.jms.MapMessage;  
  5.   
  6. import org.springframework.jms.core.JmsTemplate;  
  7.   
  8. public class Receiver {  
  9.   
  10.     private JmsTemplate jmsTemplate;  
  11.   
  12.     public JmsTemplate getJmsTemplate() {  
  13.         return jmsTemplate;  
  14.     }  
  15.   
  16.     public void setJmsTemplate(JmsTemplate jmsTemplate) {  
  17.         this.jmsTemplate = jmsTemplate;  
  18.     }  
  19.     public Receiver(){  
  20.           
  21.     }  
  22.     public String receiveMessage(){  
  23.         String receiveMessage="";  
  24.         MapMessage message=(MapMessage)jmsTemplate.receive();  
  25.         try {  
  26.             receiveMessage=message.getString("lastName");  
  27.         } catch (JMSException e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.         return receiveMessage;  
  31.     }  
  32. }  
先运行发送者,再运行接收者,结果如下:


原创粉丝点击