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

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="UIL2ConnectionFactory"></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="transactionManagerCommon" />
</bean>


<bean id="transactionManagerCommon" class="org.springframework.jms.connection.JmsTransactionManager">
  <property name="connectionFactory" ref="jmsQueueConnectionFactory" />
</bean>

你可能感兴趣的:(spring,jms)