How to properly stop camelContext from being kille

2019-06-09 21:10发布

问题:

To create a Camel application which consumes from ActiveMQ's queue, I wrote a standalone Camel application following this tutorial: http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html

The difference was The Main class from camel-spring used instead of camel-core:

My Main class:

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);
}

My camelContext in file spring-context.xml:

...

    <!-- Camel context configuration -->
    <camelContext xmlns="http://camel.apache.org/schema/spring">
        <routeBuilder ref="myRoutes" />
    </camelContext>
...

I start this application by a script which supports some commands such as: start, stop, restart. For the stop command, I kill application's process. Problem is application's connection to ActiveMQ seems not to be closed:

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

So the question is, how can I force Camel application to close all its connection when shutdown by process killing?

回答1:

Camel provide a graceful shutdown fonctionality to stop all consumings routes. This strategy will allow in-flight messages to complete.

You can create a route to stop camel context or suspend it. For example :

@Override
public void process(Exchange exchange) throws Exception {
    exchange.getContext().stop(); 
}


@Override
public void process(Exchange exchange) throws Exception {
    exchange.getContext().suspend(); 
}

And you can also create a route to start or resume your camel context.

@Override
public void process(Exchange exchange) throws Exception {
    exchange.getContext().start(); 
}

@Override
public void process(Exchange exchange) throws Exception {
    exchange.getContext().resume(); 
}

Here is the link to the official documentation : http://camel.apache.org/graceful-shutdown.html



回答2:

Following Strelok's comment, I checked and found that the process was killed by kill -9 (SIGKILL). I used only kill and the camel graceful shutdown executed, also the WARN gone. @Strlok Thank you