【发布时间】:2017-10-17 00:21:00
【问题描述】:
我使用 Spring Integration 从 JMS 读取消息,处理它们,然后使用我自己的 dbPersistor 的具有返回类型 void 的持久方法将它们持久化到数据库中。我编写了一个测试用例来验证发布到 JMS 的消息是否已成功保存在数据库中。本次测试我的 SI 和 JMS 配置如下 -
<int:poller fixed-delay="500" default="true"/>
<int:channel id="inputChannel">
<int:queue/>
</int:channel>
<int:channel id="errorChannel">
<int:queue/>
</int:channel>
<jms:message-driven-channel-adapter id="jmsInboudAdapter"
connection-factory="connectionFactory" destination-name="MessageQueue"
channel="inputChannel" error-channel="errorChannel" transaction-manager="dbTxManager"
acknowledge="transacted"/>
<int:chain id="handlerChain" input-channel="inputChannel">
<int:service-activator ref="jmsMessageHandler" method="handleMessage" />
<int:service-activator ref="dbPersistor" method="persist" />
</int:chain>
然后在测试中我执行以下操作 -
- jmsTemplate.send()
- verifyMessageWasPersistedToDB
当我只向数据库发布一条消息时,这非常有用。但是当我通过 jmsTemplate.send() 循环发布多条消息时,主线程完成了操作,而 SI 线程仍在执行并尝试验证 DB 中的消息,但由于某些消息尚未持久化而失败。我的问题是 -
- 如何让主线程等待 SI 线程完成后再调用 verify 方法?
- 如果发生 db 异常和回滚,我如何验证失败的消息是否回到了原始队列中?
谢谢 阿杰
【问题讨论】:
标签: spring jms spring-integration persistence