我的代码中有一个生成apache camel流的循环。但是,这些作业中的每一个都需要从Oracle数据库轮询,这会导致每个用户的最大会话数错误。原因是这些作业是异步的,从而导致应用程序达到其连接到数据库的数量限制。因此,我需要一种方法来等待一个流的完成,然后触发下一个流。我想知道有没有办法做到这一点?我尝试在每个循环的末尾使用Thread.sleep
,但它不起作用。
for (int i=0; i<tasks.size(); i++) {
DataStore sourceStore = dataStores.get(tasks.get(i));
DataSource source = sourceStore.getDataSource();
DefaultRegistry registry = new DefaultRegistry();
registry.bind(tasks.get(i).getName(), source);
CamelContext context = new DefaultCamelContext(registry);
try {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from ("direct:start")
.to("jdbc:" + tasks.get(i).getSourceDataStoreName())
.split(body())
// some other job
;
}
});
} catch (Exception e) {
e.printStackTrace();
}
context.start();
ProducerTemplate producerTemplate = context.createProducerTemplate();
producerTemplate.sendBody("direct:start", sourceStore.getQuery(tasks.get(i)));
//Thread.sleep(40000); // did not work
}
发布于 2019-12-03 13:36:03
我觉得你应该试试onCompletion。根据文档,只有在原始路由完成时,才会调用onCompletion()
之后的路由。你可以在onCompletion()
之后做你的工作,例如,你可以把你的任务结果发送到某个地方。
下面是我的简单onCompletion()
示例:
context.addRoutes(new RouteBuilder()
{
@Override
public void configure() throws Exception
{
from("timer://mytimer?repeatCount=2&fixedRate=true&period=3000")
.process(exchange -> {
System.out.println("Thread will sleep 2s");
Thread.sleep(2000);
}).onCompletion().process(exchange -> System.out.println("After completion"));
}
});
https://stackoverflow.com/questions/59096681
复制相似问题