Camel CBRs and POJO Property Inspection

I have a Camel route that routes Order

instances:

from("direct:start")
    .choice()
        .when(order.getProduct() == Product.Widget)
            .to("direct:widgets")
        .when(order.getProduct() == Product.Fizz)
            .to("direct:fizzes")
        .otherwise()
            .to("direct:allOtherProducts");

      

So, if the specific Order

is the order of a Widget

, it needs to be redirected to direct:widgets

, etc.

I'm choking on what to put inside each method when(...)

. What I have is not legal Camel DSL syntax and is used to illustrate what I want to accomplish.

So my question is, what do I put in each method when(...)

to accomplish the kind of routing I'm looking for?
Thanks in advance!

+3


source to share


2 answers


You have to put the value of your order.getProduct () in the header and use it like this:

from("direct:start")
        .choice()
            .when(header("product").isEqualTo(Product.Widget))
                .to("direct:widgets")
            .when(header("product").isEqualTo(Product.Fizz))
                .to("direct:fizzes")
            .otherwise()
                .to("direct:allOtherProducts");

      

EDIT:

You can use a process (i.e.: in a DSL):

<route id="foo">
    <from uri="direct:start"/>
    <process ref="beanProcessor" />
    <choice>
        <when>
            ...
        </when>
        <when>
            ...
        </when>
        <otherwise>
            ...
        </otherwise>
    </choice>

      



Bean Announcement:

<bean id="beanProcessor" class="MyProcessor" />

      

Class:

public class MyProcessorimplements Processor {

     @Override
     public void process(Exchange exchange) throws Exception {
         exchange.getIn().setHeader("headerName", yourOrderObject);
     }
}

      

+4


source


I think the Order type is the body of the message. So, in Java DSL, you can do



from("direct:start")
  .choice()
     .when(body().isInstanceOf(MyOrder.class)).to("direct:myOrder")
     .when(body().isInstanceOf(MyOtheOrder.class)).to("direct:myOtherOrder")
     .otherwise().to("direct:other");

      

+3


source







All Articles