`
liwanchun_xd
  • 浏览: 120809 次
  • 来自: ...
文章分类
社区版块
存档分类
最新评论

使用Spring实现JMS接收消息(XA事务)

阅读更多
1、开发环境: eclipse3.2+jdk1.5+jboss4.2.2
2、使用XA实现事务。

消息接收器代码:
public class ReceiverListener implements SessionAwareMessageListener  {

   public void onMessage(Message message, Session session)
        throws JMSException {
if (message instanceof TextMessage) {
  try {
     System.out.println("New Receive Message: " +
                  ((TextMessage) message).getText());
  } catch (JMSException ex) {
    throw new RuntimeException(ex);
  }
} else {
  throw new IllegalArgumentException(
                 "Message must be of type TextMessage");
}
    }

}

Spring配置:
<bean id="userJmsUtil" class="com.hc360.mmt.common.UserJmsTransactionUtil"> 
<property name="destinationJndi" value="queue/A"></property> 
<property name="connectionFactoryJndi" value="java:JmsXA"></property> 
<property name="factoryInitial" value="org.jnp.interfaces.NamingContextFactory"></property> 
<property name="providerUrl" value="localhost"></property> 
<property name="factoryUrlPkgs" value="org.jboss.naming:org.jnp.interfaces"></property> 
</bean> 

<bean id="jmsQueueConnectionFactory" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 
<property name="targetObject" ref="userJmsUtil"></property> 
<property name="targetMethod" value="getConnectionFactory"></property> 
</bean> 


<bean id="destination" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 
<property name="targetObject" ref="userJmsUtil"></property> 
<property name="targetMethod" value="getDestination"></property> 
</bean>

<!-- this is the Message Driven POJO (MDP) -->
<bean id="messageListener" class="com.hc360.jms.ReceiverListener" />

<!-- and this is the attendant message listener container -->
<bean id="listenerContainer1"
  class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  <property name="concurrentConsumers" value="1"/>
  <property name="connectionFactory" ref="jmsQueueConnectionFactory" />
  <property name="destination" ref="destination" />
  <property name="messageListener" ref="messageListener" />
  <property name="transactionManager" ref="transactionManagerJTA" />
</bean>


<bean id="transactionManagerJTA" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransactionName">
<value>UserTransaction</value>
</property>
</bean>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics