为了创建一个从 ActiveMQ 队列中使用的 Camel 应用程序,我按照本教程编写了一个独立的 Camel 应用程序: http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html
区别在于使用了来自camel-spring的Main类而不是camel-core:
我的主课:
try {
Main main = new Main();
main.setApplicationContextUri("conf/spring-context.xml");
main.run();
} catch (Exception ex) {
LOGGER.error(String.format(DifClientConstants.ERROR_START_CLIENT, ex.getMessage()), ex);
}
文件 spring-context.xml 中的camelContext:
...
<!-- Camel context configuration -->
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="myRoutes" />
</camelContext>
...
我通过一个脚本启动这个应用程序,该脚本支持一些命令,例如:启动、停止、重新启动。 对于停止命令,我终止应用程序的进程。 问题是应用程序与 ActiveMQ 的连接似乎没有关闭:
WARN | Transport Connection to: tcp://172.16.x.x:58363 failed: java.net.SocketException: Connection reset | org.apache.activemq.broker.TransportConnection.Transport | ActiveMQ Transport: tcp:///172.16.x.x:58363@61616
WARN | Transport Connection to: tcp://172.16.x.x:58325 failed: java.io.EOFException | org.apache.activemq.broker.TransportConnection.Transport | ActiveMQ Transport: tcp:///172.16.x.x:58325@61616
所以问题是,如何强制Camel应用程序在通过进程终止关闭时关闭所有连接?
最佳答案
Camel 提供优雅的关闭功能来停止所有消耗路由。此策略将允许传输中的消息完成。
您可以创建一条路线来停止 Camel 上下文或暂停它。例如:
@Override
public void process(Exchange exchange) throws Exception {
exchange.getContext().stop();
}
@Override
public void process(Exchange exchange) throws Exception {
exchange.getContext().suspend();
}
您还可以创建一条路线来启动或恢复您的 Camel 上下文。
@Override
public void process(Exchange exchange) throws Exception {
exchange.getContext().start();
}
@Override
public void process(Exchange exchange) throws Exception {
exchange.getContext().resume();
}
关于java - 如何正确阻止camelContext在独立应用程序中被杀死,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42089949/