14

The DirectComponent documentation gives the following example:

from("activemq:queue:order.in")
    .to("bean:orderServer?method=validate")
    .to("direct:processOrder");

from("direct:processOrder")
    .to("bean:orderService?method=process")
    .to("activemq:queue:order.out");

Is there any difference between that and the following?

from("activemq:queue:order.in")
    .to("bean:orderServer?method=validate")
    .to("bean:orderService?method=process")
    .to("activemq:queue:order.out");

I've tried to find documentation on what the behaviour of the to() method is on the Java DSL, but beyond the RouteDefinition javadoc (which gives the very curt "Sends the exchange to the given endpoint") I've come up blank :(

bacar
  • 9,761
  • 11
  • 55
  • 75

5 Answers5

18

In the very case above, you will not notice much difference. The "direct" component is much like a method call.

Once you start build a bit more complex routes, you will want to segment them in several different parts for multiple reasons.

You can, for instance, create "sub routes" that could be reused among multiple routes in your Camel context. Much like you segment out methods in regular programming to allow reusability and make code more clear. The same goes for sub routes using, for instance the direct component.

The same approach can be extended. Say you want multiple protocols to be used as endpoints to your route. You can use the direct endpoint to create the main route, something like this:

// Three endpoints to one "main" route.
from("activemq:queue:order.in")
  .to("direct:processOrder");

from("file:some/file/path")
  .to("direct:processOrder");

from("jetty:http://0.0.0.0/order/in")
  .to("direct:processOrder");

from("direct:processOrder")
  .to("bean:orderService?method=process")
  .to("activemq:queue:order.out");

Another thing is that one route is created for each "from()" clause in DSL. A route is an artifact in Camel, and you could do certain administrative tasks towards it with the Camel API, such as start, stop, add, remove routes dynamically. The "to" clause is just an endpoint call.

Once starting to do some real cases with somewhat complexity in Camel, you will note that you cannot get too many "direct" routes.

Petter Nordlander
  • 22,053
  • 5
  • 50
  • 84
  • OK. I understand that it's a point of reusability/identity; Is it the case that there is no behavioural difference? – bacar Jun 08 '12 at 23:57
  • 'The "to" clause is just an endpoint call.' - what, _precisely_, does this mean? It sounds a little vague. – bacar Jun 08 '12 at 23:58
  • The to clause will do just that you said in your question. Send the exchange to an endpoint of a component, where the exchange is the message envelope, and the endpoint is a certain configuration of a component. The behaviour of to() depends a lot of the component (http://http://camel.apache.org/components). – Petter Nordlander Jun 09 '12 at 07:45
  • I still don't understand. I can't find anything in that link that suggests the behaviour of the to() method is dependent on the component. – bacar Jun 09 '12 at 12:09
  • I'm accepting this answer (for now), however I'm pretty sure I had a case that behaved differently depending on whether you used `.to` directly vs going via `"direct:"`, when it spanned a split/aggregate - if I can replicate the problem I'll post it. – bacar Aug 14 '12 at 15:01
  • @bacar It ultimately invokes methods on the Consumer and Producer which represent from and to, and tend to be implmented individually for every consumer –  Feb 08 '17 at 14:20
2

Direct Component is used to name the logical segment of the route. This is similar process to naming procedures in structural programming.

In your example there is no difference in message flow. In the terms of structural programming, we could say that you make a kind of inline expansion to your route.

Henryk Konsek
  • 9,016
  • 5
  • 32
  • 41
1

Another difference is Direct component doesn't has any thread pool, the direct consumer process method is invoked by the calling thread of direct producer.

Willem Jiang
  • 3,291
  • 1
  • 14
  • 14
0
 from(A).to(B).to(OUT) 

is chaining

A --- B --- OUT

But

from(A ).to( X)
from(B ).to( X)
       from( X).to( OUT )

where X is a direct:?

is basically like a join

A
  \____ OUT
  /
B

obviously these are different behaviours, and with the second you could implement anylogic you wanted, not just a serial chain

  • How would one start a route with from(direct)... and then read a file? – MRK187 Feb 08 '17 at 13:36
  • from( file ) .to( direct ) your questions a little ambiguous so I'm approximating what I guess you mean. If you repeat more explicitly I can tell you more precisely If you mean like a trigger, try "pollenrich()" - I dont thin kit's documented very well but it's like a trigger from( direct:A ) .pollenrich( B ) –  Feb 08 '17 at 14:49
0

Mainly its used for break the complex route configuration like in java we used to have method for reusability. And also by configuring threads at direct route we can reduce the work for calling thread .