使用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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值