@DynamicRouter public String route(@Header(Exchange.SLIP_ENDPOINT) String previous) { if (previous == null) { return target; } else { return null; } }
/** * The method invoked by Dynamic Router EIP to compute where to go next. * <p/> * Notice this method has been annotated with @DynamicRouter which means Camel turns this method * invocation into a Dynamic Router EIP automatically. * * @param body the message body * @param previous the previous endpoint, is <tt>null</tt> on the first invocation * @return endpoint uri where to go, or <tt>null</tt> to indicate no more */ @DynamicRouter public String route(String body, @Header(Exchange.SLIP_ENDPOINT) String previous) { if (previous == null) { // 1st time return "mock://a"; } else if ("mock://a".equals(previous)) { // 2nd time - transform the message body using the simple language return "language://simple:Bye ${body}"; } else { // no more, so return null to indicate end of dynamic router return null; } }
/** * Returns the next endpoint to route a message to or null to finish routing. * This implementation leverages Camel's * <a href="http://camel.apache.org/bean-integration.html">Bean injection</a> * to pass parts of the Camel Exchange to the method for processing. This can * help the code be easier to maintain as it does not need the extra boilerplate * code for extracting the relative data from the Exchange. * <p></p> * This implementation stores an int property with the message exchange that is * used to drive the routing behavior. This method will be called from multiple * threads, one per message, so storing message specific state as a property is * a good strategy. * * @param body the IN message converted to a String using Camel Bean injection * @param properties the properties map associated with the Camel Exchange * @return next endpoint uri(s) to route to or <tt>null</tt> to finish routing */ @Consume(uri = "direct:start") @DynamicRouter(delimiter = ",") public String routeMe(String body, @ExchangeProperties Map<String, Object> properties) { LOG.info("Exchange.SLIP_ENDPOINT = {}, invoked = {}", properties.get(Exchange.SLIP_ENDPOINT), properties.get(PROPERTY_NAME_INVOKED)); // Store a property with the message exchange that will drive the routing // decisions of this Dynamic Router implementation. int invoked = 0; Object current = properties.get(PROPERTY_NAME_INVOKED); // property will be null on first call if (current != null) { invoked = Integer.valueOf(current.toString()); } invoked++; properties.put(PROPERTY_NAME_INVOKED, invoked); if (invoked == 1) { return "mock:a"; } else if (invoked == 2) { return "mock:b,mock:c"; } else if (invoked == 3) { return "direct:other"; } else if (invoked == 4) { return "mock:result"; } // no more, so return null return null; }