gpt4 book ai didi

spring-boot - Apache Camel Spring Boot - 处理路由后应用程序的正常关闭

转载 作者:行者123 更新时间:2023-12-04 10:29:17 25 4
gpt4 key购买 nike

我的 Spring Boot 应用程序中有几条路线(路线 1 和路线 2)。我一直在研究如何在处理完两条路线后优雅地关闭应用程序。我已经引用了文档 ( https://camel.apache.org/manual/latest/graceful-shutdown.html ) 但无法成功实现我所需要的。可能我的理解有误。

下面是我的两条路线

路线 1

 from("timer://runOnce?repeatCount=1")
.to("{{sql.selectAll}}")
......... SOME PROCESSING
.to("direct:checkStatus")

路线 2

from("direct:checkStatus")
.delay(5000)
.loopDoWhile(CONDITION)
.process(DO_SOMETHING)
.end()
.to("jpa:com.pqr.MyClass)
.stop();

我已经尝试了所有这些选项

<强>1。 60 秒后自动关机

camel.springboot.duration-max-seconds = 60

它会优雅地关闭 2 条路由,但随后会发出关于强制关闭 ExecutorsService 的警告,并且它不会停止主线程来停止应用程序。

2020-03-01 18:28:25.507  WARN 30279 --- [otTerminateTask] o.a.c.i.e.BaseExecutorServiceManager     : Forcing shutdown of ExecutorService: org.apache.camel.util.concurrent.SizedScheduledExecutorService@17fbfb02[CamelSpringBootTerminateTask] due first await termination elapsed.
2020-03-01 18:28:25.507 WARN 30279 --- [otTerminateTask] o.a.c.i.e.BaseExecutorServiceManager : Forcing shutdown of ExecutorService: org.apache.camel.util.concurrent.SizedScheduledExecutorService@17fbfb02[CamelSpringBootTerminateTask] due interrupted.
2020-03-01 18:28:25.508 INFO 30279 --- [otTerminateTask] o.a.c.i.e.BaseExecutorServiceManager : Shutdown of ExecutorService: org.apache.camel.util.concurrent.SizedScheduledExecutorService@17fbfb02[CamelSpringBootTerminateTask] is shutdown: true and terminated: false took: 10.004 seconds.
2020-03-01 18:28:25.508 WARN 30279 --- [otTerminateTask] o.a.c.i.e.BaseExecutorServiceManager : Forced shutdown of 1 ExecutorService's which has not been shutdown properly (acting as fail-safe)
2020-03-01 18:28:25.508 WARN 30279 --- [otTerminateTask] o.a.c.i.e.BaseExecutorServiceManager : forced -> org.apache.camel.util.concurrent.SizedScheduledExecutorService@17fbfb02[CamelSpringBootTerminateTask]

<强>2。从 Route2 发起关机

    from("direct:checkStatus")
.delay(5000)
.loopDoWhile(CONDITION)
.process(DO_SOMETHING)
.end()
.to("jpa:com.pqr.MyClass)
.process(exchange -> {
exchange.getContext().getRouteController().stopRoute("route1");
exchange.getContext().getRouteController().stopRoute("route2");
System.out.println("Route1 -->"+exchange.getContext().getRouteController().getRouteStatus("route1"));
System.out.println("Route2 -->"+exchange.getContext().getRouteController().getRouteStatus("route2"));
exchange.getContext().shutdown();
});

“route1”已正常停止,但“route2”无法正常停止并显示以下消息并等待默认超时(300 秒)。

2020-03-01 18:35:29.113  INFO 30504 --- [read #4 - Delay] o.a.c.i.engine.DefaultShutdownStrategy   : Starting to graceful shutdown 1 routes (timeout 300 seconds)
2020-03-01 18:35:29.116 INFO 30504 --- [ - ShutdownTask] o.a.c.i.engine.DefaultShutdownStrategy : Route: route1 shutdown complete, was consuming from: timer://runOnce?repeatCount=1
2020-03-01 18:35:29.116 INFO 30504 --- [read #4 - Delay] o.a.c.i.engine.DefaultShutdownStrategy : Graceful shutdown of 1 routes completed in 0 seconds
2020-03-01 18:35:29.117 INFO 30504 --- [read #4 - Delay] o.a.c.s.boot.SpringBootCamelContext : Route: route1 is stopped, was consuming from: timer://runOnce?repeatCount=1
2020-03-01 18:35:29.117 INFO 30504 --- [read #4 - Delay] o.a.c.i.engine.DefaultShutdownStrategy : Starting to graceful shutdown 1 routes (timeout 300 seconds)
2020-03-01 18:35:29.118 INFO 30504 --- [ - ShutdownTask] o.a.c.i.engine.DefaultShutdownStrategy : Waiting as there are still 1 inflight and pending exchanges to complete, timeout in 300 seconds. Inflights per route: [route2 = 1]

看起来有待消费的待处理交换消息。我是否需要手动清除/使用交换消息以清除和促进正常关机?

任一选项都不会停止主应用程序。我是否必须编写自定义关机策略而不是 DefaultShutdownStrategy 才能实现此目的?有人可以指出一个在路由完成后关闭 Spring Boot 应用程序的例子吗?提前致谢!!!

最佳答案

您是否尝试使用 exchange.getContext().stop() 停止主应用程序?

要在不等待默认超时的情况下强制停止路由,您可以使用 exchange.getContext().stopRoute(routeId, 1L, TimeUnit.SECONDS); 或以秒为单位设置超时 context。 getShutdownStrategy().setTimeout(30);

关于spring-boot - Apache Camel Spring Boot - 处理路由后应用程序的正常关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60481228/

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