dev2d
dev2d

Reputation: 4262

why my method is not Asyncronous?

i am trying to make a method async using @Async annotation provided by Spring 3.0

i have done following

inclued following in my module-context.xml

<task:executor id="initiateContactCreation" pool-size="2-10" queue-capacity="5"/>
<task:annotation-driven executor="initiateContactCreation" />

annotated method with @Async

@Async
    private void initiateContactCreation(String fromUserId, List<String> toUsers){
        logger.info("Inside Async method for contact creation");
        ContactDetails contactDetails = new ContactDetails();
        contactDetails.setUserId(fromUserId);
        contactDetails.setContactEmailIds(toUsers.toArray(new String[toUsers.size()]));
        this.contactsAndDirSvc.addContact(contactDetails);
        logger.info("Returning from Async method for contact creation");
    }

but i see that the method does not return control immediately.

my logger shows logs from initiateContactCreation then logs from addContact(PS. it is taking time executing this method and i do not want it to be executed synchronously) and then logs from the method from where i am calling initiateContactCreation

what am i doing wrong?

Upvotes: 0

Views: 59

Answers (2)

geoand
geoand

Reputation: 64079

prabugp's explanation is spot on, I don't have anything more to add to it.

I do have some links however that you should check regarding Spring AOP's pitfalls and how to get past them (also applies to your code).

1, 2, 3, 4

Upvotes: 0

Praba
Praba

Reputation: 1381

Since this method is private, I assume that you're calling this method from within this class using the 'this' reference. Spring cannot proxy calls which are made within class. the call has to come from outside of your class so that Spring can intercept and apply annotations and other proxying stuff that it does.

Upvotes: 5

Related Questions