How can I shutdown a standalone Apache Camel application in an automated way?

前端 未结 3 876
情话喂你
情话喂你 2021-01-03 01:59

I\'m trying to use Apache Camel to download and route files from an FTP server. However, files are only added to the FTP server once in a long while so having the program r

相关标签:
3条回答
  • 2021-01-03 02:45

    See this FAQ how to stop a route from a route: http://camel.apache.org/how-can-i-stop-a-route-from-a-route.html.

    Then you can enable the option: sendEmptyMessageWhenIdle=true, and then in the route do a message filter, or content based route, and detect the empty message, and then stop the route and then after that CamelContext.

    Though I also think this question has been discussed before, so you can maybe find other SO questions or google etc. As there is also alternative ways of doing this.

    0 讨论(0)
  • 2021-01-03 02:48

    Completing Claus answer, this code run in a only once fashioned way Main:

    import org.apache.camel.builder.RouteBuilder;
    import org.apache.camel.main.Main;
    
    public class MyMainRouter extends RouteBuilder {
    
      static Main main;
    
      @Override
      public void configure() throws Exception {
        from("timer:foo?delay=5s")
            .log("Hello camel, main world after 5 seconds!")
            .process(processor -> main.completed());
      }
    
      public static void main(String[] args) throws Exception {
        main = new Main();
        main.addRouteBuilder(new MyMainRouter());
        main.run();
      }
    
    }
    

    After 5 seconds, the code will run only once, because we will call a processor that will call completed() method, wich internally have CountDownLatch stopping a route pattern from another thread.

    0 讨论(0)
  • 2021-01-03 03:04

    Adding this example that may be useful to others without digging all the examples in the link.

    Define a bean/processor that will launch a separate thread. This new thread will call stop() on the active CamelContext.

    public class ShutdownBean {
    
        private final static Logger log = LoggerFactory.getLogger(ShutdownBean.class);
    
        public void process(Exchange exchange) throws Exception {
            final CamelContext camelContext = exchange.getContext();
    
            Thread shutdownThread = new Thread(() -> {
                Thread.currentThread().setName("ShutdownThread");
                try {
                    camelContext.stop();
                } catch (Exception e) {
                    log.error("Errore during shutdown", e);
                }
            });
    
            shutdownThread.start();
        }
    }
    

    In your application context define this route and call it when you need to shutdown Camel.

    <bean id="shutdownBean"
          class="your.package.ShutdownBean" />
    
    <camelContext>
    
        <route id="ShutdownRoute">
            <from uri="direct:shutdown" />
            <log message="Shutdown..." />
            <to uri="bean:shutdownBean" />
        </route>
    
    </camelContext>
    

    Note: enableHangupSupport() is deprecated on newer Camel versions: is enabled by default now, so no longer need to call this method.

    0 讨论(0)
提交回复
热议问题