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