Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the early version of the new website
https://camel.apache.org/staging/
We would very much like to receive any feedback on the new site, please join the discussion on the Camel user mailing list.
Routebox ComponentAvailable as of Camel 2.6 Routebox subject for change The routebox component enables the creation of specialized endpoints that offer encapsulation and a strategy based indirection service to a collection of camel routes hosted in an automatically created or user injected camel context. Routebox endpoints are camel endpoints that may be invoked directly on camel routes. The routebox endpoint performs the following key functions
The routebox component supports both consumer and producer endpoints. Producer endpoints are of two flavors
Maven users will need to add the following dependency to their <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-routebox</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> The need for a Camel Routebox endpointThe routebox component is designed to ease integration in complex environments needing
In such environments, it is often necessary to craft an integration solution by creating a sense of layering among camel routes effectively organizing them into
Requests sent to Routebox endpoints on coarse grained routes can then delegate requests to inner fine grained routes to achieve a specific integration objective, collect the final inner result, and continue to progress to the next step along the coarse-grained route. URI formatroutebox:routeboxname[?options] You can append query options to the URI in the following format, Options
Sending/Receiving Messages to/from the routeboxBefore sending requests it is necessary to properly configure the routebox by loading the required URI parameters into the Registry as shown below. In the case of Spring, if the necessary beans are declared correctly, the registry is automatically populated by Camel. Step 1: Loading inner route details into the Registry@Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry registry = new JndiRegistry(createJndiContext()); // Wire the routeDefinitions & dispatchStrategy to the outer camelContext where the routebox is declared List<RouteBuilder> routes = new ArrayList<RouteBuilder>(); routes.add(new SimpleRouteBuilder()); registry.bind("registry", createInnerRegistry()); registry.bind("routes", routes); // Wire a dispatch map to registry HashMap<String, String> map = new HashMap<String, String>(); map.put("addToCatalog", "seda:addToCatalog"); map.put("findBook", "seda:findBook"); registry.bind("map", map); // Alternatively wiring a dispatch strategy to the registry registry.bind("strategy", new SimpleRouteDispatchStrategy()); return registry; } private JndiRegistry createInnerRegistry() throws Exception { JndiRegistry innerRegistry = new JndiRegistry(createJndiContext()); BookCatalog catalogBean = new BookCatalog(); innerRegistry.bind("library", catalogBean); return innerRegistry; } ... CamelContext context = new DefaultCamelContext(createRegistry()); Step 2: Optionaly using a Dispatch Strategy instead of a Dispatch MapUsing a dispatch Strategy involves implementing the interface org.apache.camel.component.routebox.strategy.RouteboxDispatchStrategy as shown in the example below. public class SimpleRouteDispatchStrategy implements RouteboxDispatchStrategy { /* (non-Javadoc) * @see org.apache.camel.component.routebox.strategy.RouteboxDispatchStrategy#selectDestinationUri(java.util.List, org.apache.camel.Exchange) */ public URI selectDestinationUri(List<URI> activeDestinations, Exchange exchange) { URI dispatchDestination = null; String operation = exchange.getIn().getHeader("ROUTE_DISPATCH_KEY", String.class); for (URI destination : activeDestinations) { if (destination.toASCIIString().equalsIgnoreCase("seda:" + operation)) { dispatchDestination = destination; break; } } return dispatchDestination; } } Step 2: Launching a routebox consumerWhen creating a route consumer, note that the # entries in the routeboxUri are matched to the created inner registry, routebuilder list and dispatchStrategy/dispatchMap in the CamelContext Registry. Note that all routebuilders and associated routes are launched in the routebox created inner context private String routeboxUri = "routebox:multipleRoutes?innerRegistry=#registry&routeBuilders=#routes&dispatchMap=#map"; public void testRouteboxRequests() throws Exception { CamelContext context = createCamelContext(); template = new DefaultProducerTemplate(context); template.start(); context.addRoutes(new RouteBuilder() { public void configure() { from(routeboxUri) .to("log:Routes operation performed?showAll=true"); } }); context.start(); // Now use the ProducerTemplate to send the request to the routebox template.requestBodyAndHeader(routeboxUri, book, "ROUTE_DISPATCH_KEY", "addToCatalog"); } Step 3: Using a routebox producerWhen sending requests to the routebox, it is not necessary for producers do not need to know the inner route endpoint URI and they can simply invoke the Routebox URI endpoint with a dispatch strategy or dispatchMap as shown below It is necessary to set a special exchange Header called ROUTE_DISPATCH_KEY (optional for Dispatch Strategy) with a key that matches a key in the dispatch map so that the request can be sent to the correct inner route from("direct:sendToStrategyBasedRoutebox") .to("routebox:multipleRoutes?innerRegistry=#registry&routeBuilders=#routes&dispatchStrategy=#strategy") .to("log:Routes operation performed?showAll=true"); from ("direct:sendToMapBasedRoutebox") .setHeader("ROUTE_DISPATCH_KEY", constant("addToCatalog")) .to("routebox:multipleRoutes?innerRegistry=#registry&routeBuilders=#routes&dispatchMap=#map") .to("log:Routes operation performed?showAll=true"); |