简体   繁体   English

Apache Camel:带计划程序的FTP文件(目录为空时关闭连接)

[英]Apache Camel: FTP files with scheduler (close connection when directory is empty)

I would like to use Apache Camel for sending files from a local directory to a FTP location. 我想使用Apache Camel将文件从本地目录发送到FTP位置。 We only get 1 or 2 times a day files in that local directory. 每天在该本地目录中仅获得1或2次文件。 So it isn't necessary to have that connection open the whole day. 因此,不必全天打开该连接。 What I would like to achieve is that we will open the connection when there are files in the directory (this is something we could do by checking the dir), but how could we check if sending the files with Apache Camel is done and close the connection? 我要实现的是,当目录中有文件时,我们将打开连接(这可以通过检查dir来完成),但是我们如何检查使用Apache Camel发送文件是否完成并关闭连接?

Is there some feedback from the RouteBuilder or what is the best way to achieve this? 是否有来自RouteBuilder的反馈,或者最好的方法是什么?

Thanks 谢谢

You can use the disconnect=true option to close the connection after sending is complete. 发送完成后,可以使用disconnect=true选项关闭连接。

See the documentation at: http://camel.apache.org/ftp2 请参阅以下文档: http : //camel.apache.org/ftp2

I solved the issue by using the FTPToClientRouteBuilder which is called in the following method: 我通过使用FTPToClientRouteBuilder(在以下方法中调用)解决了该问题:

@Override
public void sentFilesViaFTPToClient() {

    CamelContext context = new DefaultCamelContext();

    try {
        context.addRoutes(new FTPToClientRouteBuilder());
        context.start();

        while (true) {
            if (CollectionUtils.isEmpty(context.getRoutes())) {
                LOG.info("Finished sending files via FTP.");
                context.stop();
                break;
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Couldn't FTP files", e);
    } finally {
        try {
            context.stop();
        } catch (Exception e) {
            //ignore exception
        }
    }
}

FTPToClientRouteBuilder FTPToClientRouteBuilder

public class FTPToClientRouteBuilder extends RouteBuilder {

private static final Logger LOG = LoggerFactory.getLogger(FTPToClientRouteBuilder.class);

private String ftpLocation;
private String uploadLocation = "file:" + location + "?delete=true&moveFailed=../error&sendEmptyMessageWhenIdle=true";

@Override
public void configure() throws Exception {

    // set graceful shutdown timeout
    getContext().getShutdownStrategy().setTimeout(10);

    from(uploadLocation)
        .choice()
            .when(body().isNotNull())
            .log("Uploading file ${file:name}")
            .to(ftpLocation)
            .log("Uploaded file ${file:name} complete.")
        .otherwise()
        .process(new ShutdownProcessor());
}

private class ShutdownProcessor implements Processor {

    @Override
    public void process(Exchange exchange) throws Exception {
        new Thread() {
            @Override
            public void run() {
                try {
                    exchange.getContext().stop();
                } catch (Exception e) {
                    LOG.error("Couldn't stop the route", e);
                }
            }
        }.start();
    }
}
}

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

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