gpt4 book ai didi

spring-boot - 如何在 Spring Boot 应用程序中优雅地停止 Camel 上下文

转载 作者:行者123 更新时间:2023-12-02 20:17:46 25 4
gpt4 key购买 nike

我正在使用 Camel 和 Spring Boot。 Camel 上下文在应用程序启动时启动并保持运行。在应用程序关闭时如何关闭 Camel 上下文。

提前致谢。

最佳答案

我通过实现 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 - 如何在 Spring Boot 应用程序中优雅地停止 Camel 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52055404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com