Apache camel 骆驼消费者在关机时仍会收到新消息
我使用的是Camel 2.16.1。关闭时,Camel的消费者仍然接受新消息。有没有办法迫使消费者立即停止消费。这里也有同样的问题: 我为这个问题创建了一个测试用例:Apache camel 骆驼消费者在关机时仍会收到新消息,apache-camel,Apache Camel,我使用的是Camel 2.16.1。关闭时,Camel的消费者仍然接受新消息。有没有办法迫使消费者立即停止消费。这里也有同样的问题: 我为这个问题创建了一个测试用例: public class GracefulShutdownTest extends CamelTestSupport { public class ThreadStopContext implements Runnable { private CamelContext context;
public class GracefulShutdownTest extends CamelTestSupport {
public class ThreadStopContext implements Runnable {
private CamelContext context;
public ThreadStopContext(CamelContext context) {
this.context = context;
}
@Override
public void run() {
try {
Thread.sleep(1000);
System.out.println("------start shutdown....");
context.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Sender implements Runnable {
private ProducerTemplate template;
public Sender(ProducerTemplate template) {
this.template = template;
}
@Override
public void run() {
template().sendBody("direct:start", "test");
}
}
@Test
public void shutdownTest() throws Exception {
context().setTracing(true);
getMockEndpoint("mock:endpoint").expectedMessageCount(0);
getMockEndpoint("mock:deadletterEndpoint").expectedMessageCount(1);
Thread thread = new Thread(new ThreadStopContext(context()));
thread.start();
Thread threadSender1 = new Thread(new Sender(template()));
threadSender1.start();
Thread.sleep(2000);
System.out.println("-------------------second message-----------------------------");
Thread threadSender2 = new Thread(new Sender(template()));
threadSender2.start();
assertMockEndpointsSatisfied();
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
errorHandler(deadLetterChannel("direct:deadletter")
.logHandled(true));
from("direct:start").process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
System.out.println("----------start sleep....");
Thread.sleep(5000);
System.out.println("----------end sleep....");
throw new Exception();
}
});
from("direct:deadletter")
.startupOrder(1)
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
System.out.println("----message at deadletter----");
}
})
.to("mock:deadletterEndpoint");
}
};
}
}
运行测试用例时,我们可以看到在开始正常关机后,机上交换的数量在增加:
Waiting as there are still 1 inflight and pending exchanges to complete
.....
Waiting as there are still 2 inflight and pending exchanges to complete
在阅读Defaultshutdownstrategy类的源代码后,我发现当关闭使用者时,Camel更喜欢挂起而不是关闭。这意味着,如果使用者支持suspend,它将执行suspend方法,然后将使用者推迟到稍后关闭列表。一些使用者(direct、RabbitMQ)有空的suspend()方法,然后他们仍然会收到新消息,如在延迟模式下在阅读DefaultShutdowsStrategy类的源代码后,我发现在关闭使用者时,Camel更喜欢挂起而不是关闭。这意味着,如果使用者支持suspend,它将执行suspend方法,然后将使用者推迟到稍后关闭列表。一些使用者(direct、RabbitMQ)有空的suspend()方法,然后他们仍然会在延迟模式下获得新消息是的,direct需要它,内部组件也是如此-外部组件(如file/jms)可以挂起。是的,它应该是直接的,就像内部组件一样——像file/jms这样的外部组件可以挂起。下一个版本中有兔子。