异步处理电子邮件中的附件(Spring Mail Integration)

发布于 2024-12-09 12:18:38 字数 344 浏览 0 评论 0原文

如果我有一个带有邮件服务器入站通道的 Spring 应用程序,那么处理每封电子邮件中的每个文件的最佳方法是什么(我大约每 1 分钟轮询一次,并获取 1 封带有多个附件的电子邮件)。

虽然我可以在接收通道(SimpleAsyncTaskExecutor 或 ThreadPoolTask​​Executor)应用多线程,但这并没有多大帮助,因为如果我在一封电子邮件中附加了 10 个文件,它们的处理几乎绑定到一个线程。

到目前为止,我一直保持同步,因为我想为每封电子邮件聚合一些数据,并在处理所有文件后发送响应。我相信这也可以通过更好的方式来完成。

一般来说,如何异步处理每封电子邮件中的每个文件,然后再次异步构建电子邮件回复?

If I have a Spring app with a mail server inbound channel, what is the best way to process every file in every email (I poll approx. every 1 min, and fetch 1 email with multiple attachments).

Although I can apply multithreading at the receiving channel (SimpleAsyncTaskExecutor or ThreadPoolTaskExecutor) this doesn't help much because if I have 10 files attached in an email, their processing is pretty much bound to one thread.

I've been keeping this pretty synchronous until now, because I wanted to aggregate some data for every email, and send a response after all files have been processed. I believe this could also be done in a better way.

In general how can I asynchronously process every file in every email, and then again asynchronously build an email reply?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

才能让你更想念 2024-12-16 12:18:38

看起来您正在要求 java.lang. util.concurrent.Future。这是一个java核心概念,阻塞直到计算出(方法)结果。 (有关示例,请参阅 JavaDoc

Spring @Async 也支持 Future 概念。

因此,您唯一需要做的就是拥有一种使用 @Async 的方法,将邮件的一个附件作为参数,并返回将来计算的结果。
您需要为所有附件调用所有这些方法(异步)并将立即返回的 future 存储在列表中。调用完所有方法后。您尝试在新循环中获取功能结果。此循环完成后,所有附件都将异步进行。

 processOneMail(List<Attachement> attachments) {
      List<Future<AttachmentResult>> futures = new ArrayList...

      for(Attachment attachment : attachments) {
        futures.add(processOneAttachment(attachment)); //async
      }

      List<AttachmentResult> attachmentResults = new ArrayList...
      for(Future<AttachmentResult>> future : futures) {
         attachmentResults.add(future.get()); //eventually blocks
      }
      //now all attachments are calculated and stored in the list.      
      ...
    }
@Async
Future<AttachmentResult> processOneAttachment(Attachment attachment) {
   ...
}

另请参阅:http://blog.espenberntsen.net/2010/03 /08/spring-asynchronous-support/

Looks like you are asking for java.util.concurrent.Future. This is a java core concept to block until a (method) result is calculated. (see JavaDoc for an example)

The Spring @Async support the Future concept too.

So the only think you need to do is having a method that uses @Async takes one attachment of the Mail as argument and returns what ever is calculated in a future.
The you need to invoke all this methods for all attachments (asynchronous) and store the immediately returned future in a list. After all methods are invoked. You try to get the feature results in an new loop. After this loop is finish all attachments are proceed asynchronous.

 processOneMail(List<Attachement> attachments) {
      List<Future<AttachmentResult>> futures = new ArrayList...

      for(Attachment attachment : attachments) {
        futures.add(processOneAttachment(attachment)); //async
      }

      List<AttachmentResult> attachmentResults = new ArrayList...
      for(Future<AttachmentResult>> future : futures) {
         attachmentResults.add(future.get()); //eventually blocks
      }
      //now all attachments are calculated and stored in the list.      
      ...
    }
@Async
Future<AttachmentResult> processOneAttachment(Attachment attachment) {
   ...
}

See also: http://blog.espenberntsen.net/2010/03/08/spring-asynchronous-support/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文