Monish Sen
Monish Sen

Reputation: 1888

Spring XML equivalent of @EnableAsync

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.

Upvotes: 17

Views: 17189

Answers (3)

Micho
Micho

Reputation: 3963

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

Upvotes: 5

Nishant Mittal
Nishant Mittal

Reputation: 11

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.

Upvotes: 1

Pulkit
Pulkit

Reputation: 4084

Did you try using this

<task:annotation-driven /> 

Upvotes: 13

Related Questions