简体   繁体   English

变压器,在 Spring 集成中摄取后更改文件名

[英]Transformer, changing filename after ingestion in Spring Integration

I am completely new to Spring Integration package and found different advice on how to approach the problem.我是 Spring Integration package 的新手,发现了有关如何解决该问题的不同建议。 So far I only found one that worked, but I am unsure if it is correct or why it produces a logged Error (even though the file is ingested and re-named as I intended).到目前为止,我只找到了一个有效的方法,但我不确定它是否正确或为什么它会产生一个记录的错误(即使文件已按我的预期被提取和重命名)。

The use case for this is that my system expects a "temp_" prefix to ignore a partially downloaded file.这个用例是我的系统需要一个“temp_”前缀来忽略部分下载的文件。 I found that even though the default ".writing" can be changed, it only works as a suffix.我发现即使可以更改默认的“.writing”,它也只能用作后缀。

Here is the config I used:这是我使用的配置:

SftpInboundChannelAdapterSpec integrationFlow = Sftp.inboundAdapter(sftpSessionFactory)
        .filter(chainFilter)
        .localFilename(temp_::concat)
        .remoteDirectory(remoteDirectory)
        .deleteRemoteFiles(false)
        .preserveTimestamp(true)
        .localDirectory(new File(localDirectory));
return IntegrationFlows
        .from(integrationFlow, pc -> pc.poller(pm -> pm.cron(cron).maxMessagesPerPoll(1).errorHandler(new SftpUtils.SftpErrorHandler())))
        .transform((File f) -> f.getName().contains(temp_) ?
                f.renameTo(new File(f.getParent(), f.getName().replace(temp_, ""))) :
                null)
        .handle((transformResult, messageHeaders) -> {
            log.info("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ({}): {}", localDirectory, transformResult);
            return null;
        })
        .get();

And the error being produced:并产生错误:

SftpErrorHandler: Error occured in SFTP ingestion flow, exception: nested exception is org.springframework.integration.handler.ReplyRequiredException: No reply produced by handler 'leapInboundFlow.org.springframework.integration.config.ConsumerEndpointFactoryBean#0', and its 'requiresReply' property is set to true., failedMessage=GenericMessage [payload=src\main\resources\input\genericfile.name, headers={file_originalFile=src\main\resources\input\genericfile.name, id=293a3e85-cd62-37f7-b701-bbacbf27a83f, file_name=genericfile.name, file_relativePath=genericfile.name, timestamp=1668518160014}]

Your problem is here:你的问题在这里:

    .transform((File f) -> f.getName().contains(temp_) ?
            f.renameTo(new File(f.getParent(), f.getName().replace(temp_, ""))) :
            null)

The transformer is a request-reply endpoint which does not tolerate null as a reply. transformer器是一个请求-回复端点,它不允许null作为回复。

Your logic over here is not fully clear.您在这里的逻辑并不完全清楚。 Let's forget that you cannot return null .让我们忘记您不能返回null It's definitely wrong and has to be fixed this or other way.这绝对是错误的,必须以这种或其他方式解决。

What exactly do you want to return from this transformer?你到底想从这个变压器返回什么? Probably your wish is just to rename the file and go ahead downstream with that renamed file already?可能您的愿望只是重命名该文件,并且 go 已经在该重命名文件的下游?

Probably you wanted something like this:可能你想要这样的东西:

    .transform((File f) -> 
         { 
            if (f.getName().contains(temp_)) {
                f.renameTo(new File(f.getParent(), f.getName().replace(temp_, "")));
            }
            return f;
          })

?

Sorry, it is not clear what is a business task from a content of your quesiton, but I hope I explained why your error appears.抱歉,从您的问题内容中不清楚什么是业务任务,但我希望我解释了为什么会出现您的错误。

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

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