使用pollEnrich()从表中读取文件位置并复制到特定文件夹

时间:2016-02-08 23:06:08

标签: apache-camel camel-sql

我正在尝试编写一个读取数据库表的camel路由来获取绝对文件路径列表,然后将这些文件复制到另一个文件夹。但是,仅将文件路径创建为内容而不是原始内容。

    from("timer://testDataGen?repeatCount=1")
    .to("sql:" + positionSql + "?dataSource=dataSource")
    .split(body())
    .to("file://" + positionlistDir )
    .log("Finished copying the list of Files.")

请让我知道我在这里缺少什么来将绝对文件路径转换为实际文件。

更新#1。

在代码片段下面调用pollEnrich()。但是,pollEnrich()正在复制文件的数量,该数量等于sql返回的行数,而不是根据前一个交换的文件名。

        String positionListSqlOptions = "?dataSource=dataSource";
//      String positionSrcDirOptions = "?noop=true&delay=500&readLockMarkerFile=false&fileName=${header.positionFileToBeCopied}";
        String positionSrcDirOptions = "?noop=true&delay=500&readLockMarkerFile=false&fileName=${body}";
        String positionStagingDirOptionsForWriting = "?doneFileName=${file:name}.DONE";

        from("timer://testDataGen?repeatCount=1")
        .to("sql:" + positionListSql + positionListSqlOptions)
        .split(body())
        \\ Getting the column value from the resultset which is a LinkedCaseInsensitiveMap and storing in the body
        .process(new positionFeederProcessor()) 
        .setHeader("positionFileToBeCopied", body())
        .pollEnrich("file://" + positionSrcDir + positionSrcDirOptions)
//      .pollEnrich().simple("file://" + positionSrcDir + positionSrcDirOptions)
        .to("file://" + positionStagingDir + positionStagingDirOptionsForWriting)
        .log("Finished copying the list of Files.");

我仍然无法将实际文件名传递给pollingEnrich()端点。我尝试从身体以及通过标题提取它。什么可能出错。

2 个答案:

答案 0 :(得分:1)

好吧,最后我能够在不使用pollEnrich()的情况下完成此任务。

    String positionListSqlOptions = "?dataSource=dataSource";
    String positionSrcDirOptions = "?noop=true&delay=500&readLockMarkerFile=false&fileName=${header.CamelFileName}";
    String positionStagingDirOptionsForWriting = "?fileName=${header.position.file.name}&doneFileName=${file:name}.DONE";

    from("timer://testDataGen?repeatCount=1")
    .to("sql:" + positionListSql + positionListSqlOptions)
    .routeId("Copier:")
    .setHeader("positionFileList", body())
    .log("Creating the list of position Files ...")
    .split(body())
    .process(new PositionListProcessor())
    .setHeader("position.file.name", body())
    .setHeader("position.dir.name", constant(positionSrcDir))
    .process(new PositionFileProcessor())
    .choice()
        .when(body().isNull())
            .log("Position File not found. ${header.position.file.name}")
        .otherwise()
            .to("file://" + positionStagingDir + positionStagingDirOptionsForWriting)
            .log("Position File Copied from Src to : " + "${header.CamelFileNameProduced} ... ${headers} ...");

以下是处理器。

public class PositionListProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
    LinkedCaseInsensitiveMap positionFilesResultSet = (LinkedCaseInsensitiveMap) exchange.getIn().getBody();
    try {
        String positionFileStr = positionFilesResultSet.get("PF_LOCATION_NEW").toString();
        }
        exchange.getOut().setBody(positionFileStr.trim());
    } catch (Exception e) {     }
} }




public class PositionFileProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
    String filename = exchange.getIn().getBody(String.class);
    String filePath = exchange.getIn().getHeader("position.dir.name", String.class);
    URI uri = new URI("file:///".concat(filePath.concat(filename)));
    File file = new File(uri);
    if (!file.exists()) {
            logger.debug((String.format("File %s not found on %s", filename, filePath)));
        exchange.getIn().setBody(null);
    } else {
        exchange.getIn().setBody(file);
    }
} }

答案 1 :(得分:0)

file组件在to定义上使用时,会生成一个包含交换内容的文件,但它不会读取文件。您可以使用例如pollEnrich处理器:

from("timer://testDataGen?repeatCount=1")
    .to("sql:" + positionSql + "?dataSource=dataSource")
    .split(body())
    .pollEnrich().simple("file:folder?fileName=${body}")
    .to("file://" + positionlistDir )
    .log("Finished copying the list of Files.")