Java Springs onMessage处理程序的事务支持
所以我有一些样板代码,它使用来自某个主题的消息:Java Springs onMessage处理程序的事务支持,java,spring,jms,jta,spring-jms,Java,Spring,Jms,Jta,Spring Jms,所以我有一些样板代码,它使用来自某个主题的消息: public void onMessage(Message message ) { try { // try my conversion } catch(MyConversionException e) { //catch conversion error but still consume off topic } //Any other error
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 MessageListenerContainerBean设置为事务性的,并且只有在成功实现时才使用它
[编辑]以下是目前为止的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>
不建议这样做,但您可以调用TransactionStatus.setRollbackOnly()
也可以考虑与事务模型一致,如果要回滚,则通过异常来执行它。那样的话。。。您需要抛出RuntimeException,这样Spring才能回滚消息。如果捕获到异常并只记录它,Spring就不知道发生了什么错误…
我希望捕获自己的异常,但不使用引发任何其他异常的任何内容。请尝试TransactionStatus.setRollbackOnly()