Search in sources :

Example 91 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class AdviceWithTwoRoutesOnExceptionTest method testAdviceWithAB.

public void testAdviceWithAB() throws Exception {
    RouteDefinition route = context.getRouteDefinition("a");
    route.adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("mock://a").skipSendToOriginalEndpoint().throwException(new IllegalArgumentException("Forced"));
        }
    });
    route = context.getRouteDefinition("b");
    route.adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("mock://b").skipSendToOriginalEndpoint().throwException(new IllegalArgumentException("Forced"));
        }
    });
    getMockEndpoint("mock:a").expectedMessageCount(0);
    getMockEndpoint("mock:b").expectedMessageCount(0);
    getMockEndpoint("mock:error").expectedMessageCount(2);
    getMockEndpoint("mock:error").message(0).exchangeProperty(Exchange.EXCEPTION_CAUGHT).isInstanceOf(IllegalArgumentException.class);
    getMockEndpoint("mock:error").message(1).exchangeProperty(Exchange.EXCEPTION_CAUGHT).isInstanceOf(IllegalArgumentException.class);
    template.sendBody("direct:a", "Hello World");
    template.sendBody("direct:b", "Bye World");
    assertMockEndpointsSatisfied();
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder)

Example 92 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class InterceptSendToEndpointWithParametersTest method testInterceptSendToEndpoint.

public void testInterceptSendToEndpoint() throws Exception {
    RouteDefinition route = context.getRouteDefinitions().get(0);
    route.adviceWith(context, new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("log*").to("mock:http").skipSendToOriginalEndpoint();
        }
    });
    getMockEndpoint("mock:http").expectedMessageCount(1);
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) RouteBuilder(org.apache.camel.builder.RouteBuilder)

Example 93 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class AdviceWithTwoRoutesTest method testAdviceWithA.

public void testAdviceWithA() throws Exception {
    RouteDefinition route = context.getRouteDefinition("a");
    route.adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("mock://a").skipSendToOriginalEndpoint().to("mock:detour");
        }
    });
    getMockEndpoint("mock:a").expectedMessageCount(0);
    getMockEndpoint("mock:detour").expectedMessageCount(1);
    template.sendBody("direct:a", "Hello World");
    assertMockEndpointsSatisfied();
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder)

Example 94 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class AbstractCamelContextFactoryBean method setupRoutes.

/**
     * Setup all the routes which must be done prior starting {@link CamelContext}.
     */
protected void setupRoutes() throws Exception {
    if (routesSetupDone.compareAndSet(false, true)) {
        LOG.debug("Setting up routes");
        // mark that we are setting up routes
        getContext().setupRoutes(false);
        // must init route refs before we prepare the routes below
        initRouteRefs();
        // must init rest refs before we add the rests
        initRestRefs();
        // and add the rests
        getContext().addRestDefinitions(getRests());
        // convert rests into routes so we reuse routes for runtime
        for (RestDefinition rest : getRests()) {
            List<RouteDefinition> routes = rest.asRouteDefinition(getContext());
            for (RouteDefinition route : routes) {
                getRoutes().add(route);
            }
        }
        // convert rests api-doc into routes so they are routes for runtime
        for (RestConfiguration config : getContext().getRestConfigurations()) {
            if (config.getApiContextPath() != null) {
                // avoid adding rest-api multiple times, in case multiple RouteBuilder classes is added
                // to the CamelContext, as we only want to setup rest-api once
                // so we check all existing routes if they have rest-api route already added
                boolean hasRestApi = false;
                for (RouteDefinition route : getContext().getRouteDefinitions()) {
                    FromDefinition from = route.getInputs().get(0);
                    if (from.getUri() != null && from.getUri().startsWith("rest-api:")) {
                        hasRestApi = true;
                    }
                }
                if (!hasRestApi) {
                    RouteDefinition route = RestDefinition.asRouteApiDefinition(getContext(), config);
                    LOG.debug("Adding routeId: {} as rest-api route", route.getId());
                    getRoutes().add(route);
                }
            }
        }
        // do special preparation for some concepts such as interceptors and policies
        // this is needed as JAXB does not build exactly the same model definition as Spring DSL would do
        // using route builders. So we have here a little custom code to fix the JAXB gaps
        prepareRoutes();
        // and add the routes
        getContext().addRouteDefinitions(getRoutes());
        LOG.debug("Found JAXB created routes: {}", getRoutes());
        findRouteBuilders();
        installRoutes();
        // and we are now finished setting up the routes
        getContext().setupRoutes(true);
    }
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) FromDefinition(org.apache.camel.model.FromDefinition) InterceptFromDefinition(org.apache.camel.model.InterceptFromDefinition) RouteDefinition(org.apache.camel.model.RouteDefinition) RestConfiguration(org.apache.camel.spi.RestConfiguration)

Example 95 with RouteDefinition

use of org.apache.camel.model.RouteDefinition in project camel by apache.

the class AbstractCamelContextFactoryBean method initRouteRefs.

protected void initRouteRefs() throws Exception {
    // add route refs to existing routes
    if (getRouteRefs() != null) {
        for (RouteContextRefDefinition ref : getRouteRefs()) {
            List<RouteDefinition> defs = ref.lookupRoutes(getContext());
            for (RouteDefinition def : defs) {
                LOG.debug("Adding route from {} -> {}", ref, def);
                // add in top as they are most likely to be common/shared
                // which you may want to start first
                getRoutes().add(0, def);
            }
        }
    }
}
Also used : RouteContextRefDefinition(org.apache.camel.model.RouteContextRefDefinition) RouteDefinition(org.apache.camel.model.RouteDefinition)

Aggregations

RouteDefinition (org.apache.camel.model.RouteDefinition)102 AdviceWithRouteBuilder (org.apache.camel.builder.AdviceWithRouteBuilder)17 RouteBuilder (org.apache.camel.builder.RouteBuilder)17 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)9 FromDefinition (org.apache.camel.model.FromDefinition)9 HashMap (java.util.HashMap)6 Processor (org.apache.camel.Processor)6 ConnectException (java.net.ConnectException)5 Exchange (org.apache.camel.Exchange)5 ProcessorDefinition (org.apache.camel.model.ProcessorDefinition)5 IOException (java.io.IOException)4 Map (java.util.Map)4 MBeanServer (javax.management.MBeanServer)4 ObjectName (javax.management.ObjectName)4 ChoiceDefinition (org.apache.camel.model.ChoiceDefinition)4 HystrixConfigurationDefinition (org.apache.camel.model.HystrixConfigurationDefinition)4 HystrixDefinition (org.apache.camel.model.HystrixDefinition)4 LogDefinition (org.apache.camel.model.LogDefinition)4 ModelCamelContext (org.apache.camel.model.ModelCamelContext)4