Spring boot 如何在Springboot应用程序中优雅地停止驼峰上下文
我用的是带弹簧靴的骆驼鞋。骆驼上下文在应用程序启动时启动,并保持运行。应用程序关闭时如何关闭camel上下文Spring boot 如何在Springboot应用程序中优雅地停止驼峰上下文,spring-boot,apache-camel,camel-ftp,Spring Boot,Apache Camel,Camel Ftp,我用的是带弹簧靴的骆驼鞋。骆驼上下文在应用程序启动时启动,并保持运行。应用程序关闭时如何关闭camel上下文 提前感谢。您可以使用CamelContext类停止方法 @Autowired CamelContext camelContext; stop()-关闭(将停止所有路由/组件/端点等并清除内部状态/缓存) 请参阅和我通过实现spring的SmartLifeCycle编写了一个自定义解决方案,该解决方案在关闭CamelContext之前等待其他spring bean停止。按原样使用这个类,
提前感谢。您可以使用CamelContext类停止方法
@Autowired CamelContext camelContext;
stop()-关闭(将停止所有路由/组件/端点等并清除内部状态/缓存)
请参阅和我通过实现spring的SmartLifeCycle编写了一个自定义解决方案,该解决方案在关闭CamelContext之前等待其他spring bean停止。按原样使用这个类,它会工作得很好
@Component
public class SpringBootCamelShutDown implements SmartLifecycle {
private static final Logger log = LoggerFactory.getLogger(SpringBootCamelShutDown.class);
@Autowired
private ApplicationContext appContext;
@Override
public void start() {}
@Override
public void stop() {}
@Override
public boolean isRunning() {
SpringCamelContext context = (SpringCamelContext)appContext.getBean(CamelContext.class);
return context.isStarted();
}
@Override
public boolean isAutoStartup() {
return true;
}
@Override
public void stop(Runnable runnable) {
SpringCamelContext context = (SpringCamelContext)appContext.getBean(CamelContext.class);
if (!isRunning()) {
log.info("Camel context already stopped");
return;
}
log.info("Stopping camel context. Will wait until it is actually stopped");
try {
context.stop();
} catch (Exception e) {
log.error("Error shutting down camel context",e) ;
return;
}
while(isRunning()) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
log.error("Error shutting down camel context",e) ;
}
};
// Calling this method is necessary to make sure spring has been notified on successful
// completion of stop method by reducing the latch countdown value.
runnable.run();
}
@Override
public int getPhase() {
return Integer.MAX_VALUE;
}
}
当您停止/关闭Spring Boot时,Camel将自动关闭。如果我是被问这个问题的人,我会将这个答案标记为已回答。顺便说一句,我们不需要“runnable.run();”什么时候已经停止了?