@Async的使用

从Spring3.x 开始,加入@Async这个注解,用户异步线程处理,使用起来很方便。

使用配置如下:spring-task.xml

<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" />

使用处:

在被调用的方法上增增加@Async的注解,无返回值实例片段

@Async
@Override
public void sendMessage(Long phone, Integer type, Integer batch) throws Exception {}

有返回值:

@Async
@Override
public Future<String> sendMessage(Long phone, Integer type, Integer batch) throws Exception{

        ....

        return AsyncResult<String>("SUCCESS"); 
} 

注意事项

使用@Async注解的方法必须是直接被调用的那个方法,如果是一个内部调用方法的私有方法,该注解不会生效。

原文地址:https://www.cnblogs.com/blacksonny/p/7087184.html