Apache-Camel中的文件侦听器

时间:2015-02-09 12:42:36

标签: java apache-camel

我想在Java中编写一个Camel Route,它不断检查文件夹,然后将它们发送到处理器。

我这样做的方式对我来说似乎很“脏”:

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
        }

有没有更好的方法来实现它?

提前致谢。

2 个答案:

答案 0 :(得分:1)

您还可以将文件处理移至专用类:

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
    }
}

然后按如下方式使用它:

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

查看可用的驼峰式原型:http://camel.apache.org/camel-maven-archetypes.html其中 camel-archetype-java 反映您的情况

答案 1 :(得分:0)

这可能是一种更清洁的方法:

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();
}