标签:camel esb async processor asyncprocessor
Camel支持一种更复杂的异步的处理模型,异步处理器实现一个继承自Processor接口的AsyncProcessor接口,使用异步Processor的优点:至于是否是进行异步处理依赖于Camel路由的配置.
以上是Camel官方对异步Processor的解释,下面是本人用于测试的一个例子:
public static void main(String[] args) throws Exception {
RouteBuilder builder = new RouteBuilder() {
@Override
public void configure() throws Exception {
RouteDefinition definition1 = this.from("file:H:/temp/in");
RouteDefinition definition2 = definition1.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
System.out.println(Thread.currentThread().getName());
System.out.println("process1");
}
}).process(new AsyncProcessor() {
@Override
public void process(Exchange exchange) throws Exception {
System.out.println("process");
}
@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
System.out.println(Thread.currentThread().getName());
System.out.println("async process");
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
callback.done(false);
return false;
}
}).process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
System.out.println(Thread.currentThread().getName());
System.out.println("process2");
}
});
definition2.to("file:H:/temp/out");
}
};
DefaultCamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(builder);
camelContext.start();
Object object = new Object();
synchronized (object) {
object.wait();
}
}
getAsyncProcessor().process(exchange, new AsyncCallback() {
public void done(boolean doneSync) {
// noop
if (log.isTraceEnabled()) {
log.trace("Done processing file: {} {}", target, doneSync ? "synchronously" : "asynchronously");
}
}
});标签:camel esb async processor asyncprocessor
原文地址:http://blog.csdn.net/xtayfjpk/article/details/40463609