Spring / WebLogic - 失败时未重新传递JMS消息

时间:2014-06-19 02:04:59

标签: java jms weblogic spring-3

我正在使用WerbLogic 10.3.5和Spring 3.0来实现JMS队列。我有以下Spring配置:

    <!--  JMS Configuration -->
<bean id="paymentlistener" class="com.myproject.service.impl.PaymentListener"/> 

<bean id="paymentlistenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="concurrentConsumers" value="10"/>
    <property name="connectionFactory" ref="paymentConnectionFactory"/>
    <property name="destination" ref="paymentQueue"/>
    <property name="messageListener" ref="paymentlistener"/>
</bean>

<bean id="paymentQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jms/paymentResponseHandlerQueue"/>
    <property name="jndiTemplate" ref="jndiTemplate"/>
</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>
        </props>
    </property>
</bean>

<bean id="paymentConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate" ref="jndiTemplate"/>
    <property name="jndiName" value="jms/paymentResponseHandlerConnectionFactory"/>
</bean>

<bean id="jmsDestinationResolver" class="org.springframework.jms.support.destination.JndiDestinationResolver">
    <property name="jndiTemplate" ref="jndiTemplate"/>
    <property name="cache" value="true"/>
</bean>

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="paymentConnectionFactory"/>
    <property name="destinationResolver" ref="jmsDestinationResolver"/>
    <property name="defaultDestination" ref="paymentQueue"/>
    <property name="sessionAcknowledgeModeName" value="DUPS_OK_ACKNOWLEDGE"/>
    <property name="sessionTransacted" value="true"/>
</bean>

我的消息创建代码如下所示:

    MessageCreator messageCreator = new MessageCreator() {
        public Message createMessage(Session session) throws JMSException {
                logger.debug("Session ack mode: " + session.getAcknowledgeMode());
                return session.createObjectMessage(payment);
        }
    };


    jmsTemplate.send("jms/paymentResponseHandlerQueue", messageCreator);

我的听众看起来像这样:

@Override
public void onMessage(Message message) { // , Session session
if (!(message instanceof ObjectMessage)) {
    throw new IllegalStateException("The PaymentListener queue expects an object message"); 
}

ObjectMessage objectMessage = (ObjectMessage) message;

try {
    logger.debug("Is Message redelivered:" + objectMessage.getJMSRedelivered ());

    if (objectMessage.getObject() instanceof CreditCardPaymentDTO) {

        logger.debug("Object is of type CreditCardPaymentDTO");

        // The user that just logged in
        CreditCardPaymentDTO payment  = (CreditCardPaymentDTO) objectMessage.getObject();               
        otpCollectorDAO.savePayment(payment);
    }
} catch (JMSException e) {
    logger.error(e);
    throw new RuntimeException(e);
} catch (PaymentResponseException e) {
    logger.error(e);
    throw new RuntimeException(e);
}
}

正在正确创建消息,并且正在调用我的侦听器的onMessage()方法,但是如果逻辑失败并且我抛出RuntimeException(),则消息不会被重新传递。我尝试了上述代码的许多细微变化(例如,设置sessionAcknowledgeModeName = SESSION_TRANSACTED并显式回滚事务),但消息永远不会重新排队。将失败的消息转发给重新传递的技巧是什么?

1 个答案:

答案 0 :(得分:3)

我认为您需要为sessionTransacted设置trueDefaultMessageListenerContainer

<bean id="paymentlistenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    ...
    <property name="sessionTransacted" value="true"/>
</bean>

您可以找到here相关参考文档。