285.5. 경로 상자에서 /로 메시지 전송/거부
요청을 보내기 전에 다음과 같이 필요한 URI 매개변수를 레지스트리에 로드하여 경로 상자를 올바르게 구성해야 합니다. Spring의 경우 필요한 빈이 올바르게 선언되면 Camel에 의해 레지스트리가 자동으로 채워집니다.
285.5.1. 1단계: 레지스트리에 내부 경로 세부 정보 로드 링크 복사링크가 클립보드에 복사되었습니다!
@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 Map 대신 Dispatch Strategy를 선택적으로 사용 링크 복사링크가 클립보드에 복사되었습니다!
디스패치 전략을 사용하려면 아래 예제와 같이 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에서 생성된 내부 레지스트리, routebuilder list 및 dispatchStrategy/dispatchMap과 일치합니다. 모든 routebuilders 및 관련 경로는 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 생산자 사용 링크 복사링크가 클립보드에 복사되었습니다!
경로 박스에 요청을 보낼 때 생산자는 내부 경로 엔드포인트 URI를 알 필요가 없으며 아래 표시된 대로 디스패치 전략 또는 디스패치 전략으로 Routebox URI 끝점을 간단히 호출할 수 있습니다.
요청을 올바른 내부 경로로 보낼 수 있도록 디스패치 맵의 키와 일치하는 키를 사용하여 ROUTE_DISPATCH_KEY 라는 특수 교환 헤더를 설정해야 합니다.
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");