spring 3中的@async初探

在spring 3中,@Async注解能让某个方法快速变为异步执行,马上来先DEMO上手下。

假如在网站的用户注册后,需要发送邮件,然后用户得到邮件确认后才能继续其他工作;
假设发送是一个很耗费时间的过程,因此需要异步。

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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值