Which library of spring should I use to send emails with multy-threading

☆樱花仙子☆ 提交于 2019-12-31 05:12:57

问题


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.

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.

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.

Do I need Spring integration for this?

Best regards,


回答1:


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"/>

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.

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.

UPDATE

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

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.

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. :-)

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.




回答2:


You should use java executors framework. For example you can write something like the code below:

ExecutorService executor = Executors.newWorkStealingPool();
executor.execute(() -> mailSender.send(mail));


来源:https://stackoverflow.com/questions/28987379/which-library-of-spring-should-i-use-to-send-emails-with-multy-threading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!