EIP Patterns in Apache Camel using examples | JavaInUse






EIP Patterns in Apache Camel using examples

In this tutorial we will try to understand the Enterprise Integration Patterns using Camel Examples. In previous tutorial
have explained a simple example for integrating Apache camel and ActiveMQ where we created SimpleRouteBuilder.java and configure routes. Here will create multiple RouteBuilder classes to configure Routes according to the various EIP patterns

Apache Camel - Table of Contents

File Transfer Using Java DSL Apache Camel Apache Camel Java DSL + Spring Integration Hello World Example Apache Camel Exception Handling Using Simple Example Apache Camel Redelivery policy using example Integrate Apache Camel and ActiveMQ EIP patterns using Apache Camel Apache Camel Tutorial- Integrate Spring Boot+ Apache Camel Apache Camel Tutorial- Integrate with MySQL DB using SQL query Apache Camel + Spring + ActiveMQ + JBoss Fuse Apache Camel EIP - Splitter and Aggregator pattern Apache Camel Unit Testing Apache Camel + Spring + Quartz Hello World Example Camel application deployment on JBoss Fuse Apache Camel + Apache CXF SOAP Webservices Apache Camel + JAX-RS REST Webservice Apache Camel + CXFRS REST Webservice Apache Camel Routing Slip Pattern Apache Camel Dynamic Router Pattern Apache Camel Load Balancer EIP Pattern Apache Camel Interceptors Apache Camel + Kafka Hello World Example Apache Camel - Marshalling/Unmarshalling XML/JSON Data Example

Video

This tutorial is explained in the below Youtube Video.

Lets Begin

  • EIP- Splitter
    The Splitter from the EIP patterns allows users split a message into a number of pieces and process them individually.

    Apache Camel Splitter EIP Pattern

    package com.javainuse;
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class SimpleRouteBuilder extends RouteBuilder {
    //Split the content of the file into lines and the process it 
        @Override
        public void configure() throws Exception {
            from("file:C:/inputFolder").split().tokenize("\n").to("jms:queue:javainuse");
        }
    
    }
    
  • EIP- Content Based Router
    The Content-Based Router inspects the content of a message and routes it to another channel based on the content of the message. Using such a router enables the message producer to send messages to a single channel and leave it to the Content-Based Router to inspect messages and route them to the proper destination. This alleviates the sending application from this task and avoids coupling the message producer to specific destination channels.
    Apache Camel EIP Patterns Tutorial

    package com.javainuse;
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class SimpleRouteBuilder extends RouteBuilder {
    
        @Override
        public void configure() throws Exception {
            from("file:C:/inputFolder").split().tokenize("\n").to("direct:test");
            
    //Content Based routing- Route the message based on the token it contains.
            from("direct:test"). 
            choice().
            when(body().contains("javainuse1"))
            .to("jms:queue:javainuse1").
            when(body().contains("javainuse2"))
            .to("jms:queue:javainuse2")
            .when(body().contains("javainuse3"))
            .to("jms:queue:javainuse3").
            otherwise().
            to("jms:queue:otherwise");
           
        }
    
    }
    }
    
  • EIP- Message Filter
    A Message Filter is a special form of a Content-Based Router. It examines the message content and passes the message to another channel if the message content matches certain criteria. Otherwise, it discards the message.
    Apache Camel Message Filter EIP Pattern

    package com.javainuse;
    
    import org.apache.camel.builder.RouteBuilder;
    
    public class SimpleRouteBuilder extends RouteBuilder {
    
        @Override
        public void configure() throws Exception {
            from("file:C:/inputFolder").split().tokenize("\n").to("direct:test");
            
            //Message Filter is a type of Content Based routing. 
            //If condition satisfied perform a task else discard it.
            from("direct:test"). 
            filter(body().contains("javainuse1"))
            .to("jms:queue:javainuse1");
           
        }
    
    }
    
  • Recipient List
    A Content-Based Router allows us to route a message to the correct system based on message content. This process is transparent to the original sender in the sense that the originator simply sends the message to a channel, where the router picks it up and takes care of everything.
    Apache Camel Recipient List EIP Pattern

    package com.javainuse;
    
    import org.apache.camel.Exchange;
    import org.apache.camel.Processor;
    import org.apache.camel.builder.RouteBuilder;
    
    public class SimpleRouteBuilder extends RouteBuilder {
    
        @Override
        public void configure() throws Exception {
            from("file:C:/inputFolder").split().tokenize("\n").to("direct:test");
            
    //Recipient List- Dynamically set the recipients of the exchange 
             //by creating the queue name at runtime
            from("direct:test")
            .process(new Processor() {
                public void process(Exchange exchange) throws Exception {
                   String recipient = exchange.getIn().getBody().toString();
                   String recipientQueue="jms:queue:"+recipient;
                   exchange.getIn().setHeader("queue", recipientQueue);
          }
          }).recipientList(header("queue"));
           
        }
    
    }
       
  • Wire Tap
    Wire Tap allows you to route messages to a separate location while they are being forwarded to the ultimate destination.
    Apache Camel Wire Tap EIP Pattern

    package com.javainuse;
    
    import org.apache.camel.Exchange;
    import org.apache.camel.Processor;
    import org.apache.camel.builder.RouteBuilder;
    
    public class SimpleRouteBuilder extends RouteBuilder {
    
        @Override
        public void configure() throws Exception {
            from("file:C:/inputFolder").split().tokenize("\n").to("direct:test1");
            
            from("direct:test1")
            //Wire Tap:Suppose get some error so send seperate copies of the message to 
            //DeadLetter queue and also to direct:test2 
            .wireTap("jms:queue:DeadLetterQueue")
            .to("direct:test2");
            
            from("direct:test2")
            .process(new Processor() {
                public void process(Exchange arg0) throws Exception {
                  //Some logic here
                }
          });
        }
    }
    

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions