Springs onMessage 处理程序的事务支持
所以我有一些样板代码来使用来自某个主题的消息:
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 设置为事务性的,并且仅在成功发生时才使用???
[编辑] 这是到目前为止的 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>
So I have some boilerplate code that consumes messages from a topic:
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
}
I wish to be able to try to convert the message into another object. If this causes an error I will catch it with my own exception handling and write it to an error queue.
My question is, how do I set up Springs messageListenerContainer bean to be Transactional and only consume if this has taken place successfully???
[EDIT] Here is the bean so far:
<!-- 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不建议这样做,但可以调用TransactionStatus.setRollbackOnly()。
另外你可能需要考虑与事务模型保持一致,如果你想回滚,那么通过异常来完成......在这种情况下......你需要抛出一个 RuntimeException 以便 Spring 回滚消息,如果你捕获异常并只记录它,Spring 不知道出了什么问题......
Is not recommended to do so, but you can call TransactionStatus.setRollbackOnly().
Also you may want to consider to be consistent with the transaction model, and if you want to rollback, then do it through an exception... In that case... You need to throw a RuntimeException so that Spring rollbacks the message, if you catch the exception and only logs it, Spring has no clue that something went wrong...