0

I want to route to different destinations based on the values that are present in the Map. For example, I've a map with key and value. I want to iterate over the Map and want to route the message to different destination based on the key value of Map.

Assume that my map contains below keys and values:

Map myMap = new HashMap();

I would like to iterate over the map and based on key I wanted to invoke the corresponding endpoint which is in map value.

If (Key == "demo") then route to demo uri in the map value etc.

rocky
  • 753
  • 2
  • 10
  • 26
  • Hi! Do you want to route the whole map, or just the value from the map? Is it Key=URL, Object=Body? – Screwtape Jan 29 '19 at 18:43
  • 2
    Look at the content based router EIP or the dynamic router EIP or recipient list EIP which can route messages depending on their content. – Claus Ibsen Jan 29 '19 at 19:10
  • The best place to find how to do something with Apache Camel is to refer to the extensive test suite. Follow what @ClausIbsen said. [This](https://github.com/apache/camel/blob/master/camel-core/src/test/java/org/apache/camel/processor/DynamicRouterTest.java) is a good place to start and imporovise on. If I get some time later today, I'll post an example. – ShellDragon Jan 30 '19 at 10:55

1 Answers1

0

Dynamic router/Splitter are not the solutions, but Recipient list is.


Having the same question and it seems the original link is broken. Searching DynamicRouterTests.java leading me to this repo: https://github.com/camelinaction/camelinaction/blob/master/chapter8/dynamicrouter/src/test/java/camelinaction/DynamicRouterTest.java

But I think it only sends to one endpoint at last; it's a choice, not a duplication to multiple endpoints.

Splitter neither. See https://github.com/camelinaction/camelinaction/blob/master/chapter8/splitter/src/test/java/camelinaction/SplitterBeanTest.java

where one Customer is split into 3 departments. But it's duplication on one endpoint, not one message to multiple endpoints.

While Recipient List EIP allows you to appoint several destinations for one message. Check https://github.com/camelinaction/camelinaction/tree/master/chapter2/recipientlist/src/main/java/camelinaction

The main route:

                from("jms:xmlOrders").bean(RecipientListBean.class);
                
                // test that our route is working
                from("jms:accounting").process(new Processor() {
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("Accounting received order: " 
                                + exchange.getIn().getHeader("CamelFileName"));   
                    }
                });                
                from("jms:production").process(new Processor() {
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("Production received order: " 
                                + exchange.getIn().getHeader("CamelFileName"));   
                    }
                });

And the recipients come from:

public class RecipientListBean {
    @RecipientList
    public String[] route(@XPath("/order/@customer") String customer) {
        if (isGoldCustomer(customer)) {
            return new String[] {"jms:accounting", "jms:production"};
        } else {
            return new String[] {"jms:accounting"};
        }
    }

    private boolean isGoldCustomer(String customer) {
        return customer.equals("honda");
    }
}

You can see in the example that for each recipient, you can specify what to do afterwards in the route, respectively. Maybe defining universal processing in the main route is also possible.

WesternGun
  • 11,303
  • 6
  • 88
  • 157