Monday, March 7, 2016

Direct vs Seda vs Direct-Vm vs VM end points in Apache Camel

Apache camel is a rule based routing and mediation engine. It provides facility to perform seamless integration of various type of components via routes and endpoints.

Camel comes up with four camel aware end points.These end points have following similarities-
1. All of these end points accept camel exchange message only.
2. All of these end points are in-memory. Message in these queues would get lost in case of abrupt shutdown of system.

All of these queues provide better performance than other end points because no camel message transformation is performed by the end point.

Despite having multiple similarities, the purpose of each and every end point is different.The exact nature of difference can be noticed from the following matrix -

With In Same Camel Context
Across Camel contexts in a Single JVM

Synchronous communication refers to the communication, where the sender sends the message and proceed ahead only when response is retrieved back. In this case, all further processing happens on the received message.

Asynchronous communication refers to the kind of communication, where sender sends a copy of message to the receiver and proceeds without waiting for the processing at receiver to be completed.

In the following example, I will show you the behavior of direct end point and let you use the same example to test and understand the behavior of seda, direct-vm and vm end points. In case of any difficulty, you can download the complete project with the example of all of the end points from the following git hub location.-

Technology used -
java version "1.8.0_73"
Apache Camel 2.16.2
Apache Maven 3.0.4

pom.xml -
<project xmlns="" xmlns:xsi=""
Project Structure
package com.salil.camelawareendpoints.router;

import org.apache.camel.builder.RouteBuilder;

public class DirectEndPointRouter extends RouteBuilder  {

 public static final String DIRECT_END = "direct:End";
 public static final String DIRECT_START = "direct:start";

 public void configure() throws Exception {
  .setBody().simple("Start Message")
  .log("Message at start route completion = ${body}");
  .setBody().simple("End Message")
        .log("message after end-route completion = ${body}");

package com.salil.camelawareendpoints.ContextExample;

import com.salil.camelawareendpoints.router.DirectEndPointRouter;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.impl.DefaultCamelContext;

 * Created by Salil on 29-02-2016.
public class DirectEndPointBehaviorExample {

    public static void main(String[] args) throws Exception {
        CamelContext camelContext = new DefaultCamelContext();
        camelContext.addRoutes(new DirectEndPointRouter());

        ProducerTemplate producerTemplate=camelContext.createProducerTemplate();
        producerTemplate.sendBody(DirectEndPointRouter.DIRECT_START, "Initial Message");

Execution output-
427 [main] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.16.2 (CamelContext: camel-1) is starting
429 [main] INFO - JMX is enabled
843 [main] INFO org.apache.camel.impl.converter.DefaultTypeConverter - Loaded 182 type converters
973 [main] INFO org.apache.camel.impl.DefaultRuntimeEndpointRegistry - Runtime endpoint registry is in extended mode gathering usage statistics of all incoming and outgoing endpoints (cache limit: 1000)
1280 [main] INFO org.apache.camel.impl.DefaultCamelContext - AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
1280 [main] INFO org.apache.camel.impl.DefaultCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at
1441 [main] INFO org.apache.camel.impl.DefaultCamelContext - Route: StartRouteId started and consuming from: Endpoint[direct://start]
1451 [main] INFO org.apache.camel.impl.DefaultCamelContext - Route: EndRouteId started and consuming from: Endpoint[direct://End]
1451 [main] INFO org.apache.camel.impl.DefaultCamelContext - Total 2 routes, of which 2 is started.
1459 [main] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.16.2 (CamelContext: camel-1) started in 1.031 seconds
1501 [main] INFO EndRouteId - message after end-route completion = End Message
1501 [main] INFO StartRouteId - Message at start route completion = End Message

In this case, we can notice following things
1- The output of end route got printed before the completion of start route
2- Both start route and end route had the same message in the body of the message at the time of printing
3- All the execution happens on the main thread only

This behavior appears because of synchronous nature of direct end point, where at the call of '.to(DIRECT_END)', the execution of first route stopped & it resumed again on receiving of the message with body 'End Message'

Let us know whether you could understand the behavior of seda, direct-vm and vm points, or faced any difficulty even after executing the examples at following git hub location

No comments:

Post a Comment