Results 1 to 3 of 3

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

  1. #1
    Join Date
    Jun 2009
    Posts
    8

    Unhappy @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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2009
    Posts
    8

    Default

    Thanks for pointing me, now it makes sense

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •