Search in sources :

Example 71 with RouteDefinition

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

the class ErrorHandlerSupport method addExceptionPolicy.

public void addExceptionPolicy(RouteContext routeContext, OnExceptionDefinition exceptionType) {
    if (routeContext != null) {
        // add error handler as child service so they get lifecycle handled
        Processor errorHandler = exceptionType.getErrorHandler(routeContext.getRoute().getId());
        if (errorHandler != null) {
            addChildService(errorHandler);
        }
    }
    List<Class<? extends Throwable>> list = exceptionType.getExceptionClasses();
    for (Class<? extends Throwable> clazz : list) {
        String routeId = null;
        // only get the route id, if the exception type is route scoped
        if (exceptionType.isRouteScoped()) {
            RouteDefinition route = ProcessorDefinitionHelper.getRoute(exceptionType);
            if (route != null) {
                routeId = route.getId();
            }
        }
        ExceptionPolicyKey key = new ExceptionPolicyKey(routeId, clazz, exceptionType.getOnWhen());
        exceptionPolicies.put(key, exceptionType);
    }
}
Also used : Processor(org.apache.camel.Processor) RouteDefinition(org.apache.camel.model.RouteDefinition) ExceptionPolicyKey(org.apache.camel.processor.exceptionpolicy.ExceptionPolicyKey)

Example 72 with RouteDefinition

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

the class RouteScopedErrorHandlerAndOnExceptionTest method testOnException.

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

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("seda:*").skipSendToOriginalEndpoint().throwException(new ConnectException("Forced"));
        }
    });
    getMockEndpoint("mock:local").expectedMessageCount(0);
    getMockEndpoint("mock:seda").expectedMessageCount(0);
    // we fail all redeliveries so after that we send to mock:exhausted
    getMockEndpoint("mock:exhausted").expectedMessageCount(1);
    try {
        template.sendBody("direct:start", "Hello World");
        fail("Should thrown an exception");
    } catch (CamelExecutionException e) {
        ConnectException cause = assertIsInstanceOf(ConnectException.class, e.getCause());
        assertEquals("Forced", cause.getMessage());
    }
    assertMockEndpointsSatisfied();
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) RouteDefinition(org.apache.camel.model.RouteDefinition) RouteBuilder(org.apache.camel.builder.RouteBuilder) CamelExecutionException(org.apache.camel.CamelExecutionException) ConnectException(java.net.ConnectException) FileNotFoundException(java.io.FileNotFoundException) ConnectException(java.net.ConnectException)

Example 73 with RouteDefinition

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

the class RouteScopedOnExceptionWithInterceptSendToEndpointIssueTest method testIssue.

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

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("seda:*").skipSendToOriginalEndpoint().throwException(new ConnectException("Forced"));
        }
    });
    getMockEndpoint("mock:global").expectedMessageCount(0);
    getMockEndpoint("mock:seda").expectedMessageCount(0);
    // we fail all redeliveries so after that we send to mock:exhausted
    getMockEndpoint("mock:exhausted").expectedMessageCount(1);
    try {
        template.sendBody("direct:start", "Hello World");
        fail("Should thrown an exception");
    } catch (CamelExecutionException e) {
        ConnectException cause = assertIsInstanceOf(ConnectException.class, e.getCause());
        assertEquals("Forced", cause.getMessage());
    }
    assertMockEndpointsSatisfied();
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) RouteDefinition(org.apache.camel.model.RouteDefinition) RouteBuilder(org.apache.camel.builder.RouteBuilder) CamelExecutionException(org.apache.camel.CamelExecutionException) ConnectException(java.net.ConnectException) ConnectException(java.net.ConnectException)

Example 74 with RouteDefinition

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

the class TemporalRule method getOverdueAction.

public Processor getOverdueAction() throws Exception {
    if (overdueAction == null && overdueProcessors != null) {
        RouteDefinition route = new RouteDefinition();
        RouteContext routeContext = new DefaultRouteContext(first.getBuilder().getProcessBuilder().getContext(), route, null, new ArrayList<Route>());
        overdueAction = overdueProcessors.createOutputsProcessor(routeContext);
    }
    return overdueAction;
}
Also used : DefaultRouteContext(org.apache.camel.impl.DefaultRouteContext) RouteDefinition(org.apache.camel.model.RouteDefinition) Route(org.apache.camel.Route) DefaultRouteContext(org.apache.camel.impl.DefaultRouteContext) RouteContext(org.apache.camel.spi.RouteContext)

Example 75 with RouteDefinition

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

the class RouteBuilder method populateRests.

protected void populateRests() throws Exception {
    ModelCamelContext camelContext = getContext();
    if (camelContext == null) {
        throw new IllegalArgumentException("CamelContext has not been injected!");
    }
    getRestCollection().setCamelContext(camelContext);
    // setup rest configuration before adding the rests
    if (getRestConfigurations() != null) {
        for (Map.Entry<String, RestConfigurationDefinition> entry : getRestConfigurations().entrySet()) {
            RestConfiguration config = entry.getValue().asRestConfiguration(getContext());
            if ("".equals(entry.getKey())) {
                camelContext.setRestConfiguration(config);
            } else {
                camelContext.addRestConfiguration(config);
            }
        }
    }
    camelContext.addRestDefinitions(getRestCollection().getRests());
    // convert rests into routes so we they are routes for runtime
    List<RouteDefinition> routes = new ArrayList<RouteDefinition>();
    for (RestDefinition rest : getRestCollection().getRests()) {
        List<RouteDefinition> list = rest.asRouteDefinition(getContext());
        routes.addAll(list);
    }
    // convert rests api-doc into routes so they are routes for runtime
    for (RestConfiguration config : camelContext.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 : camelContext.getRouteDefinitions()) {
                FromDefinition from = route.getInputs().get(0);
                if (from.getUri() != null && from.getUri().startsWith("rest-api:")) {
                    hasRestApi = true;
                }
            }
            if (!hasRestApi) {
                RouteDefinition route = RestDefinition.asRouteApiDefinition(camelContext, config);
                log.debug("Adding routeId: {} as rest-api route", route.getId());
                routes.add(route);
            }
        }
    }
    // add the rest routes
    for (RouteDefinition route : routes) {
        getRouteCollection().route(route);
    }
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) FromDefinition(org.apache.camel.model.FromDefinition) InterceptFromDefinition(org.apache.camel.model.InterceptFromDefinition) ArrayList(java.util.ArrayList) ModelCamelContext(org.apache.camel.model.ModelCamelContext) RestConfigurationDefinition(org.apache.camel.model.rest.RestConfigurationDefinition) RouteDefinition(org.apache.camel.model.RouteDefinition) RestConfiguration(org.apache.camel.spi.RestConfiguration) HashMap(java.util.HashMap) Map(java.util.Map)

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