我应该使用哪个spring库发送带有多线程的电子邮件 [英] Which library of spring should I use to send emails with multy-threading

查看:75
本文介绍了我应该使用哪个spring库发送带有多线程的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的电子邮件太多了.我应该编写调度程序以便向他们发送消息.消息是不同的.我使用的是Spring Framework4.x.

I have too many emails. I should write scheduler in order to send messages to them. Messages are different. I use spring framework 4.x.

我可以编写连接到SMTP服务器的简单类.但是在这种情况下,我也应该编写线程库以便并行发送电子邮件.

I can write simple class, which connects to SMTP server. But in this case I should write my thread library too in order to send emails parallel.

spring是否已经编写了可以让我更灵活地执行此任务的方法的库?我不想使用线程.如果spring已经具有此功能,那就太好了.

Do spring have already written library which give me more flexible way to do this tasks? I do not want to use threads. It will be nice if spring already have this functionality.

我需要为此进行Spring集成吗?

最诚挚的问候,

推荐答案

是的,您肯定可以使用Spring Integration来做到这一点,因为Spring Core的TaskExecutor可以提供ExecutorChannel实现:

Yes, you definitely can do that with Spring Integration, because there is an ExecutorChannel implementation with can be supplied with an TaskExecutor from the Spring Core:

<channel id="sendEmailChannel">
   <dispatcher task-executor="threadPoolTaskExecutor"/>
</channel>

<int-mail:outbound-channel-adapter channel="sendEmailChannel" mail-sender="mailSender"/>

但是无论如何,您应该记住所有Spring Integration组件均基于Java,并且ExecutorService用于后台.

But anyway you should keep in mind that all Spring Integration components are based on the Java and that ExecutorService is used on the background.

从另一面看,如果您只需要Spring Integration的邮件发送内容,那将是一项开销,并且可以简单地将Core Spring Framework传统(如JavaMailSender作为bean)和@Async用于sendMail方法以达到您的parallel要求.

From other side if you need only the mail sending stuff from the Spring Integration, it would be an overhead and can simply use Core Spring Framework legacy like JavaMailSender as a bean and @Async for the sendMail method to achieve your parallel requirement.

更新

您能告诉我在这种情况下我是否需要JMS?

could you tell me whether I need JMS for this situation?

我在这里看不到任何与JMS相关的东西.您的解决方案中没有(或至少不显示)任何真实的integration点.关于Spring Integration,即使是用于电子邮件发送,我也可以说同样的话.但是,使用Spring Boot,您的SI配置将足够短.从另一方面讲,如果您将更好地研究Spring Integration,最终将获得更多收益,依靠您的系统的内部和外部组件,通过JMS,AMQP,Kafka等与其他系统配合使用

I don't see any JMS-related stuff here. You don't have (or at least don't show) any real integration points in your solution. The same I can say even about Spring Integration just for email sending. However with the Spring Boot your SI config will be enough short. From other side if you'll study Spring Integration better eventually you'll get more gain to rely on the Integration components for your systems, as internally, as well as externally with other systems through JMS, AMQP, Kafka etc.

说实话:很多年前,我对Spring Integration的初次认识是由于需要从FTP获取文件并具有自动拾取新文件的能力.我仅在Spring Integration 1.0.0.M1中找到了解决方案.在<int-ftp:inbound-channel-adapter>的简短XML配置之后,我爱上了Spring Integration,从那时起,它就成为了我生活的一部分. :-)

To be honest: a lot of years ago my first acquaintance with Spring Integration was due the requirement to get files from the FTP and have ability to pick up new files automatically. I found the solution only in the Spring Integration 1.0.0.M1. After that short XML config for the <int-ftp:inbound-channel-adapter> I loved Spring Integration and since that time it became as a part of my life. :-)

因此,您可以在简单的应用程序中继续进行Spring Integration,或者直接使用JavaMailSender直接使用更正式的解决方案.

So, it's up to you to go ahead with Spring Integration in your simple app, or just follow with more formal solution with JavaMailSender direct usage.

这篇关于我应该使用哪个spring库发送带有多线程的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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