HAG
HAG

Reputation: 3

File Listener in Apache-Camel

I want to program a Camel Route in Java that is constantly checking a Folder for Files, and then send them to a Processor.

The way i do it know seems quite "dirty" to me:

from( "file:C:\\exampleSource" ).process( new Processor()
                {
                    @Override
                    public void process( Exchange msg )
                    {
                        File file = msg.getIn().getBody( File.class );
                        Filecheck( file );
                    }
                } );

            }
        } );
        camelContext.start();
        while ( true )
        {
            // run
        }

Is there a better way to implement that?

Thanks in advance.

Upvotes: 0

Views: 1304

Answers (2)

cslysy
cslysy

Reputation: 830

You can also move you file processing to dedicated class:

import java.io.File;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public class FileProcessor implements Processor {

    @Override
    public void process(Exchange exchange) throws Exception {
        File file = exchange.getIn().getBody(File.class);
        processFile(file);
    }

    private void processFile(File file) {
        //TODO process file
    }
}

And then use it as follows:

from("file:C:\\exampleSource").process(new FileProcessor());

Take a look on available camel maven archetypes: http://camel.apache.org/camel-maven-archetypes.html where camel-archetype-java reflects your case

Upvotes: 2

vikingsteve
vikingsteve

Reputation: 40398

Here is a perhaps cleaner way to do it:

public static void main(String[] args) throws Exception {
    Main camelMain = new Main();
    camelMain.addRouteBuilder(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("file:C:\\xyz")
                    // do whatever
            ;
        }
    });
    camelMain.run();
}

Upvotes: 0

Related Questions