SpringJMS解析1-使用示例
Spring配置文件:
<?xml version="1.0" encoding="UTF-8"?> <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-2.0.xsd"> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL"> <value>tcp://localhost:61616</value> </property> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory"> <ref bean="connectionFactory"/> </property> </bean> <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg index="0"> <value>HelloWorldTopic</value> </constructor-arg> </bean> </beans>
发布者:
package ch13.JMS; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; public class HelloWorldSenderPS { public static void main(String[] args) throws Exception{ ApplicationContext context = new ClassPathXmlApplicationContext( new String[] {"ch13/JMS/applicationContext-PS.xml"}); JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate"); Destination destination = (Destination) context.getBean("destination"); for (int i = 0; i < 2; i++) { System.out.println("Sending Message: Foo"+i); jmsTemplate.convertAndSend(destination,"Foo "+i); try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
这里我们使用了convertAndSend的方式进行发送,这个方法将一个String转换成一个TextMessage实例,并发送他道指定的destination---这一切完全都是自动的,如果不这样,我们就必须通过以下冗长的代码:
jmsTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage("11"); } });
Spring使用一个MessageConvert接口的实现类来把一个java对象转换到一个可发送的消息实例,当使用receiveAndConvert()方法接受一个消息时候,Spring使用MessageConvert来进行相反地操作,转换一个消息实例到一个java对象。
接收者:
package ch13.JMS; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.TextMessage; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jms.core.JmsTemplate; public class HelloWorldReciverPS { public static void main(String args[]){ ApplicationContext context=new ClassPathXmlApplicationContext( new String[]{"ch13/JMS/applicationContext-PS.xml"}); JmsTemplate jmsTemplate=(JmsTemplate)context.getBean("jmsTemplate"); Destination destination=(Destination)context.getBean("destination"); while(true){ TextMessage msg=(TextMessage)jmsTemplate.receive(destination); try { System.out.println("Recieved:"+msg.getText()); } catch (JMSException e) { System.out.println(e.getMessage()); } } } }
测试结果:
发送端出现:
Sending Message: Foo0
Sending Message: Foo1
接收端出现:
Recieved:Foo0
Recieved:Foo1