private void terminateMainControllerAfter()

in core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationListener.java [239:275]


    private void terminateMainControllerAfter(final CamelContext camelContext, int seconds,
                                              final MainShutdownStrategy shutdownStrategy, final Runnable mainCompletedTask) {
        ScheduledExecutorService executorService = camelContext.getExecutorServiceManager().newSingleThreadScheduledExecutor(this, "CamelSpringBootTerminateTask");

        final AtomicBoolean running = new AtomicBoolean();
        Runnable task = () -> {
            // need to spin up as separate thread so we can terminate this thread pool without problems
            Runnable stop = () -> {
                running.set(true);
                LOG.info("CamelSpringBoot triggering shutdown of the JVM.");
                try {
                    camelContext.stop();
                } catch (Throwable e) {
                    LOG.warn("Error during stopping CamelContext", e);
                } finally {
                    shutdownStrategy.shutdown();
                    mainCompletedTask.run();
                }
                running.set(false);
            };
            new Thread(stop, "CamelSpringBootTerminateTaskWorker").start();
        };

        final ScheduledFuture<?> future = executorService.schedule(task, seconds, TimeUnit.SECONDS);
        camelContext.addLifecycleStrategy(new LifecycleStrategySupport() {
            @Override
            public void onContextStopping(CamelContext context) {
                // we are stopping then cancel the task so we can shutdown quicker
                if (!running.get()) {
                    future.cancel(true);
                    // trigger shutdown
                    shutdownStrategy.shutdown();
                    mainCompletedTask.run();
                }
            }
        });
    }