Java apachecamel:Camel 17和Camel 18/19中的异步回调回复线程更改
对于高达17.x的Camel版本,通过Java apachecamel:Camel 17和Camel 18/19中的异步回调回复线程更改,java,concurrency,apache-camel,Java,Concurrency,Apache Camel,对于高达17.x的Camel版本,通过activemq:queue对Synchronization.onComplete()回调的所有调用都是在不同的线程中完成的,因此,即使onComplete中的响应处理非常慢,消息也不会被阻塞和排队。据我所知,这应该是asyncConsumer=true&defaultTaskExecutorType=ThreadPool&concurrentConsumers=2&maxConcurrentConsumers=100配置的结果。 因此,此showcase的
activemq:queue
对Synchronization.onComplete()
回调的所有调用都是在不同的线程中完成的,因此,即使onComplete中的响应处理非常慢,消息也不会被阻塞和排队。据我所知,这应该是asyncConsumer=true&defaultTaskExecutorType=ThreadPool&concurrentConsumers=2&maxConcurrentConsumers=100
配置的结果。
因此,此showcase的示例输出为:
Received async reply: 2000 OK
Received async reply: 3000 OK
Received async reply: 5000 OK
Received async reply: 9000 OK
Received async reply: 10000 OK
Finished async reply: 2000 OK
Finished async reply: 3000 OK
Finished async reply: 5000 OK
Finished async reply: 9000 OK
Finished async reply: 10000 OK
因此,“完成的”日志毕竟是“收到的”,因为每个日志都在不同的线程中调用。所有回复都是异步接收的,处理一个回复的时间不会影响其他回复的接收
升级到Camel 18.x(或19.x)后,情况就不一样了。现在收到回复和处理(漫长的过程)会阻止其他人的接收。这是因为使用同一个线程调用Synchronization.onComplete(),因此答复将排队等待,直到完全处理
Received async reply: 2000 OK
Received async reply: 10000 OK
Received async reply: 9000 OK
Finished async reply: 2000 OK
Received async reply: 3000 OK
Finished async reply: 3000 OK
Finished async reply: 9000 OK
Finished async reply: 10000 OK
Received async reply: 5000 OK
Finished async reply: 5000 OK
我认为这些新属性的配置正好是这样的:replyToConcurrentConsumers=2&replyToMaxConcurrentConsumers=100,如果出现新消息,如果当前所有线程都在处理,则会在新线程中处理和回复该消息(当然,如果未达到最大计数,则线程通常会处理多条消息并将其排队)
也许我做错了什么,但是如何配置路由以获得与Camel 17类似的结果。增加replyToConcurrentConsumers属性它可以工作,但我认为它应该动态缩放,并在需要时依赖replyToMaxConcurrentConsumers
守则:
public class AsyncCallbackTest {
private static final Logger LOG = LoggerFactory.getLogger(AsyncCallbackTest.class);
public static void main(String[] args) throws Exception {
// create and setup a default Camel context
final CamelContext camelContext = new DefaultCamelContext();
setupRoutes(camelContext);
camelContext.start();
// real test
asyncCallbackTest(camelContext);
camelContext.stop();
System.exit(0);
}
private static void setupRoutes(CamelContext camelContext) throws Exception {
camelContext.addRoutes(new RouteBuilder() {
public void configure() {
from("activemq://queue:asyncTest?asyncConsumer=true&defaultTaskExecutorType=ThreadPool" +
"&concurrentConsumers=2&maxConcurrentConsumers=100")
.process(exchange -> {
final String msg = String.valueOf(exchange.getIn().getBody());
exchange.getOut().setBody(msg + " OK");
});
}
});
}
private static void asyncCallbackTest(CamelContext camelContext) throws Exception {
final int[] delays = new int[]{9000, 10000, 5000, 2000, 3000};
final CountDownLatch countDownLatch = new CountDownLatch(delays.length);
final Synchronization callback = new SynchronizationAdapter() {
@Override
public void onComplete(Exchange exchange) {
LOG.info("Received async reply: " + exchange.getOut().getBody());
final int delay = (int) exchange.getIn().getBody();
synchronized (this) {
try {
this.wait(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
LOG.info("Finished async reply: " + exchange.getOut().getBody());
super.onComplete(exchange);
}
@Override
public void onDone(Exchange exchange) {
countDownLatch.countDown();
}
};
final ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
for (int i = 0; i < delays.length; i++) {
final Exchange exchange = new DefaultExchange(camelContext);
exchange.getIn().setBody(delays[i]);
exchange.setPattern(ExchangePattern.InOut);
producerTemplate.asyncCallback("activemq://queue:asyncTest?" +
"replyToConcurrentConsumers=2&replyToMaxConcurrentConsumers=100",
exchange, callback);
}
countDownLatch.await();
camelContext.stop();
}
public类异步回调测试{
私有静态最终记录器LOG=LoggerFactory.getLogger(AsyncCallbackTest.class);
公共静态void main(字符串[]args)引发异常{
//创建并设置默认的驼峰上下文
final CamelContext CamelContext=新的DefaultCamelContext();
设置路径(上下文);
camelContext.start();
//真实测试
异步回调测试(camelContext);
camelContext.stop();
系统出口(0);
}
私有静态void setupRoutes(CamelContext CamelContext)引发异常{
camelContext.addRoutes(新RouteBuilder(){
public void configure(){
从(”activemq://queue:asyncTest?asyncConsumer=true&defaultTaskExecutorType=ThreadPool" +
“&concurrentConsumers=2&maxConcurrentConsumers=100”)
.流程(交换->{
最后一个字符串msg=String.valueOf(exchange.getIn().getBody());
exchange.getOut().setBody(msg+“OK”);
});
}
});
}
私有静态void asyncCallbackTest(CamelContext CamelContext)引发异常{
最终整数[]延迟=新整数[]{9000,10000,5000,2000,3000};
最终倒计时闩锁=新倒计时闩锁(延迟.长度);
最终同步回调=新的SynchronizationAdapter(){
@凌驾
未完成的公共作废(交换){
LOG.info(“收到异步回复:”+exchange.getOut().getBody());
final int delay=(int)exchange.getIn().getBody();
已同步(此){
试一试{
这个。等待(延迟);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
LOG.info(“完成异步回复:”+exchange.getOut().getBody());
super.onComplete(交换);
}
@凌驾
公用电话(交换){
countdownlock.countDown();
}
};
final ProducerTemplate ProducerTemplate=camelContext.createProducerTemplate();
对于(int i=0;i
}