You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/03/16 12:58:05 UTC

[camel-spring-boot] 02/03: CAMEL-14642: camel-spring-boot - Using max duration and shutdown before should terminate scheduled thread quicker

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git

commit 0881d6716809bc967fff1d2b438437a90a2bbbb1
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Mar 16 13:38:29 2020 +0100

    CAMEL-14642: camel-spring-boot - Using max duration and shutdown before should terminate scheduled thread quicker
---
 .../boot/CamelSpringBootApplicationListener.java   | 34 ++++++++++++++++++++--
 1 file changed, 31 insertions(+), 3 deletions(-)

diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationListener.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationListener.java
index 58ace8a..9942705 100644
--- a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationListener.java
+++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationListener.java
@@ -20,19 +20,23 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
 import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.StartupListener;
 import org.apache.camel.main.MainDurationEventNotifier;
+import org.apache.camel.main.MainLifecycleStrategy;
 import org.apache.camel.main.RoutesCollector;
 import org.apache.camel.main.RoutesConfigurer;
 import org.apache.camel.spi.CamelEvent;
 import org.apache.camel.spi.CamelEvent.Type;
 import org.apache.camel.spi.EventNotifier;
 import org.apache.camel.support.EventNotifierSupport;
+import org.apache.camel.support.LifecycleStrategySupport;
 import org.apache.camel.support.service.ServiceHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -227,7 +231,15 @@ public class CamelSpringBootApplicationListener implements ApplicationListener<C
                 latch.countDown();
             }
         };
-        executorService.schedule(task, seconds, TimeUnit.SECONDS);
+
+        final ScheduledFuture future = executorService.schedule(task, seconds, TimeUnit.SECONDS);
+        camelContext.addLifecycleStrategy(new LifecycleStrategySupport() {
+            @Override
+            public void onContextStop(CamelContext context) {
+                // we are stopping then cancel the task so we can shutdown quicker
+                future.cancel(true);
+            }
+        });
     }
 
     private void terminateApplicationContext(final ConfigurableApplicationContext applicationContext, final CamelContext camelContext, int seconds) {
@@ -237,7 +249,15 @@ public class CamelSpringBootApplicationListener implements ApplicationListener<C
             // we need to run a daemon thread to stop ourselves so this thread pool can be stopped nice also
             new Thread(applicationContext::close).start();
         };
-        executorService.schedule(task, seconds, TimeUnit.SECONDS);
+
+        final ScheduledFuture future = executorService.schedule(task, seconds, TimeUnit.SECONDS);
+        camelContext.addLifecycleStrategy(new LifecycleStrategySupport() {
+            @Override
+            public void onContextStop(CamelContext context) {
+                // we are stopping then cancel the task so we can shutdown quicker
+                future.cancel(true);
+            }
+        });
     }
 
     private void terminateApplicationContext(final ConfigurableApplicationContext applicationContext, final CamelContext camelContext, final CountDownLatch latch) {
@@ -252,7 +272,15 @@ public class CamelSpringBootApplicationListener implements ApplicationListener<C
                 // ignore
             }
         };
-        executorService.submit(task);
+
+        final Future future = executorService.submit(task);
+        camelContext.addLifecycleStrategy(new LifecycleStrategySupport() {
+            @Override
+            public void onContextStop(CamelContext context) {
+                // we are stopping then cancel the task so we can shutdown quicker
+                future.cancel(true);
+            }
+        });
     }
 
 }