Using a Camel Processor to modify the message

The Camel Processor is a key building block of Camel, and gives you full access to the message being transferred including the message body, any headers, and any properties associated with the message. By creating a Processor you can modify any of those elements, for example you can alter the body of the message transforming it from XML to CSV or you can add custom properties or headers to it. Let’s see how to build a custom Processor which transforms the body of a Message.

camel processor

In the following class, we are creating a custom Processor named myProcessor which receives as input the files contained in /var/data/in and format them as CSV files. The files are then transferred to /vr/data/out :

package com.sample;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;

public class ProcessorExample {

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        main.addRouteBuilder(new MyRoute());;

class MyRoute extends RouteBuilder {

    public void configure() throws Exception {
        System.out.println("My Routing Started");
        from("file:/var/data/in?noop=true").process(new MyProcessor()).to(
        System.out.println("My Routing complete");

And here is the implementation of MyProcessor:

package com.sample;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
public class MyProcessor implements Processor {

    public void process(Exchange exchange) throws Exception {

        String myString = exchange.getIn().getBody(String.class);
        String[] myArray = myString.split(System.getProperty("line.separator"));
        StringBuffer sb = new StringBuffer();
        for (String s : myArray) {
        System.out.println("MyProcessor complete");

    public MyProcessor() {


On the other hand, if you prefer using Spring DSL, then you can achieve the same result using the following XML file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="">
   <bean id="myProcessor" class="com.sample.MyProcessor" />
   <camelContext xmlns="">
         <from uri="file:source?noop=true" />
         <process ref="myProcessor" />
         <to uri="file:dest?fileName=output.csv" />