Loading
 
What's Next: View All Tutorial →

Apache Camel - Hello World Program - File Transfer

In this article, you will learn how to write a simple Apache Camle Helloworld program and the project setup. Create a basic maven java project   Add Maven Dependency <dependency> <groupId>org.apache.camel</...

Published Date: 03/05/2015  Last Modified Date: 03/05/2015    New Post



In this article, you will learn how to write a simple Apache Camle Helloworld program and the project setup.

Create a basic maven java project
 

Add Maven Dependency

 <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>2.15.1</version>
        </dependency>


Camel Program:

 

package com.mycompany.camel;

import java.io.File;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;


public class CamelHelloWorld {

    public static void main(String args[]) throws Exception {

        doFileSetup();
        CamelContext context = new DefaultCamelContext();
        context.addRoutes(new RouteBuilder() {

            @Override
            public void configure() throws Exception {

                //To move all file from sourceLocation folde to destLocation folder from c drive
                from("file:C:\\sourceLocation?noop=true").process(new Processor() {

                    @Override
                    public void process(Exchange arg0) throws Exception {
                        System.out.println("hello camel!");
                    }
                }).
                        to("file:C:\\destLocation").end();
            }
        });
        context.start();
        Thread.sleep(3000);
        context.stop();
    }

    public static void doFileSetup() throws Exception {
        File f = new File("c:\\sourceLocation");
        f.delete();
        f.mkdir();
        File fs1 = new File("c:\\sourceLocation\\text1.txt");
        fs1.delete();
        fs1.createNewFile();
        File f1 = new File("c:\\destLocation");
        f1.delete();
        f1.mkdir();
    }

}


Description:
This program listen for the file from the "c:/sourceLocation" folder, whenever the file is available, the file will be moved to "c:/destLocation" folder.

Keywords to know

DefaultCamelContext - To define the camelContext class, We can also define it throught the Spring xml configuration

RouteBuilder - The RouteBuilder is a base class which is derived from to create routing rules using the DSL. Instances of RouteBuilder are then added to the CamelContext.

from()  - from() is the starting point of the router

to() - to() is the destination place for the router

.process(new Processor() { - process is the method to process the data sent from "from()" to "to(). process store the messaage in the Exchange object. The Exchange Object has header, body, ExchangeInHeader and ExchangeOutHeaders


 
Awaiting for Administrator approval





Tags: Basic

← Back to list


Related Post




×