3

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!

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

2 Answers2

4

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);
     }
}
kinaesthesia
  • 703
  • 6
  • 27
  • Thanks @kinaesthesia (+1) - how and where do I set this header? Let's say that the `direct:start` endpoint was actually an `OrderConsumer` that generated `Orders`. I assume somewhere between the `from()` and `choice()` methods I can set the header somehow?? Thanks again! – IAmYourFaja Jan 25 '13 at 12:39
  • From [this page](http://camel.apache.org/constant.html) I see that I could do something like `from(...).setHeader("product", constant(order.getProduct()).to(...)`, **however**, where does the `order` instance variable come from in the first place?!? I think this is at the root of my confusion! – IAmYourFaja Jan 25 '13 at 12:45
  • 1
    Again, thanks @jinaesthesia and +1. But we're still at the same problem: where does `yourOrderObject` come from? Somewhere in the `exchange` instance? I think my mental hangup is stemming from the fact that I don't see how to obtain each `Order` instance as it travels along the route, and set it's `getProduct()` value to the appropriate header. Once I understand where `yourOrderObject` comes from (in your example above there would be a compiler error because `yourOrderObject` would be unrecognized) I'll be able to piece it all together. Thanks again so much! – IAmYourFaja Jan 25 '13 at 12:59
  • That's the point. i don't know your application but can't you use injection with spring to obtain a refence ? Or maybe by a JMS queue if you can ? – kinaesthesia Jan 25 '13 at 13:08
  • No - it should be possible (I believe the `Exchange` is the key) to obtain the payload of the message being passed to the processor. You've got me 90% of the way there and I am thankful for that, let's see if anybody else can help guide me in the last 10%. Again, thanks! – IAmYourFaja Jan 25 '13 at 14:12
  • Ahh, it's so simple! From inside the `beanProcessor`, which implements `Processor` and thus has a `process(Exchange inExchange)` method, call `exchange.getIn().getBody()` to get the `Order` instance, and set the header with `exchange.getIn().setHeader(...)`. I only wish there was a way to do this from the route itself without having to create a clunky `beanProcessor`. Anybody have any ideas? – IAmYourFaja Jan 25 '13 at 15:48
3

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");
Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65