Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Email with spring in a new thread issue

One of the functionalities of app that I'm developing is that an email is sent every time user get's his invoice registered in our system. Sending an email from Java app easy especially if using Spring framework. I use JavaMailSenderImpl and SimpleMailMessage from Spring framework and it works okay.

But I need to send email in a new thread so that communication with SMTP server does not slow down the rest of apps processes. Problem is that when I call

MailSender.send()

method from a new thread, email message is not sent, as opposed when sending in a same thread. I tried with spring's @Async annotation, spring Executor and plain old java.lang.Thread but it doesn't work.

Can email be send asynchronously in java with spring? Had anyone a similar issue with this? I can post some code samples if needed.

Tnx

like image 941
Marko Avatar asked Mar 14 '11 15:03

Marko


1 Answers

It should work.

You need to tell Spring that it should pay attention to your @Async Annotation by:

<task:annotation-driven />

And there are some limitations you need to pay respect to:

  • the annotated method must belong to a spring bean
  • the invocation of the annotated method must be executed from a different Spring Bean (if you are using standard Spring AOP).
like image 123
Ralph Avatar answered Oct 02 '22 20:10

Ralph