sea*_*ges 15 java spring multithreading spring-integration
我有一个自我执行的jar程序,它很大程度上依赖于Spring Integration.我遇到的问题是程序在其他Spring bean完成之前终止.
下面是我正在使用的代码的简化版本,如果需要,我可以提供更多代码/配置.入口点是一个main()方法,它引导Spring并启动导入过程:
public static void main(String[] args) {
ctx = new ClassPathXmlApplicationContext("flow.xml");
DataImporter importer = (DataImporter)ctx.getBean("MyImporterBean");
try {
importer.startImport();
} catch (Exception e) {
e.printStackTrace();
} finally {
ctx.close();
}
}
Run Code Online (Sandbox Code Playgroud)
DataImporter包含一个简单的循环,用于将消息发送到Spring Integration网关.这为流程提供了一种主动的"推送"方法,而不是轮询数据的常用方法.这是我的问题所在:
public void startImport() throws Exception {
for (Item item : items) {
gatewayBean.publish(item);
Thread.sleep(200); // Yield period
}
}
Run Code Online (Sandbox Code Playgroud)
为了完整起见,流XML看起来像这样:
<gateway default-request-channel="inChannel" service-interface="GatewayBean" />
<splitter input-channel="inChannel" output-channel="splitChannel" />
<payload-type-router input-channel="splitChannel">
<mapping type="Item" channel="itemChannel" />
<mapping type="SomeOtherItem" channel="anotherChannel" />
</payload-type-router>
<outbound-channel-adapter channel="itemChannel" ref="DAOBean" method="persist" />
Run Code Online (Sandbox Code Playgroud)
流程有效地启动和处理项目,但是一旦startImport()循环结束,主线程终止并立即拆除所有Spring Integration线程.这导致竞争条件,当程序终止时,最后(n)项未被完全处理.
我有一个想法是保持我正在处理的项目的引用计数,但事实证明这非常复杂,因为流程经常将消息拆分/路由到多个服务激活器 - 这意味着很难确定每个项目是否具有"完成".
我认为我需要的是检查没有Spring bean仍在执行的方法,或者标记发送到网关的所有项目在终止之前已经完全处理.
我的问题是,我怎么可能去做其中任何一个,或者是否有更好的方法解决我没有想到的问题?
Víc*_*ero 10
您没有在此处使用请求 - 响应模式.
outbound-channel-adapter是一个fire and forget操作,如果你想等待响应,你应该使用一个等待响应的出站网关,并将响应连接到原来的网关,然后在java sendAndReceive中不仅仅是发布.