org.apache.camel.support.LifecycleStrategySupport Java Examples

The following examples show how to use org.apache.camel.support.LifecycleStrategySupport. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CamelSpringBootApplicationListener.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
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 onContextStop(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();
            }
        }
    });
}
 
Example #2
Source File: CamelSpringBootApplicationListener.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
private void terminateApplicationContext(final ConfigurableApplicationContext applicationContext, final CamelContext camelContext, int seconds) {
    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.");
            // we need to run a daemon thread to stop ourselves so this thread pool can be stopped nice also
            new Thread(applicationContext::close).start();
            running.set(false);
        };
        new Thread(stop, "CamelSpringBootTerminateTaskWorker").start();
    };

    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
            if (!running.get()) {
                future.cancel(true);
            }
        }
    });
}
 
Example #3
Source File: CamelSpringBootApplicationListener.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
private void terminateApplicationContext(final ConfigurableApplicationContext applicationContext, final CamelContext camelContext,
                                         final MainShutdownStrategy shutdownStrategy) {
    ExecutorService executorService = camelContext.getExecutorServiceManager().newSingleThreadExecutor(this, "CamelSpringBootTerminateTask");

    final AtomicBoolean running = new AtomicBoolean();
    Runnable task = () -> {
        try {
            shutdownStrategy.await();
            // only mark as running after the latch
            running.set(true);
            LOG.info("CamelSpringBoot triggering shutdown of the JVM.");
            // we need to run a daemon thread to stop ourselves so this thread pool can be stopped nice also
            new Thread(applicationContext::close).start();
        } catch (Throwable e) {
            // ignore
        }
        running.set(false);
    };

    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
            if (!running.get()) {
                future.cancel(true);
            } else {
                // trigger shutdown
                shutdownStrategy.shutdown();
            }
        }
    });
}
 
Example #4
Source File: CronTest.java    From camel-k-runtime with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@MethodSource("parameters")
public void testCronTimerActivation(String routes, String cronOverride) throws Exception {
    ApplicationRuntime runtime = new ApplicationRuntime();
    runtime.setProperties(
        "loader.interceptor.cron.overridable-components", cronOverride
    );
    runtime.addListener(RoutesConfigurer.forRoutes(routes));

    // To check auto-termination of Camel context
    CountDownLatch termination = new CountDownLatch(1);
    runtime.getCamelContext().addLifecycleStrategy(new LifecycleStrategySupport() {
        @Override
        public void onContextStop(CamelContext context) {
            termination.countDown();
        }
    });

    MockEndpoint mock = runtime.getCamelContext().getEndpoint("mock:result", MockEndpoint.class);
    mock.expectedMessageCount(1);
    mock.setResultWaitTime(10000);

    runtime.run();
    mock.assertIsSatisfied();

    termination.await(10, TimeUnit.SECONDS);
    assertThat(termination.getCount()).isEqualTo(0);
}
 
Example #5
Source File: JavaScriptSourceLoader.java    From camel-k-runtime with Apache License 2.0 5 votes vote down vote up
@Override
public Result load(Runtime runtime, Source source) throws Exception {
    RoutesBuilder builder = new EndpointRouteBuilder() {
        @Override
        public void configure() throws Exception {
            final Context context = Context.newBuilder("js").allowAllAccess(true).build();

            try (InputStream is = source.resolveAsInputStream(getContext())) {
                Value bindings = context.getBindings(LANGUAGE_ID);

                // configure bindings
                bindings.putMember("__dsl", new IntegrationConfiguration(this));

                final String script = new String(is.readAllBytes(), StandardCharsets.UTF_8);
                final String wrappedScript = "with (__dsl) { " + script + " }";

                context.eval(LANGUAGE_ID, wrappedScript);

                //
                // Close the polyglot context when the camel context stops
                //
                getContext().addLifecycleStrategy(new LifecycleStrategySupport() {
                    @Override
                    public void onContextStop(CamelContext camelContext) {
                        context.close(true);
                    }
                });
            }
        }
    };

    return Result.on(builder);
}
 
Example #6
Source File: IgniteCamelStreamerTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception
 */
@Test
public void testUserSpecifiedCamelContext() throws Exception {
    final AtomicInteger cnt = new AtomicInteger();

    // Create a CamelContext with a probe that'll help us know if it has been used.
    CamelContext context = new DefaultCamelContext();
    context.setTracing(true);
    context.addLifecycleStrategy(new LifecycleStrategySupport() {
        @Override public void onEndpointAdd(Endpoint endpoint) {
            cnt.incrementAndGet();
        }
    });

    streamer.setSingleTupleExtractor(singleTupleExtractor());
    streamer.setCamelContext(context);

    // Subscribe to cache PUT events.
    CountDownLatch latch = subscribeToPutEvents(50);

    // Action time.
    streamer.start();

    // Send messages.
    sendMessages(0, 50, false);

    // Assertions.
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertCacheEntriesLoaded(50);
    assertTrue(cnt.get() > 0);
}