使用Java DSL成功进行ftp传输后移动文件

时间:2015-08-04 20:57:25

标签: java spring ftp spring-integration

我已经使用spring-integration java dsl定义了一个流程来ftp传输一个文件,处理它,然后将它传回一个" archive" dir,最后将它移到本地存档目录中。 这是什么"很容易"为:

@Bean
public IntegrationFlow ftpInboundFlow() {
    if (ftpProperties.getEnabled() == false) {
        return null;
    }
    logger.trace("Starting ftp flow");
    return IntegrationFlows
            .from(source -> source.ftp(ftpSessionFactory()).deleteRemoteFiles(true).preserveTimestamp(true)
                    .filter(compositeWithAcceptOnceFilter())                        .remoteDirectory(ftpProperties.getRemoteDirectory())
                    .localDirectory(new File(ftpProperties.getLocalDirectory())).autoCreateLocalDirectory(true),
            consumer -> consumer.id("ftpInboundAdapter")) /* Fine from() */
            .handle(new GenericHandler<File>() {

                @Override
                @Transactional
                public Object handle(File payload, Map<String, Object> headers) {
                    logger.debug("Data arrived {} {}", payload, payload.getClass().getName());
                    return payload;
                }

            }) /* Fine GenericHandler */
            .handleWithAdapter(a -> a.ftp(ftpSessionFactory())
                    .remoteDirectory(ftpProperties.getRemoteArchiveDirectory()).autoCreateDirectory(true))              
            .get();
}

如果我追加

.handleWithAdapter(a -> a.file("'" + ftpProperties.getLocalArchiveDirectory() + "'")
.autoCreateDirectory(true).deleteSourceFiles(true))

在ftp适配器配置之后,bean初始化程序回复以下错误消息:

Caused by: org.springframework.beans.factory.BeanCreationException: The 'currentComponent' (org.springframework.integration.file.remote.handler.FileTransferringMessageHandler@80bfa9d) is a one-way 'MessageHandler' and it isn't appropriate to configure 'outputChannel'. This is the end of the integration flow.

我该如何解决?

2 个答案:

答案 0 :(得分:2)

作为一种解决方法,我在放弃文件适配器时重构了我的代码:

@Bean
public IntegrationFlow ftpInboundFlow() {
    return IntegrationFlows
        .from(source -> source.ftp(ftpSessionFactory()).deleteRemoteFiles(true).preserveTimestamp(true)
                .filter(compositeWithAcceptOnceFilter())                        .remoteDirectory(ftpProperties.getRemoteDirectory())
                .localDirectory(new File(ftpProperties.getLocalDirectory())).autoCreateLocalDirectory(true),
        consumer -> consumer.id("ftpInboundAdapter")) /* Fine from() */
        .handle(new GenericHandler<File>() {

            @Override
            @Transactional
            public Object handle(File payload, Map<String, Object> headers) {
                logger.debug("Data arrived {} {}", payload, payload.getClass().getName());
                /* handle file content here ... */
                /* ... then move it to archive */
                File dstFile = new File("archive", payload);
                FileUtils.moveFile(payload, dstFile);
                return dstFile;
            }

        }) /* Fine GenericHandler */
        /* Archive the remote file */
        .handleWithAdapter(a -> a.ftp(ftpSessionFactory())
                .remoteDirectory(ftpProperties.getRemoteArchiveDirectory()).autoCreateDirectory(true))              
        .get();

}

答案 1 :(得分:1)

使用XML配置,我们有一个单向组件的约定,例如adapter,例如<int-ftp:outbound-channel-adapter><int-file:outbound-channel-adapter>。对于Java DSL,我们使用类似Gateway后缀的约定来生成request/reply端点的工厂方法。否则它就像你的a.ftp(ftpSessionFactory())一样。

从XML配置的另一方面来看,我们没有任何选择继续进行下游流定义,因为output-channel上没有<outbound-channel-adapter>

使用Java DSL,我们没有太多选择来防止像你这样的错误。但我在那里做了如此全面的异常消息,它应该按照您的流程定义的不同方式跟随您。

你的答案是.publishSubscribeChannel(),当你可以为它订阅几个端点时接受相同的消息来为它制作不同的逻辑。

请找我的article,我逐行解释大部分SI Java DSL功能。