在spring 3中,@Async注解能让某个方法快速变为异步执行,马上来先DEMO上手下。
假如在网站的用户注册后,需要发送邮件,然后用户得到邮件确认后才能继续其他工作;
假设发送是一个很耗费时间的过程,因此需要异步。
1 namespace要注意,加上task
2 RegularService.java 注册类
4 最后在applicationContext.xml中加入:
就是<task:annotation-driven/>这个一定不能少喔。
5 运行:
User registration for tom complete
注册完成,邮件稍后发送
在做发送准备工作中
异步发送完毕
6 有的时候,要从异步中返回值,这个时候,spring会返回一个java.util.concurrent.Future对象,要调用其中的get方法,比如
[/code]
假如在网站的用户注册后,需要发送邮件,然后用户得到邮件确认后才能继续其他工作;
假设发送是一个很耗费时间的过程,因此需要异步。
1 namespace要注意,加上task
<?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”
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=”cs”/>
</beans>
2 RegularService.java 注册类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cs.async.MailUtility;
@Service
public class RegularService {
@Autowired
private MailUtility mailUtility ;
public void registerUser(String userName){
System.out.println(” User registration for “+userName +” complete”);
mailUtility.sendMail(userName);
System.out.println(” 注册完成,邮件稍后发送“);
}
}
3 发送邮件的工具类
[code="java"]
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class MailUtility {
@Async
public void sendMail(String name){
System.out.println(” 在做发送准备工作中 “);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(” 异步发送完毕“);
}
}
4 最后在applicationContext.xml中加入:
<?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=”cs”/>
<task:annotation-driven/>
</beans>
就是<task:annotation-driven/>这个一定不能少喔。
5 运行:
User registration for tom complete
注册完成,邮件稍后发送
在做发送准备工作中
异步发送完毕
6 有的时候,要从异步中返回值,这个时候,spring会返回一个java.util.concurrent.Future对象,要调用其中的get方法,比如
@Async
public Future<Balance> findBalanceAsync(final Account account) {
Balance balance = accountRepository.findBalance(account);
return new AsyncResult<Balance>(balance);
}
Balance balance = future.get();
[/code]