Java 如何使用ApacheCamel触发路由并收集结果?
路由A由quartz2组件触发(根据cron计划)。当路由A完成其业务逻辑时,在某些情况下,我需要触发路由B,它将读取给定目录中的所有文件,并将结果发送到另一个路由C,但不希望路由B一直运行Java 如何使用ApacheCamel触发路由并收集结果?,java,apache-camel,jbossfuse,Java,Apache Camel,Jbossfuse,路由A由quartz2组件触发(根据cron计划)。当路由A完成其业务逻辑时,在某些情况下,我需要触发路由B,它将读取给定目录中的所有文件,并将结果发送到另一个路由C,但不希望路由B一直运行 当路线A需要时,如何只运行路线B一次?目前,我正在使用路线A中的“controlbus:”命令启动路线B(autoStart=false),路线C再次使用“controlbus:”停止路线B。您能为我的用例提出更好的解决方案吗 “路由A”实现了池消费者,它轮询给定目录中的所有文件(在我们迭代时)。当我们收到
当路线A需要时,如何只运行路线B一次?目前,我正在使用路线A中的“controlbus:”命令启动路线B(autoStart=false),路线C再次使用“controlbus:”停止路线B。您能为我的用例提出更好的解决方案吗 “路由A”实现了池消费者,它轮询给定目录中的所有文件(在我们迭代时)。当我们收到空正文时,轮询将停止(选中SendEmptyMessageWhenId选项)
公共作废进程(Exchange)引发异常{
列表结果=新建ArrayList();
字符串endpointUrl=”file:////tmp?sendEmptyMessageWhenIdle=true"
PollingConsumer pConsumer=camelContext.getEndpoint(endpointUrl.createPollingConsumer();
pConsumer.start();
通用文件gFile;
做{
Exchange ex=pConsumer.receive(扫描超时);
gFile=(GenericFile)ex.getIn().getBody();
如果(gFile!=null){
结果。添加(gFile);
}
//在文件端点url中使用&sendEmptyMessageWhenIdle=true或设置接收超时,否则您将永远无法知道文件夹扫描何时完成!!!
}while(gFile!=null);
pConsumer.stop();
//其他业务逻辑
}
public void process(Exchange exchange) throws Exception {
List<GenericFile> result = new ArrayList<GenericFile>();
String endpointUrl = "file:////tmp?sendEmptyMessageWhenIdle=true"
PollingConsumer pConsumer = camelContext.getEndpoint(endpointUrl).createPollingConsumer();
pConsumer.start();
GenericFile<File> gFile;
do {
Exchange ex = pConsumer.receive(SCAN_TIMEOUT);
gFile = (GenericFile<File>) ex.getIn().getBody();
if(gFile != null) {
result.add(gFile);
}
// use &sendEmptyMessageWhenIdle=true in file endpoint url OR set receive timeouts otherwise you will never know when folder scan has finished !!!
} while(gFile != null);
pConsumer.stop();
// other business logic
}