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);
}
}
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();
}
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();
}
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;
}
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);
}
}
Aggregations