Announcement Announcement Module
Collapse
No announcement yet.
@Async doesn't work on some methods in same class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Async doesn't work on some methods in same class

    Hi,
    I am trying to send e-mail asynchronously but I successfully ran into trouble I have 2 methods in same class. Annotating one methods with @Async leads to running method asynchornously. Annotating second method with @Async doesn't work. This is that class simplified:

    Code:
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;
    
    @Service
    public class EmailService {
    
    	public void sendEmailsFromTemplate(Collection<User> to, String templateName, String... args) {
    		// load templates
    		....
    		// send emails
    		sendEmails(to, subject, message);
    	}
    
    	public void sendEmails(Collection<User> to, String subject, String message) {
    		for (User user : to) {
    			sendEmail(user.getEmail(), subject, message);
    		}
    	}
    
    	private void sendEmail(String to, String subject, String message) {
    		// do real email sending
    		...
    	}
    
    }
    This works perfect:
    Code:
    @Async
    public void sendEmailsFromTemplate(Collection<User> to, String templateName, String... args)
    But annotating
    Code:
    @Async
    public void sendEmails(Collection<User> to, String subject, String message)
    doesn't work. Why it doesnt work?

  • #2
    I suggest the forum search and a read of chapter 7.6.1 of the reference guide.

    Short answer
    Spring uses proxy based AOP internal method calls don't pass through the proxy rendering your @Async (or any other AOP advice for that matter) useless.

    Comment


    • #3
      Thanks for pointing me, now it makes sense

      Comment

      Working...
      X