简体   繁体   English

Apache-Camel中的文件监听器

[英]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. 我想用Java编写骆驼路线,该路线不断检查文件的文件夹,然后将它们发送到处理器。

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. 提前致谢。

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 看看可用的骆驼Maven原型: http://camel.apache.org/camel-maven-archetypes.html : http://camel.apache.org/camel-maven-archetypes.html ,其中camel-archetype-java反映了您的情况

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM