11

I had worked with both SEDA and direct, and I've also read the documentation.

But I still cannot visualize the usage of SEDA and direct. Vm is new to me.

Please explain it with an example.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
javalearner_heaven
  • 483
  • 1
  • 6
  • 21
  • 2
    this is a relevant question specific to the apache camel framework. i will write an answer - no down votes required here as far as i can see – vikingsteve Sep 07 '17 at 09:57
  • why on earth do you vote to close this? its perfectly clear what OP is asking. google "apache camel components" and you'll see immediately – vikingsteve Sep 07 '17 at 10:29
  • This a reasonably clear and relevant question if you know Camel, it should not have been closed. – pedram bashiri Jan 03 '19 at 18:55
  • Related: [Apache Camel Routes and Direct Component](https://stackoverflow.com/questions/16331886/back-to-basics-apache-camel-routes-and-direct-component) – jkdev Aug 28 '19 at 11:16
  • This link might also help: https://github.com/lasalazarr/camel-direct-vs-seda-example – jkdev Aug 28 '19 at 11:17

2 Answers2

34

There are at least four different mechanisms by which one Camel route can directly pass data to another. By "directly" I mean without using a network or some form of intermediate storage (file, database). These mechanisms can be grouped according to whether they can pass data between CamelContext instances or not, and whether they are synchronous or asynchronous.

  • direct -- single CamelContext, synchronous (blocks producer)
  • SEDA -- single CamelContext, asynchronous (does not block producer)
  • VM -- multiple CamelContext, asynchronous (does not block producer)
  • direct-VM -- multiple CamelContext, synchronous (blocks producer)

The direct and direct-VM mechanisms are synchronous, in the sense that the producing endpoint blocks until the consuming endpoint, and all the rest of its routing logic, is complete. The SEDA and VM mechanisms both use a pool of threads on the consumer, such that each request made by the producer is assigned to one of the threads in the pool. This allows the consumer endpoint and its associated routing logic to act independently of the producer.

Both the VM endpoints are required in situations where communication is between different Camel contexts. In many cases it is possible to combine routes into the same CamelContext. However, it may sometimes be inadvisable, for reasons of modularity, or impossible, because some application framework makes it so. For example, I might implement some Camel routing logic in a library (or component) with the intention that the library be used by other code. To be complete, this library will probably define a self-contained CamelContext with various routes. If I want to invoke the Camel logic in the library, I will need to use VM or direct-VM, because direct and SEDA endpoints do not contain the logic needed to route between Camel contexts.

Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219
Kevin Boone
  • 4,092
  • 1
  • 11
  • 15
  • Helped a lot ty....I wanna know in which scenario they are used in real-time and which mechanism is used in most common. – javalearner_heaven Sep 07 '17 at 10:14
  • I use direct: most of the time, simply to organize routing elements. In my experience, most Camel routes that have an interface to the world outside the route take data from some external system -- HTTP, database, FTP, file, whatever, and produce data to some other external system. direct: endpoints can be used within such a configuration to make the routing easier to read. In that situation, SEDA will be required if you want the routing between sections to be asynchronous, but I don't find that to be common (in my work). – Kevin Boone Sep 07 '17 at 10:22
  • Which one is the default, if any? Because I have seen routes in code without using any one of them. – Abhishek Singh Dec 06 '18 at 14:42
14

The difference between direct: and seda: components is that the first is synchronous and the second is asynchronous, with a queue.

The practical difference is that for sending synchronous messages, you must wait for the route to complete, whereas with asynchronous messages, its "fire and forget" - you put them onto a queue and presume a consumer will process them. You can also have multiple consumers (parallelisation).

The last example, vm: is also asynchronous, but it can also call routes in different camel contexts within the same JVM. Imagine application 1 has a camel context, and application 2 has a camel context, in this way they can communicate with each other.

edit:

In relation to "what to use when" :

  • use direct: for calling normally between endpoints in a camel context
  • use seda: when you need parallelisation or queues, but dont want to use jms:
  • use vm: when calling between applications.

There are of course many other use cases but those are the common ones (subjective to my own experience)

vikingsteve
  • 38,481
  • 23
  • 112
  • 156