Spring集成:从JMS读取->;处理消息->;持久化数据库
我使用springintegration从JMS读取消息,处理它们,然后使用我自己的dbPersistor的persist方法将它们持久化到数据库中,该方法的返回类型为void。我编写了一个测试用例来验证发布到JMS的消息是否成功地持久化到数据库中。对于此测试,我的SI和JMS配置如下所示-Spring集成:从JMS读取->;处理消息->;持久化数据库,spring,jms,spring-integration,persistence,Spring,Jms,Spring Integration,Persistence,我使用springintegration从JMS读取消息,处理它们,然后使用我自己的dbPersistor的persist方法将它们持久化到数据库中,该方法的返回类型为void。我编写了一个测试用例来验证发布到JMS的消息是否成功地持久化到数据库中。对于此测试,我的SI和JMS配置如下所示- <int:poller fixed-delay="500" default="true"/> <int:channel id="inputChannel"> <int
<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
inputChannel
不应是队列通道-消息插入队列时JMS事务将提交-DB事务不会在JMS事务的范围内执行。为此,您必须使用直接通道(卸下轮询器和
)。看谢谢Gary,将
inputChannel
更改为DirectChannel
帮助我解决了事务管理问题。现在,失败的消息保留在后端消息存储中。但我的第一个问题仍然存在——如何让主线程在继续执行其余代码之前等待SI链完成执行?目前,在调用主线程中的其余代码之前,我使用了一个Thread.sleep()
。正如我在我的#2中所说的,您不能“轻松”让测试线程等待—您必须将其他组件编织到流中。在预期结果出现之前(或者某个时间限制到期,表明测试失败),继续轮询数据库更容易。我明白你的意思。我将采用在流中添加倒计时锁存器的方法。谢谢你的回复和解释!