1

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

    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() 端点。我也尝试从正文以及标题中提取它。可能出了什么问题。

4

2 回答 2

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);
    }
} }
于 2016-02-11T23:07:19.783 回答
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.")
于 2016-02-09T07:13:54.183 回答