Hi folks,
I'm working lately on some experiment to run a generic Camel application on
a Kubernetes CronJob. The behavior expected by the cluster is that, once
the job workload is over, the application would exit with either a success
or error code. As we're running a Camel application, although the route may
have finished, the entire application will continue to run until a forceful
shutdown. I am not sure it exists any other way, so I found my way to
workaround this behavior by following the approach in [1], which would ends
up in something like the following code:

public class TestCron extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("timer:cron?delay=0&period=1&repeatCount=1")
        // Simulate heavylift work
        .delay(10000)
        .to("log:info")
        .log("DONE!")
        .process(new Processor() {
            Thread stop;
            @Override
            public void process(final Exchange exchange) throws Exception {
                if (stop == null) {
                    stop = new Thread() {
                        @Override
                        public void run() {
                            try {
                                exchange.getContext().stop();
                            } catch (Exception e) {
                                // ignore
                            }
                        }
                    };
                }
                // start the thread that stops the context
                stop.start();
            }
        });
    }
}

However, this workaround feels a bit too hacky as it involves playing with
Camel Context directly. It is also impossible to reproduce easily in non
Java DSL, like in YAML. I am wondering if we can think on having a higher
level EIP such we have with "stop", something like "shutdown" which
behavior may be similar of what described above, taking care to gracefully
shutdown the application when it is invoked. This one may do some finer
grained controls to verify that the exchanges has really completed or there
are no others routes running or any other low level check that would
perform a controlled shutdown. How does it sound? or which would be any
alternative to make Camel more cloud cron "friendly"?

Regards,
Pasquale.

[1]
https://camel.apache.org/manual/faq/how-can-i-stop-a-route-from-a-route.html#HowcanIstoparoutefromaroute-Usingathreadtostoparoutefromaroute

Reply via email to