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

前端 未结 3 874
情话喂你
情话喂你 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 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.

    
    
    
    
        
            
            
            
        
    
    
    

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

提交回复
热议问题