简体   繁体   English

关于文件类型的Apache-Camel选择

[英]Apache-Camel Choice on Filetype

The Code listed below works just fine, but it's function is to look into an XML file and if the field is 'us' to move it to another directory; 下面列出的代码可以很好地工作,但是它的功能是查看XML文件,如果该字段是'us',则将其移动到另一个目录。 what I would like to know about using the .choice() function: 我想知道有关使用.choice()函数的什么:

1) How do I specify a specific file to be routed? 1)如何指定要路由的特定文件? (Adding the filename to the end of the path did not work) (将文件名添加到路径末尾不起作用)

2) How do I specify a filetype to be routed? 2)如何指定要路由的文件类型? (Ex: route all .txt files to "blah") (例如:将所有.txt文件路由到“等等”)

3) Are there other options besides using .choice that will help me to do this? 3)除了使用.choice以外,还有其他选择可以帮助我做到这一点吗?

CamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder()
    {
        public void configure() throws Exception
        {
            from("file:C:\\camels\\inner?delete=true")
                .choice()
                    .when(xpath("/author/country = 'us'"))
                        .to("file:C:\\testing");
        }
    });
    context.start();
    Thread.sleep(10000);
    context.stop();

Here are some of the ways to do this 这是一些这样做的方法

  • Have a look at http://camel.apache.org/file-language.html for file languages exposed by camel this provides some option you can use to get file name with extension , file only name, file parent file extension etc.. 看看http://camel.apache.org/file-language.html,了解骆驼公开的文件语言,这提供了一些选项,可用于获取扩展名,仅文件名,父文件扩展名文件名

  • Also look at the include option at http://camel.apache.org/file2.html , this will help to poll out only the files with filename matching a regex pattern. 还要查看http://camel.apache.org/file2.html上的include选项,这将有助于仅轮询文件名与正则表达式模式匹配的文件。

     from("file:C:\\\\camels\\\\inner?delete=true&include=abc") 
  • Build a Predicate and use it like below: 建立一个谓词并按如下方式使用它:

     CamelContext context = new DefaultCamelContext(); context.addRoutes(new RouteBuilder() { Predicate predicate = PredicateBuilder.and(simple("${file:name.ext} == 'txt'"), XPathBuilder.xpath("/author/country = 'us'")); public void configure() throws Exception { from("file:C:\\\\camels\\\\inner?delete=true") .choice() .when(predicate) .to("file:C:\\\\testing"); } }); context.start(); Thread.sleep(10000); context.stop(); 

Answering first two points of your question: It is possible - take a look at documentation . 回答您问题的前两点:可能-看一下文档 Parameter called fileName is well described there, and it allows you to control file names and extensions (actually part of name) very strictly. 在那里很好地描述了名为fileName的参数,它使您可以非常严格地控制文件名和扩展名(实际上是名称的一部分)。

When it comes to point number 3, then if your "choice" is about only one case, then I'd like recomend using filter component rather then choice. 关于第3点,如果您的“选择”只是一种情况,那么我想建议您使用过滤器组件而不是选择。

It will allow you easily model route where for example *xml is routed to your "testing" directory when it indeed is XML and contains country "us". 这将使您可以轻松地对路由进行建模,例如,当* xml确实为XML且包含国家(地区)“ us”时,会将* xml路由至“测试”目录。 Note that catching some exceptions might be necessary when faulty file (non XML) will appear ;). 请注意,当出现错误文件(非XML)时,可能需要捕获一些异常;)。

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

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