异步处理电子邮件中的附件(Spring Mail Integration)
如果我有一个带有邮件服务器入站通道的 Spring 应用程序,那么处理每封电子邮件中的每个文件的最佳方法是什么(我大约每 1 分钟轮询一次,并获取 1 封带有多个附件的电子邮件)。
虽然我可以在接收通道(SimpleAsyncTaskExecutor 或 ThreadPoolTaskExecutor)应用多线程,但这并没有多大帮助,因为如果我在一封电子邮件中附加了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在要求
java.lang. util.concurrent.Future
。这是一个java核心概念,阻塞直到计算出(方法)结果。 (有关示例,请参阅 JavaDoc)Spring
@Async
也支持Future
概念。因此,您唯一需要做的就是拥有一种使用 @Async 的方法,将邮件的一个附件作为参数,并返回将来计算的结果。
您需要为所有附件调用所有这些方法(异步)并将立即返回的 future 存储在列表中。调用完所有方法后。您尝试在新循环中获取功能结果。此循环完成后,所有附件都将异步进行。
另请参阅: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 theFuture
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.
See also: http://blog.espenberntsen.net/2010/03/08/spring-asynchronous-support/