Searching...
Saturday, 2 November 2013

Camel Interceptor Simple Example

06:08

Apache camel provides intercepting feature while Exchanges are on route. Camel supports three types for interceptors (See more about Camel interceptors )

In this example we will see how to add interceptor in a simple route and that is executing among routes.

1. Create a Maven Project

Create a maven project with following dependencies

   <dependencies>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
      <version>2.10.3</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-spring</artifactId>
      <version>2.10.3</version>
    </dependency>
    <dependency>
 <groupId>org.apache.activemq</groupId>
 <artifactId>activemq-core</artifactId>
 <version>5.1.0</version>
    </dependency>
    <dependency>
 <groupId>org.apache.activemq</groupId>
 <artifactId>activemq-camel</artifactId>
 <version>5.8.0</version>
</dependency>
        <dependency>
 <groupId>org.apache.activemq</groupId>
 <artifactId>activemq-pool</artifactId>
 <version>5.4.1</version>
</dependency>
    <!-- logging -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.5</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.5</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
  </dependencies>

2. Create a Router

package com.pretech;
import org.apache.camel.builder.RouteBuilder;
/**
 * A Camel Java DSL Router
 */
public class MyRouteBuilder extends RouteBuilder {
 /**
  * Let's configure the Camel routing rules using Java code...
  */
 public void configure() {
  this.intercept().process(new InterceptorProcessor());
  from("file:input?noop=true").process(new ProcessorOne())
    .process(new ProcessorTwo()).process(new ProcessorThree());
 }
}

3. Create Processors


InterceptorProcessor.java

package com.pretech;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class InterceptorProcessor implements Processor  {
 @Override
 public void process(Exchange exchange) throws Exception {
  System.out.println("Interceptor executing");
  
 }
}

ProcessorOne.java

package com.pretech;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class ProcessorOne implements Processor  {
 @Override
 public void process(Exchange exchange) throws Exception {
  System.out.println("Processor one executing");
  
 }
}

Add ProcessorTwo and ProcessorThree as same as above processor


4 Main Program to start Camel (MainApp.java)

package com.pretech;
import org.apache.camel.main.Main;
/**
 * A Camel Application
 */
public class MainApp {
    /**
     * A main() so we can easily run these routing rules in our IDE
     */
    public static void main(String... args) throws Exception {
        Main main = new Main();
        main.enableHangupSupport();
        main.addRouteBuilder(new MyRouteBuilder());
        main.run(args);
     
     
    }
}

Final Structure


image


In this example input folder is starting end point so put some sample input files in input folder and run the main program. We can see interceptor is running among each processors.


Output



Interceptor executing
Processor one executing
Interceptor executing
Processor two executing
Interceptor executing
Processor Three executing


0 comments: