在处理从JMS收到的Exchange时,我正在动态创建一个从FTP获取文件到文件系统的路由,当批处理完成后,我需要删除相同的路由。以下代码片段显示了我如何执行此操作:
public void execute() {
try {
context.addRoutes(createFetchIndexRoute(routeId()));
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
private RouteBuilder createFetchIndexRoute(final String routeId) {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("ftp://" + getRemoteQuarterDirectory() +
"?fileName=" + location.getFileName() +
"&binary=true" +
"&localWorkDirectory=" + localWorkDirectory)
.to("file://" + getLocalQuarterDirectory())
.process(new Processor() {
RouteTerminator terminator;
@Override
public void process(Exchange exchange) throws Exception {
if (camelBatchComplete(exchange)) {
terminator = new RouteTerminator(routeId,
exchange.getContext());
terminator.start();
}
}
})
.routeId(routeId);
}
};
}
我使用线程停止路线,这是Camel文档中推荐的方法 - How can I stop a route from a route
public class RouteTerminator extends Thread {
private String routeId;
private CamelContext camelContext;
public RouteTerminator(String routeId, CamelContext camelContext) {
this.routeId = routeId;
this.camelContext = camelContext;
}
@Override
public void run() {
try {
camelContext.stopRoute(routeId);
camelContext.removeRoute(routeId);
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
}
结果路线确实停止了。但我在 jconsole 中看到的是,未删除与路径对应的线程。因此,这些放弃的线程及时累积。
有没有办法动态/编程地正确停止/删除路由,还有释放路由的线程,以便它们不会随时间累积?
答案 0 :(得分:5)
这在下一个Camel版本2.9.2和2.10中得到修复。由此票证固定: https://issues.apache.org/jira/browse/CAMEL-5072