Search in sources :

Example 6 with AdviceWithRouteBuilder

use of org.apache.camel.builder.AdviceWithRouteBuilder in project camel by apache.

the class AdviceWithIssueTest method testAdviceWithErrorHandler.

public void testAdviceWithErrorHandler() throws Exception {
    RouteDefinition route = context.getRouteDefinitions().get(0);
    try {
        route.adviceWith(context, new AdviceWithRouteBuilder() {

            @Override
            public void configure() throws Exception {
                errorHandler(deadLetterChannel("mock:dead"));
            }
        });
        fail("Should have thrown exception");
    } catch (IllegalArgumentException e) {
        assertEquals("You can not advice with error handlers. Remove the error handlers from the route builder.", e.getMessage());
    }
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder)

Example 7 with AdviceWithRouteBuilder

use of org.apache.camel.builder.AdviceWithRouteBuilder in project camel by apache.

the class AdviceWithOnExceptionMultipleIssueTest method testMultipleAdvice.

public void testMultipleAdvice() throws Exception {
    context.addRoutes(createRouteBuilder());
    context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("mock:resultA").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    throw new Exception("my exception");
                }
            });
        }
    });
    context.getRouteDefinition("RouteB").adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
        }
    });
    context.start();
    getMockEndpoint("mock:resultA").expectedMessageCount(0);
    template.sendBody("direct:startA", "a trigger");
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder)

Example 8 with AdviceWithRouteBuilder

use of org.apache.camel.builder.AdviceWithRouteBuilder in project camel by apache.

the class AdviceWithOnExceptionMultipleIssueTest method testSimpleMultipleAdvice.

public void testSimpleMultipleAdvice() throws Exception {
    context.addRoutes(createRouteBuilder());
    context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("mock:resultA").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                }
            });
        }
    });
    context.getRouteDefinition("RouteB").adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
        }
    });
    context.start();
    getMockEndpoint("mock:resultA").expectedMessageCount(1);
    template.sendBody("direct:startA", "a trigger");
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder)

Example 9 with AdviceWithRouteBuilder

use of org.apache.camel.builder.AdviceWithRouteBuilder in project camel by apache.

the class AdviceWithInvalidConfiguredTest method testNoExtraRoutes.

public void testNoExtraRoutes() throws Exception {
    try {
        context.getRouteDefinition("route-a").adviceWith(context, new AdviceWithRouteBuilder() {

            @Override
            public void configure() throws Exception {
                from("direct:foo").to("mock:foo");
            }
        });
        fail("Should have thrown an exception");
    } catch (IllegalArgumentException e) {
        assertEquals("You can only advice from a RouteBuilder which has no existing routes. Remove all routes from the route builder.", e.getMessage());
    }
}
Also used : AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder)

Example 10 with AdviceWithRouteBuilder

use of org.apache.camel.builder.AdviceWithRouteBuilder in project camel by apache.

the class RouteDefinition method adviceWith.

/**
     * Advices this route with the route builder.
     * <p/>
     * <b>Important:</b> It is recommended to only advice a given route once (you can of course advice multiple routes).
     * If you do it multiple times, then it may not work as expected, especially when any kind of error handling is involved.
     * The Camel team plan for Camel 3.0 to support this as internal refactorings in the routing engine is needed to support this properly.
     * <p/>
     * You can use a regular {@link RouteBuilder} but the specialized {@link org.apache.camel.builder.AdviceWithRouteBuilder}
     * has additional features when using the <a href="http://camel.apache.org/advicewith.html">advice with</a> feature.
     * We therefore suggest you to use the {@link org.apache.camel.builder.AdviceWithRouteBuilder}.
     * <p/>
     * The advice process will add the interceptors, on exceptions, on completions etc. configured
     * from the route builder to this route.
     * <p/>
     * This is mostly used for testing purpose to add interceptors and the likes to an existing route.
     * <p/>
     * Will stop and remove the old route from camel context and add and start this new advised route.
     *
     * @param camelContext the camel context
     * @param builder      the route builder
     * @return a new route which is this route merged with the route builder
     * @throws Exception can be thrown from the route builder
     * @see AdviceWithRouteBuilder
     */
@SuppressWarnings("deprecation")
public RouteDefinition adviceWith(ModelCamelContext camelContext, RouteBuilder builder) throws Exception {
    ObjectHelper.notNull(camelContext, "CamelContext");
    ObjectHelper.notNull(builder, "RouteBuilder");
    log.debug("AdviceWith route before: {}", this);
    // and offer features to manipulate the route directly
    if (builder instanceof AdviceWithRouteBuilder) {
        ((AdviceWithRouteBuilder) builder).setOriginalRoute(this);
    }
    // configure and prepare the routes from the builder
    RoutesDefinition routes = builder.configureRoutes(camelContext);
    log.debug("AdviceWith routes: {}", routes);
    // we can only advice with a route builder without any routes
    if (!builder.getRouteCollection().getRoutes().isEmpty()) {
        throw new IllegalArgumentException("You can only advice from a RouteBuilder which has no existing routes." + " Remove all routes from the route builder.");
    }
    // context scoped error handler, in case no error handlers was configured
    if (builder.getRouteCollection().getErrorHandlerBuilder() != null && camelContext.getErrorHandlerBuilder() != builder.getRouteCollection().getErrorHandlerBuilder()) {
        throw new IllegalArgumentException("You can not advice with error handlers. Remove the error handlers from the route builder.");
    }
    // stop and remove this existing route
    camelContext.removeRouteDefinition(this);
    // any advice with tasks we should execute first?
    if (builder instanceof AdviceWithRouteBuilder) {
        List<AdviceWithTask> tasks = ((AdviceWithRouteBuilder) builder).getAdviceWithTasks();
        for (AdviceWithTask task : tasks) {
            task.task();
        }
    }
    // now merge which also ensures that interceptors and the likes get mixed in correctly as well
    RouteDefinition merged = routes.route(this);
    // add the new merged route
    camelContext.getRouteDefinitions().add(0, merged);
    // log the merged route at info level to make it easier to end users to spot any mistakes they may have made
    log.info("AdviceWith route after: " + merged);
    // If the camel context is started then we start the route
    if (camelContext instanceof StatefulService) {
        StatefulService service = (StatefulService) camelContext;
        if (service.isStarted()) {
            camelContext.startRoute(merged);
        }
    }
    return merged;
}
Also used : AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder) AdviceWithTask(org.apache.camel.builder.AdviceWithTask) StatefulService(org.apache.camel.StatefulService)

Aggregations

AdviceWithRouteBuilder (org.apache.camel.builder.AdviceWithRouteBuilder)94 RouteDefinition (org.apache.camel.model.RouteDefinition)17 Test (org.junit.Test)17 Exchange (org.apache.camel.Exchange)10 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)10 Processor (org.apache.camel.Processor)9 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)3 ResolveEndpointFailedException (org.apache.camel.ResolveEndpointFailedException)3 SedaEndpoint (org.apache.camel.component.seda.SedaEndpoint)2 ToDefinition (org.apache.camel.model.ToDefinition)2 ObjectName (javax.management.ObjectName)1 Endpoint (org.apache.camel.Endpoint)1 StatefulService (org.apache.camel.StatefulService)1 AdviceWithTask (org.apache.camel.builder.AdviceWithTask)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 ChoiceDefinition (org.apache.camel.model.ChoiceDefinition)1 LogDefinition (org.apache.camel.model.LogDefinition)1 SplitDefinition (org.apache.camel.model.SplitDefinition)1 InSequence (org.jboss.arquillian.junit.InSequence)1 Before (org.junit.Before)1