Spring提供了一個(gè)用于簡(jiǎn)化JMS API使用的抽象框架,用戶利用Spring使用JMS可以不用關(guān)心connection factory,session等資源的管理.類似于對(duì)JDBC的抽象,Spring提供了一個(gè)JmsTemplate類,抽象了操作JMS都必須做的公共步驟,留下了callback方法給用戶去實(shí)現(xiàn),如提供消息的真正內(nèi)容等.
本文主要講解一下怎么使用Spring來(lái)發(fā)送消息,接受消息和異步接受消息(MessageListener). 一,發(fā)送消息: 1,通過(guò)JmsTemplate的 send方法和提供一個(gè)MessageCreator的實(shí)現(xiàn)的最簡(jiǎn)單應(yīng)用: JAVA類:
public class JMSsenderBean { private JmsTemplate jmsTemplate; public void simpleSend() {
this.jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage("hello queue world"); } }); } 配置:(本例在weblogic上配置了JMS服務(wù)) <beans> <bean id="JMSsenderBean" class="com.test.spring.jms.JMSsenderBean"> <property name="jmsTemplate"> <ref local="jmsTemplate"></ref> </property> <property name="queue"> <ref local="destination1"></ref> </property> </bean> <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>jms/jmsconf</value> </property> <property name="jndiTemplate"> <ref local="jndiTemplate"></ref> </property> </bean> <bean id="destination1" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>jms/jmsq1</value> </property> <property name="jndiTemplate"> <ref local="jndiTemplate"></ref> </property> </bean> <bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>jms/jmsq</value> </property> <property name="jndiTemplate"> <ref local="jndiTemplate"></ref> </property> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory"> <ref local="connectionFactory" /> </property> <property name="defaultDestination"> <ref local="destination" /> </property> </bean> <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"> <property name="environment"> <props> <prop key="java.naming.factory.initial"> weblogic.jndi.WLInitialContextFactory </prop> <prop key="java.naming.provider.url"> t3://localhost:7001 </prop> <!-- <prop key="java.naming.security.authentication"> weblogic </prop> <prop key="java.naming.security.credentials"> security </prop> --> </props> </property> </bean> </beans> 2,在發(fā)送的時(shí)候指定Queue:(配置同上)
public void withQSend() {
this.jmsTemplate.send(queue, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage("hello queue world to jmsq1"); } }); } 3,通過(guò)JmsTemplate的 convertAndSend方法和提供一個(gè)MessageConverter的實(shí)現(xiàn)來(lái)將傳入的對(duì)象轉(zhuǎn)成message: public void convertAndSend(MsgObject object) {
this.jmsTemplate.setMessageConverter(new MyMessageConverter()); this.jmsTemplate.convertAndSend(object); } public class MyMessageConverter implements MessageConverter{ public Object fromMessage(Message message) throws JMSException, MessageConversionException { TextMessage msg = (TextMessage)message; MsgObject obj = new MsgObject(); obj.setName("from message"); return obj; } public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException { MsgObject obj = (MsgObject)object; return session.createTextMessage("from Object MsgObject " + obj.getName() + " " + obj.getAge()); } } 4,實(shí)現(xiàn)SessionCallback,利用 JmsTemplate的execute方法來(lái)對(duì)session進(jìn)行操作,可以發(fā)送和接受消息. public void sessionCbkSend() {
this.jmsTemplate.execute(new MySessionCallback(this.jmsTemplate)); } public class MySessionCallback implements SessionCallback{
private JmsTemplate jmsTemplate; public MySessionCallback(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public Object doInJms(Session session) throws JMSException { jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { System.out.println(".............."); return session.createTextMessage("message from session back"); } }); System.out.println(".............."); return null; } } 二,接受消息 public void receiveMsg() { TextMessage msg = (TextMessage)this.jmsTemplate.receive(); try { System.out.println(msg.getText()); } catch (JMSException e) { e.printStackTrace(); } } 三,異步接受消息:實(shí)現(xiàn)MessageListener,配置listenerContainer,默認(rèn)情況下,Spring容器啟動(dòng)后,Listener就會(huì)啟動(dòng). public class ExampleListener implements MessageListener { public void onMessage(Message message) { if (message instanceof TextMessage) { try { System.out.println(((TextMessage) message).getText()); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } catch (JMSException ex) { throw new RuntimeException(ex); } } else { throw new IllegalArgumentException("Message must be of type TextMessage"); } } } 配置 <bean id="messageListener" class="com.test.spring.jms.ExampleListener"> </bean> <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="destination" /> <property name="messageListener" ref="messageListener" /> </bean> 通過(guò)listenerContainer的stop和shutdown方法停止服務(wù).
jmsContainer.stop(); jmsContainer.shutdown(); 對(duì)listener的事務(wù)管理:
如果是本地事務(wù),只需要設(shè)置listenerContainer的sessionTransacted就可以了. <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> ... <property name="sessionTransacted" value="true"/> </bean> 如果在onMessage中還有對(duì)其他資源,如數(shù)據(jù)庫(kù)的操作,需要使用JTA來(lái)控制全局事務(wù). <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/> <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
... <property name="transactionManager" ref="transactionManager" /> </bean> 本文來(lái)自CSDN博客,,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/kkdelta/archive/2010/05/18/5604218.aspx
|
|