从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注解的方法必须是直接被调用的那个方法,如果是一个内部调用方法的私有方法,该注解不会生效。

相关文章:

  • 2021-12-23
  • 2021-08-16
  • 2021-07-18
  • 2021-10-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-21
  • 2021-10-12
  • 2021-10-12
  • 2022-12-23
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案