Spring XML equivalent of @EnableAsync
Solution 1
Did you try using this
<task:annotation-driven />
Solution 2
Yes, you can use something like this
<beans>
<task:annotation-driven executor="myExecutor" exception-handler="exceptionHandler"/>
<task:executor id="myExecutor" pool-size="7-42" queue-capacity="11"/>
<bean id="asyncBean" class="com.foo.MyAsyncBean"/>
<bean id="exceptionHandler" class="com.foo.MyAsyncUncaughtExceptionHandler"/>
</beans>
According to the Spring documentation, this is equivalent to using @EnableAsync
Solution 3
In the annotation based approach you have to have @EnableAsync on the Configuration class. Something like as shown below:
@Configuration
@EnableAsync
@ComponentScan(basePackages ="com.spring.sample.demoAsync")
public class SpringAsyncConfig {
}
Then you create a component class to have a function that is called Asynchronously. Something like as shown below:
@Component
public class AsyncClass {
@Async
public Future<String> asyncMethod() {
System.out.println("Executing Thread Async:" +Thread.currentThread().getName());
return new AsyncResult<String>(Thread.currentThread().getName());
}
}
To have the xml equivalent of this approach, you can create a bean in the applicationContext.xml file as shown below:
<bean id="AsyncClass" class="com.spring.sample.demoAsync.AsyncClass"/>
To call the function asyncMethod() in your flow, you can refer AsyncClass bean from any other bean or service. Below is something that I tried to stitch the flow:
<bean id="callingBean" class="comspring.sample.demoAsync.CallingBeanClass">
<property name="AsyncClassBean" ref="AsyncClass"/>
</bean>
It's not necessary to follow this step but is an alternative approach.
In my applicationContext.xml file, I also imported the task schema by using:
xmlns:task="http://www.springframework.org/schema/task
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
and then mentioning the executor as a task in the same file:
<task:executor id="myexecutor" pool-size="5" />
Now my AsyncClass looks like this without @component annotation.
public class AsyncClass {
@Async("myexecutor")
public Future<String> asyncMethod() {
System.out.println("Executing Thread Async:" +Thread.currentThread().getName());
return new AsyncResult<String>(Thread.currentThread().getName());
}
}
and then finally invoking the asyncMethod() asynchronously from the CallingBeanClass.
Related videos on Youtube
Comments
-
Monish Sen over 1 year
Is there a way to turn on Spring's Async configuration from XML? All the examples I saw are using programmatic context declaration and use
@EnableAsync
Is there an XML equivalent for this. In some places I saw
<context:annotation-config />
being used, but this doesn't mention anything about async .I am using Spring 4.
-
Monish Sen almost 9 yearsEven though my context declaration is xml based and using componentscan, the beans are programmatic with '@Async'...i think the problem is me using '@async' on local methods
-
Monish Sen almost 9 yearsyes i've <task:scheduler id="taskScheduler"/> <task:executor id="taskExecutor" pool-size="1"/> <task:annotation-driven executor="taskExecutor" scheduler="taskScheduler"/>
-
Pulkit almost 9 yearscan you post your servlet.xml file content?
-
Monish Sen almost 9 yearsasync wasnt working for me as i'd used them on local method...exported the async logic to a separate component
-
Pulkit almost 9 yearsyou can see a sample here on how to use it javaprogrammingtips4u.blogspot.in/2010/05/…