Search code examples
javaapache-cameleip

Camel Java DSL - routing on ENUM in header - conent based router or dynamic router


I have a bean which produces objects and sends them to a SEDA queue using the ProducerTemplate in Camel.

I send a body and a header:

producerTemp.sendBodyAndHeader(document, "sourceSystem", sourceSys);

Here the header name is sourceSystem and the header object value is an ENUM (sourceSys) which contains the source of the document object containing a number of differnt attribs.

I want to pull messages in a concurrent fashion from the SEDA queue and send them to different endpoints depending on the value of the sourceSys enum.

What is the most efficient EIP in camel to use this and does anyone have an example using Java DSL, I'm not sure how I can test the value of the Enum?

I am thinking I do something like this:

from("seda:a")
    .choice()
        .when(header("foo").isEqualTo(SourceSysEnum.SYSTEM1))
            .to("seda:b")
        .when(header("foo").isEqualTo(SourceSysEnum.SYSTEM2))
            .to("seda:c")
        .otherwise()
            .to("seda:d");

..?


Solution

  • You can use the recipient list EIP http://camel.apache.org/recipient-list.html

    And then for example use a java bean to compute the uri where the message should go.

    from("seda:a")
      .recpientList().method(MyBean.class, "whereToGo");
    

    And in the bean you can use bean parameter binding.

    So you can bind the header as follows:

    public class MyBean {
    
       public String whereToGo(String body, @Header("foo") SourceSysEnum sys) {
          ...
       }
    }
    

    If you dont need the message body, then you can omit that parameter.