0

所以我有一些样板代码使用来自主题的消息:

    public void onMessage(Message message )
{
    try
    {
        // try my conversion
    }
    catch(MyConversionException e)
    {
        //catch conversion error but still consume off topic
    }

    //Any other error i.e. runtime errors will not cause the message to be consumed from topic. So it can be retried

}

我希望能够尝试将消息转换为另一个对象。如果这导致错误,我将使用我自己的异常处理来捕获它并将其写入错误队列。

我的问题是,如何将 Springs messageListenerContainer bean 设置为 Transactional 并且仅在成功发生时才使用???

[编辑] 这是到目前为止的bean:

    <!-- MESSAGE LISTENER CONTAINER -->
<bean id="ListenerContainer"
    class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="messageListener" ref="MessageListener" />
    <property name="connectionFactory" ref="Tcf" />
    <property name="destinationResolver" ref="JmsDestinationResolver" />
    <property name="receiveTimeout" value="${jms-timeout}" />
    <property name="destinationName" value="${jms-topic}" />
    <property name="concurrency" value="1" />
    <property name="pubSubDomain" value="true" />
    <property name="subscriptionDurable" value="${jms-durable-flag}"/>
    <property name="durableSubscriptionName" value="${jms-durable-name}" />
    <property name="clientId" value="${jms-client-id}"/>
</bean> 
4

1 回答 1

-1

不建议这样做,但可以调用 TransactionStatus.setRollbackOnly()。

此外,您可能要考虑与事务模型保持一致,如果要回滚,则通过异常来执行...在这种情况下...您需要抛出 RuntimeException 以便 Spring 回滚消息,如果您捕获异常并仅记录它,Spring 不知道出了什么问题...

于 2012-02-07T17:38:17.307 回答