Search in sources :

Example 1 with YamlRoutesBuilderLoader

use of org.apache.camel.dsl.yaml.YamlRoutesBuilderLoader in project camel-k-runtime by apache.

the class CronTest method testCronTimerActivation.

@ParameterizedTest
@MethodSource("parameters")
public void testCronTimerActivation(String code, String cronOverride) throws Exception {
    final Runtime runtime = Runtime.on(new DefaultCamelContext());
    runtime.getRegistry().bind("__camel_k_resolver", new CustomResolver());
    final YamlRoutesBuilderLoader loader = new YamlRoutesBuilderLoader();
    loader.setCamelContext(runtime.getCamelContext());
    loader.start();
    final CronSourceLoaderInterceptor interceptor = new CronSourceLoaderInterceptor();
    interceptor.setRuntime(runtime);
    interceptor.setOverridableComponents(cronOverride);
    final RouteBuilder builder = (RouteBuilder) loader.loadRoutesBuilder(ResourceHelper.fromBytes("my-cron.yaml", code.getBytes(StandardCharsets.UTF_8)));
    builder.addLifecycleInterceptor(interceptor);
    runtime.getCamelContext().addRoutes(builder);
    CountDownLatch termination = new CountDownLatch(1);
    runtime.getCamelContext().addLifecycleStrategy(new LifecycleStrategySupport() {

        @Override
        public void onContextStopped(CamelContext context) {
            termination.countDown();
        }
    });
    MockEndpoint mock = runtime.getCamelContext().getEndpoint("mock:result", MockEndpoint.class);
    mock.expectedMessageCount(1);
    mock.setResultWaitTime(10000);
    runtime.getCamelContext().start();
    mock.assertIsSatisfied();
    termination.await(10, TimeUnit.SECONDS);
    assertThat(termination.getCount()).isZero();
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Runtime(org.apache.camel.k.Runtime) RouteBuilder(org.apache.camel.builder.RouteBuilder) LifecycleStrategySupport(org.apache.camel.support.LifecycleStrategySupport) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) YamlRoutesBuilderLoader(org.apache.camel.dsl.yaml.YamlRoutesBuilderLoader) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CustomResolver(org.apache.camel.dsl.yaml.deserializers.CustomResolver) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with YamlRoutesBuilderLoader

use of org.apache.camel.dsl.yaml.YamlRoutesBuilderLoader in project camel-k-runtime by apache.

the class Application method load.

@GET
@Path("/load")
@Produces(MediaType.TEXT_PLAIN)
public String load() throws Exception {
    final String code = "" + "\n- from:" + "\n    uri: \"timer:tick?period=1&delay=60000\"" + "\n    steps:" + "\n      - log: \"${body}\"";
    final YamlRoutesBuilderLoader loader = new YamlRoutesBuilderLoader();
    loader.setCamelContext(context);
    loader.build();
    final Runtime rt = new DelegatingRuntime(runtime) {

        @Override
        public void stop() throws Exception {
            stopped.set(true);
        }
    };
    final CronSourceLoaderInterceptor interceptor = new CronSourceLoaderInterceptor();
    interceptor.setRuntime(rt);
    interceptor.setOverridableComponents("timer");
    final RouteBuilder builder = (RouteBuilder) loader.loadRoutesBuilder(ResourceHelper.fromBytes("my-cron.yaml", code.getBytes(StandardCharsets.UTF_8)));
    builder.addLifecycleInterceptor(interceptor);
    try {
        context.addRoutes(builder);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return "" + context.getRoutesSize();
}
Also used : DelegatingRuntime(org.apache.camel.k.support.DelegatingRuntime) Runtime(org.apache.camel.k.Runtime) RouteBuilder(org.apache.camel.builder.RouteBuilder) DelegatingRuntime(org.apache.camel.k.support.DelegatingRuntime) CronSourceLoaderInterceptor(org.apache.camel.k.cron.CronSourceLoaderInterceptor) YamlRoutesBuilderLoader(org.apache.camel.dsl.yaml.YamlRoutesBuilderLoader) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

RouteBuilder (org.apache.camel.builder.RouteBuilder)2 YamlRoutesBuilderLoader (org.apache.camel.dsl.yaml.YamlRoutesBuilderLoader)2 Runtime (org.apache.camel.k.Runtime)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 CamelContext (org.apache.camel.CamelContext)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 CustomResolver (org.apache.camel.dsl.yaml.deserializers.CustomResolver)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 CronSourceLoaderInterceptor (org.apache.camel.k.cron.CronSourceLoaderInterceptor)1 DelegatingRuntime (org.apache.camel.k.support.DelegatingRuntime)1 LifecycleStrategySupport (org.apache.camel.support.LifecycleStrategySupport)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1