2017-10-08 147 views
0

嗨,我想要使用apache駱駝計算動態輸出路由。我在文件夾位置收到一堆文件,根據其內容我想將文件移動到動態輸出文件夾。輸出文件夾的名稱將根據文件的輸入內容構建。我如何實現它。動態Apache駱駝輸出路由

下面這段代碼讀取文件,處理它們,但我不知道如何設置的$ {文件夾名}基於文件的內容價值

from("file:D:\\camel\\input\\one?recursive=true&delete=true") 
      .process(new LogProcessor()) 
      .to("file:D:\\camel\\output\\${foldername}") 

請協助

回答

2

您可以創建自定義處理器來構建文件夾名稱並將其插入到標題中。

public class DirectoryNameProcessor implements Processor { 
    @Override 
    public void process(Exchange exchange) { 
     Message in = exchange.getIn(); 
     // Get the contents of the processed file 
     String body = in.getBody(String.class); 
     //Get the original file name 
     String fileName = in.getHeader("CamelFileName", String.class); 
     // Perform your logic 
     in.setHeader("foldername"); 
    } 
} 

然後在你的路線,你可以訪問新創建的文件夾名頭:

.to("file:D:\\camel\\output\\${header.foldername}"); 
+0

還是有返回一個字符串,比如'$ {} my.getFolderName一個POJO方法' – Namphibian

+0

謝謝。它工作 – onlinejava

+0

哎呀抱歉,您提供的代碼不起作用,我必須使用recipientList(header(「foldername」)),而不是to命令 – onlinejava