grep
grep

Reputation: 5623

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

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,

Upvotes: 0

Views: 1528

Answers (2)

Artem Bilan
Artem Bilan

Reputation: 121427

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.

Upvotes: 2

Tymur Yarosh
Tymur Yarosh

Reputation: 623

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));

Upvotes: 1

Related Questions