久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Spring對(duì)JMS的支持

 wangxq 2011-04-08
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

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多