camel in-memory messaging example

In-Memory Messaging Example In camel With Simple Steps.

In our example we will learn about in-memory messaging in camel.Camel provides Direct, SEDA, VM for in-memory messaging.Among them Direct is synchronous and others are asynchronous.In our example we will learn direct component.We used in-memory messaging when we want to send message internally and wants to process and after that at the end we will send to outer component.In our example we will read from file and send it to our direct endpoint and direct will sent it to console.

  • In eclipse go to MyFirstCamelProject that we created in earlier page and right click on src/main/java to create package.New -> package

  • Give any name for example com.preparationforinterview and click on Finish

  • Right click on package and create a java class for example DirectInMemoryExample.

  • Right click on package and create one more java class for example Executor.

  • Create a folder with name input.At the end we will get following structure

  • Create any file in input folder.I created text file with name Hello.

  • Add following dependency in pom.xml can use any latest version.

    <project xmlns="" xmlns:xsi=""
  • Run mvn:install from command prompt or if you are using Eclipse right click on pom.xml and run mvn install.It will download camel jars

  • Copy below code in DirectInMemoryExample.

    	package com.preparationforinterview;
    import org.apache.camel.builder.RouteBuilder;
    public class DirectInMemoryExample extends RouteBuilder {
    	public void configure() throws Exception {


    To create a route in camel we need to extend RouteBuilder class and override configure method.In configure method we define our route.In above example we can read it as from input folder take file and send it to in-memory component .we don't need to configure any option for direct component.As direct endpoint receives message ,due to second route it sends it to console.


    Here we gave noop=true as parameter. It is optional parameter.Using that we can instruct camel to keep that input file as is.If we don't use this option then file will be moved, it means cut from input and paste in output

  • Copy below code in Executor.

    	package com.preparationforinterview;
    import org.apache.camel.CamelContext;
    import org.apache.camel.impl.DefaultCamelContext;
    public class Executor {
    	public static void main(String args[]) throws Exception{
    		CamelContext ctx = new DefaultCamelContext();
    		ctx.addRoutes(new DirectInMemoryExample());


    First we need to create CamelContext. It has many implementation but we are using DefaultCamelContext.After CamelContext is created we add route.Its like registry of route CamelContext, till that part we have just configured route in CamelContext but until and unless we start CamelContext it will not execute route.We kept Thread.sleep(10000) so that camel gets enough time to process route.

    To check the output right click on and run it.It will take file from input and print on console
    To run the program from command prompt,open command prompt and go to project location and run below command.
    mvn compile exec:java -Dexec.mainClass=com.preparationforinterview.Executor
  • After execution we get following output on console.

Download source code

Camel In-Memory Messaging Example

Visit Others Links

Camel Introduction
Camel Setup
File Copy Example Using Camel
File Copy Example In Camel Using Spring
JMS Message Transfer Example Using Camel
JMS Message Transfer Example In Camel Using Spring
Processor In Camel
Object to JSON Transfer Example Using Spring In Camel
Camel With Bean Injection Example Using Spring DSL
Camel With Bean Injection Registry Example Using Java DSL
Camel Bean Parameter Binding Example Using Java DSL
Camel Error Handling With Redelivery Example Using Java DSL
Camel Exception Handling With Example Using Java DSL
Camel JUnit Test With Example Using Java DSL
Camel JUnit Test With Example Using Spring DSL
Camel Unit Test With Mock Component Example
Camel With Stream Component Example
Camel In-Memory Messaging Example Using Direct
Camel With Timer And Quartz Example
Camel Aggregator EIP Example
Camel Splitter EIP Example
Camel Routing Slip EIP Example
Camel Dynamic Router EIP Example
Camel Load Balancer EIP Example
Top Camel Interview Question
© 2019