Saturday 29 December 2018

Apache Camel: Exploring Processor interface

Processor is used to process the message exchange.

How to define custom processor?
You can define the custom processor by implementing 'org.apache.camel.Processor' interface.

public interface Processor {
    void process(Exchange exchange) throws Exception;
}


Let’s create a custom processor, that logs the message.

public class LogProcessor implements Processor {

         @Override
         public void process(Exchange exchange) throws Exception {
                  String message = exchange.getIn().getBody(String.class);

                  System.out.println("****Reading the message from exchnage****");
                  System.out.println(message);
         }

}

You can insert the processor into the route.
from("SourceURL").process(myProc).to("TargetURL");

Example
from("file:C:\\Users\\Public\\demo?noop=true").process(new LogProcessor()).to("file:C:\\Users\\Public\\demoCopy");

Find the below working application.

LogProcessor.java
package com.sample.app.processor;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public class LogProcessor implements Processor {

 @Override
 public void process(Exchange exchange) throws Exception {
  String message = exchange.getIn().getBody(String.class);

  System.out.println("****Reading the message from exchnage****");
  System.out.println(message);
 }

}

Application.java

package com.sample.app;

import java.util.concurrent.TimeUnit;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

import com.sample.app.processor.LogProcessor;

public class Application {

 public static class FileCopyRoute extends RouteBuilder {

  @Override
  public void configure() throws Exception {
   from("file:C:\\Users\\Public\\demo?noop=true").process(new LogProcessor())
     .to("file:C:\\Users\\Public\\demoCopy");
  }

 }

 public static void main(String args[]) throws Exception {
  CamelContext context = new DefaultCamelContext();

  context.addRoutes(new FileCopyRoute());

  context.start();

  TimeUnit.MINUTES.sleep(1);

  context.stop();
 }
}


Previous                                                 Next                                                 Home

No comments:

Post a Comment