Spring XML equivalent of @EnableAsync

14,951

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.

Share:
14,951

Related videos on Youtube

Monish Sen
Author by

Monish Sen

-d(^_^)b-

Updated on September 14, 2022

Comments

  • Monish Sen
    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
    Monish Sen almost 9 years
    Even 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
    Monish Sen almost 9 years
    yes i've <task:scheduler id="taskScheduler"/> <task:executor id="taskExecutor" pool-size="1"/> <task:annotation-driven executor="taskExecutor" scheduler="taskScheduler"/>
  • Pulkit
    Pulkit almost 9 years
    can you post your servlet.xml file content?
  • Monish Sen
    Monish Sen almost 9 years
    async wasnt working for me as i'd used them on local method...exported the async logic to a separate component
  • Pulkit
    Pulkit almost 9 years
    you can see a sample here on how to use it javaprogrammingtips4u.blogspot.in/2010/05/…