Frage

I have a Camel route that is routing 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 a particular Order is an order of a Widget, it needs to be routed to direct:widgets, etc.

I'm choking on what to put inside each when(...) method. What I have is not legal Camel DSL syntax, and is used for illustrating what I want to accomplish.

So I ask: what do I put in each when(...) method to accomplish the sort of routing I'm looking for? Thanks in advance!

War es hilfreich?

Lösung

You should put the value of your order.getProduct() in a header and use it like that ::

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 could use a process (i.e : in DSL ) :

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

Bean declaration :

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

The class :

public class MyProcessorimplements Processor {

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

Andere Tipps

I think the Order type is the message body. 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");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top