Is there a way I send mail with all files (as Zip) received in Spring Integration FTP instead sending the files Individually per Message received

huangapple go评论141阅读模式
英文:

Is there a way I send mail with all files (as Zip) received in Spring Integration FTP instead sending the files Individually per Message received

问题

我有一个基于Spring Integration FTP的Spring Boot应用程序,我可以在其中下载/上传文件。

我需要将通过邮件接收的文件(作为ZIP文件)发送出去,但问题是,我只能每收到一个文件时发送一封邮件,这意味着如果我收到100个文件,邮件接收者将会收到100封邮件。这是因为Spring Integration会逐个处理消息。

我需要一种方法来汇总每次轮询接收的文件,然后发送给接收者,而不是逐个发送文件。

我尝试了一些想法,比如:

  1. 在接收文件时将文件移动到临时位置,检查轮询何时完成,然后将这些临时位置的文件压缩并发送给接收者。
  2. 找出如何知道每次轮询接收了多少文件。

请问有人可以建议我如何实现这个功能吗:

public IntegrationFlow createIntFlow(MessageDirectory directory, ConcurrentMetadataStore metadataStore) {
    var directoryName = directory.getDirectoryName();

    var integrationFlowBuilder = IntegrationFlows
            .from(getInboundAdapter(directory, metadataStore),
                    e -> e.id(directoryName + "-PerPoller")
                            .autoStartup(true)
                            .poller(Pollers
                                    .cron(directory.getSchedule())
                                    .taskExecutor(simpleAsyncTaskExecutor)
                                    .errorChannel("errorChannel")
                                    .maxMessagesPerPoll(-1)))
            .log();

    integrationFlowBuilder.publishSubscribeChannel(s -> {
        s.subscribe(sf -> sf.handle(c -> emailEmitter.createFileEvent(directory, c.getPayload().toString()));
    });

    return integrationFlowBuilder.get();
}
英文:

I have a Spring Boot application based on Spring Integration FTP where I can DOWNLOAD/UPLOAD files.

I need to send the files (as ZIP) received via mail but the problem is, I can only do this per message a file received which means if I receive 100 files, the email recipient would receive 100 emails. This is because Spring Integration processes the messages one after the other.

I need a way to aggregate the files received per poll and send to the recipient instead of sending the files one after the other.

I have tried to come up with some ideas such as these:

  1. move the files received to a temporary location as they are received, check when the poll is done, zip the files in this temporary location, and send them to the recipient.
  2. Find out how to know files received per poll.

Please can someone advise on how I can implement this:

public IntegrationFlow createIntFlow(MessageDirectory directory, ConcurrentMetadataStore metadataStore) {
        var directoryName = directory.getDirectoryName();

        var integrationFlowBuilder = IntegrationFlows
                .from(getInboundAdapter(directory, metadataStore),
                        e -> e.id(directoryName + "-PerPoller")
                                .autoStartup(true)
                                .poller(Pollers
                                        .cron(directory.getSchedule())
                                        .taskExecutor(simpleAsyncTaskExecutor)
                                        .errorChannel("errorChannel")
                                        .maxMessagesPerPoll(-1)))
                .log()
         

        **integrationFlowBuilder.publishSubscribeChannel(s -> {
           s.subscribe(sf -> sf.handle(c -> emailEmitter.createFileEvent(directory, c.getPayload().toString())));**
       });

            });
        }
        return integrationFlowBuilder.get();
    }```

答案1

得分: 1

查看此项目 https://github.com/spring-projects/spring-integration-extensions/tree/main/spring-integration-zip。其中有一个ZipTransformer,可以压缩Iterable输入消息。

因此,可能源轮询通道适配器不适合您的要求,但最好查看FtpOutboundGateway及其MGET命令支持:https://docs.spring.io/spring-integration/docs/current/reference/html/ftp.html#using-the-mget-command。这样,您将获得一个List<File>作为此调用的结果,然后将此消息发送到ZipTransformer。如果压缩成功,将得到一个zip文件,您可以简单地发送到电子邮件。

您仍然可以使用源轮询通道适配器,但最好将其作为一个普通的触发器,通过简单的Supplier - () -> ""。或者将远程目录作为FtpOutboundGateway的负载。

在压缩之前,您还可以考虑使用AcceptOnceFileListFilter作为一个转换步骤,以过滤出之前处理过的文件。

英文:

See this project https://github.com/spring-projects/spring-integration-extensions/tree/main/spring-integration-zip. There is a ZipTransformer which can zip an Iterable input message.

So, probably a source polling channel adapter does not fit to your requirements, but better to look into an FtpOutboundGateway and its MGET command support: https://docs.spring.io/spring-integration/docs/current/reference/html/ftp.html#using-the-mget-command. So, you got a List&lt;File&gt; as a result of this call and you send this message to that ZipTransformer. The result if zip is just one zip file which you simply can send to email.

You still may use a source polling channel adapter, but rather as a plain trigger via simple Supplier - () -&gt; &quot;&quot;. Or with a remote dir as a payload for that FtpOutboundGateway.

You may also consider to use a AcceptOnceFileListFilter as a transformer step before zipping to filter out those files you have processed before.

huangapple
  • 本文由 发表于 2023年3月7日 13:08:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658227.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定