java - Spring 3: How to call @Async annotated methods from the TaskExecutor -


i'm new asynchronous task execution in spring, please forgive me if sounds silly question.

i read @async annotation introduced spring 3.x onward @ method level invocation of method occur asynchronously. read can configure threadpooltaskexecutor in spring config file.

what i'm not able understand how call @async annotated method tak executor lets suppose - asynctaskexecutor

earlier used in class:

@autowired protected asynctaskexecutor executor; 

and then

executor.submit(<some runnable or callable task>) 

i'm not able understand relationship between @async annotated methods , taskexecutor.

i tried searching lot on internet not on this.

can provide example same.

here's example of @async use:

@async void dosomething() {     // executed asynchronously } 

now call method class , run asynchronously. if want return value use future

@async future<string> returnsomething(int i) {     // executed asynchronously } 

the relationship between @async , taskexecutor @async uses taskexecutor behind scenes. docs:

by default when specifying @async on method, executor used 1 supplied 'annotation-driven' element described above. however, value attribute of @async annotation can used when needing indicate executor other default should used when executing given method.

so set default executor, add spring config

<task:annotation-driven executor="myexecutor" /> 

or use particular executor single use try

@async("otherexecutor") 

see http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-async


Comments