Since Spring 3.0, annotation support for both task scheduling and asynchronous method execution has been added. The post will give a demo of @Async annotation and its uses.
@Service – annotate classes at service layer level.
@Async – put @Async annotation above the method in an asynchronous process.
To define a void async method
1 2 3 4 5 6 |
@Async public void methodAsync1() { // add logic } |
To define an asynchronous method which return something. To achieve it, the method shall return a java.util.concurrent.Future object, and the implementation shall return a new AsyncResult object () let’s use Accountclass:
1 2 3 4 5 6 7 8 |
@Async public Future<Account> methodAsync2() { // .... logic here .... Account account = .... return new AsyncResult<Account>(account); } |
To retrieve the object saved in Future object
1 2 3 |
Account account = future.get(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package net.tecbar.asynchronous; import org.apache.log4j.Logger; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Service public class OrderHandler { private static final Logger LOG = Logger.getLogger(OrderHandler.class); @Async public void processOrder() { LOG.info("start processOrder"); try { Thread.sleep(5000); // use 5 seconds to emulate the operation time } catch (InterruptedException e) { LOG.error(e.getMessage(), e); } LOG.info("end processOrder"); } } |
Bean Configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- <context:component-scan base-package="tv.blackarrow.cip.service"/> --> <bean id="orderHandler" class="net.tecbar.asynchronous.OrderHandler"> </bean> <task:annotation-driven /> </beans> |
client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package net.tecbar.asynchronous; import org.apache.log4j.Logger; import org.springframework.context.support.ClassPathXmlApplicationContext; public class OrderClient { private static final Logger LOG = Logger.getLogger(OrderClient.class); public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("conf/async_beans.xml"); OrderHandler handler = (OrderHandler)ctx.getBean("orderHandler"); LOG.info("call handler processOrderMethod"); handler.processOrder(); LOG.info("continue execute..."); } } |
Log output
1 2 3 4 5 6 |
15:27:50,750 INFO [OrderClient]:12 - call handler processOrderMethod 15:27:50,754 INFO [OrderClient]:14 - continue execute... 15:27:50,767 INFO [OrderHandler]:13 - start processOrder 15:27:55,767 INFO [OrderHandler]:21 - end processOrder |
Everything is very open with a very clear clarification of the issues. It was definitely informative.
Thank you for sharing!