在使用Java DSL成功传输ftp后移动文件 [英] Move file after successful ftp transfer using Java DSL

查看:196
本文介绍了在使用Java DSL成功传输ftp后移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个使用spring-integration java dsl的流程来传输一个文件,处理它,然后将它传回一个归档目录,最后将它移入本地归档目录中。
这很容易:

  @Bean 
Public IntegrationFlow ftpInboundFlow(){
if(ftpProperties.getEnabled()== false){
return null;
}
logger.trace(Starting ftp flow);
返回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> ; header){
logger.debug(Data arrived {} {},payload,payload.getClass()。getName());
return payload;
}

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

$ / code>

如果我追加

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

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

 原因:org.springframework.beans.factory.BeanCreationException:'currentComponent'(org.springframework.integration.file.remote.handler.FileTransferringMessageHandler @ 80bfa9d)是单向的'MessageHandler',它不适合配置'outputChannel'。这是整合流程的结束。 

我该如何解决它? >解决方案

作为解决方法,我重新构造了放弃使用文件适配器的代码:

  @ Bean 
public IntegrationFlow ftpInboundFlow(){
返回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 (新的GenericHandler< File>(){

@Override
@Transactional
public(ftpInboundAdapter))/ * Fine from()* /
.handle对象句柄(File payload,Map< String,Object> headers){
logger.debug(Data arrived {} {} ,payload,payload.getClass()。getName());
/ *在此处理文件内容... * /
/ * ...然后将其移至归档文件* /
文件dstFile =新文件(archive,payload);
FileUtils.moveFile(payload,dstFile);
return dstFile;

$ b})/ * Fine GenericHandler * /
/ *存档远程文件* /
.handleWithAdapter(a - > a.ftp(ftpSessionFactory ))
.remoteDirectory(ftpProperties.getRemoteArchiveDirectory())。autoCreateDirectory(true))
.get();

}


I've defined a flow using spring-integration java dsl to ftp transfer a file, handle it, then transfer it back in an "archive" dir, and at last move it in a local archive dir. Which is something "quite easy" as:

@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();
}

If I append

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

after the ftp adapter configuration the bean initializer replies with the following error message:

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.

How should I fix it?

解决方案

As a workaround I refactored my code in giving up with the file adapter:

@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();

}

这篇关于在使用Java DSL成功传输ftp后移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆