285.5. 将消息发送到/从 routebox 发送
在发送请求前,需要通过将所需的 URI 参数加载到 Registry 中来正确配置 routebox,如下所示。对于 Spring,如果正确声明了所需的 Bean,则 registry 将自动由 Camel 填充。
285.5.1. 第 1 步:在 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());
285.5.2. 第 2 步:可选使用 Dispatch 策略而不是 Dispatch Map 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
使用分配策略涉及实施接口 org.apache.camel.component.routebox.strategy.RouteboxDispatchStrategy,如下例所示。
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;
}
}
285.5.3. 第 2 步:启动 routebox 消费者 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
在创建路由消费者时,请注意 routeboxUri 中的 # 条目与 CamelContext Registry 中创建的内部 registry, routebuilder list 和 assignStrategy/dispatchMap 匹配。请注意,所有 routebuilder 和关联的路由都在创建的内部上下文中的 routebox 中启动
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");
}
285.5.4. 第 3 步:使用 routebox producer 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
当向 routebox 发送请求时,生成者不需要知道内部路由端点 URI,它们只需使用分配策略或分配Map 调用 Routebox URI 端点,如下所示
需要设置一个特殊的交换标头,名为 ROUTE_DISPATCH_KEY (用于 Dispatch Strategy)的密钥,其键与分配映射中的键匹配,以便可将请求发送到正确的内部路由
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");