0
    @Async
    public void sendEmail(String to, String subject, WebContext context,String template) throws MessagingException
    {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, "utf-8");
        helper.setTo(to);
        helper.setFrom("some.email@mail-server.com");
        helper.setSubject(subject);
        System.out.println("content1");  //able to see this in console
        String content=templateEngine.process(template, context); //problem with this
        System.out.println("content2");  // not able to see this in console
        helper.setText(content, true);
        mailSender.send(mimeMessage);
    }

I could not send email with @Async annotation. when I remove this annotation, email works. @Async is not working with Thymeleaf template engine. I placed @EnableAsync on RootConfig, where I am creating beans

Any ideas?

Hossein
  • 1,151
  • 9
  • 19
lch
  • 4,569
  • 13
  • 42
  • 75
  • Have you configured Async correctly in your application-server, and your web.xml to use Async? Also, in your main post, you have your email, remove it to avoid spam on that account. – We are Borg Sep 16 '15 at 09:42

2 Answers2

1

Use Context instead of WebContext

import org.thymeleaf.context.Context;

WebContext lifecycle is dependent on the request thread(main thread).

Once the main thread return the response it will destroy WebContext.

String content=templateEngine.process(template, context);

Your code above will try to access Object which has been destroyed, hence it will throw Exception

Give it a try ;)

Angky William
  • 171
  • 2
  • 6
  • I have same code. I have a method which generate PDF using IText and @Async is used.To set dynamic data i also used org.thymeleaf.TemplateEngine and org.thymeleaf.context.WebContext. I have a css file which is used in html pages. If i don't use WebContext class then "org.thymeleaf.exceptions.TemplateProcessingException: Link base "/css/styles.css" cannot be context relative (/...) unless the context used for executing the engine implements the org.thymeleaf.context.IWebContext)" error is generated. My question is that how can i use WebContext in Async method? – Dilip Dalwadi Feb 06 '19 at 11:32
-1

Sounds like your Async is not enabled check out this tutorial as to best way to turn it on.

http://blog.adaofeliz.com/2014/11/02/spring-mvc-without-xml-configuration/

In summary on startup you need to ensure the following option is registered for the DispatcherServlet:

servlet.setAsyncSupported(true);

As per comment discussion please find a similar raised question around Async not working propery and user having to force the registration of it:

Spring @Async Not Working

Community
  • 1
  • 1
Aeseir
  • 7,754
  • 10
  • 58
  • 107
  • Unfortunately EnableAsync doesn't actually work always. There is a common issue around async in framework where you actually have to force the registration of async through dispatcherservlet. i've added a similar stackoverflow question where they explore this problem. I can 99.9% guarantee this is your problem (give the fix above a go). – Aeseir Sep 17 '15 at 23:07