Camel PollingConsumer - 删除孤立锁文件,但不删除原始文件

时间:2014-06-27 18:27:03

标签: apache-camel apache-servicemix

我有一个简单的路由,即以.fin结尾的文件,其中包含我必须发送到ftp服务器的文件的名称。这是路线:

<route id="sendToFtp">
    <from uri="file:{{tmp.files.location}}/export/pr?delete=true&amp;include=.*.txt.fin"/>
    <process ref="getFileProcess" />
    <log message="Sending message ${file:name}"/>
    <setHeader headerName="CamelFileName">
        <simple>${file:name.noext}</simple>
    </setHeader>
    <delay>
        <constant>10000</constant>
    </delay>
    <to uri="{{export.feed.ftp}}{{export.feed.ftp.folder}}?username={{export.feed.ftp.username}}&amp;password={{export.feed.ftp.password}}&amp;passiveMode=true&amp;connectTimeout={{feed.interval}}&amp;timeout={{feed.interval}}&amp;soTimeout={{feed.interval}}&amp;disconnect=true" />
</route>

我在文件中使用轮询使用者来检索要发送到ftp的本地文件。这是过程:

@Override
public void process(Exchange exchange) throws Exception {
    final String filename = exchange.getIn().getBody(String.class);

    Endpoint endpoint = exchange.getContext().getEndpoint("file:{{powerreviews.tmp.files.location}}/export/pr?delete=true&fileName="+filename);

    PollingConsumer consumer = endpoint.createPollingConsumer();

    consumer.start();
    Exchange ex = consumer.receive(60000);

    if (ex==null){
        exchange.getIn().setBody("");
    }else {
        exchange.getIn().setBody(ex.getIn().getBody());
    }
    consumer.stop();
}

当我执行路由时,似乎交换在路由结束后没有关闭导致我的文件.fin和我使用pollingConsume消耗的那个没有被删除,当我明确地在端点中有delete = true参数。无论如何,文件被正确发送到ftp。

在日志中我有以下内容:

2014-06-27 20:23:03,765 | WARN  | roduct/export/pr | kerFileExclusiveReadLockStrategy | 100 - org.apache.camel.camel-core - 2.10.7 | Deleting orphaned lock file: tmp/product/export/pr/the.txt.fin.camelLock
2014-06-27 20:28:08,041 | WARN  | roduct/export/pr | kerFileExclusiveReadLockStrategy | 100 - org.apache.camel.camel-core - 2.10.7 | Deleting orphaned lock file: tmp/product/export/pr/the.txt.camelLock

1 个答案:

答案 0 :(得分:2)

不会删除该文件,因为未执行返回的消费者模板Exchange ex上的onCompletion。 javadoc文档更详细地记录了这一点。

但是,由于您希望稍后删除过滤器,因此您可以先将其上传到FTP服务器,您需要将onCompletions从Exchange ex切换到Exchange exchange,因此最后添加此代码那种方法

    }
    consumer.stop();

    // handover from me to the incoming exchange
    // so we will delete the file at the end of the route
    ex.handoverCompletions(exchange);
}