@Async的使用

從Spring3.x 開始,加入@Async這個註解,用戶異步線程處理,使用起來很方便。spring

使用配置以下:spring-task.xml異步

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

使用處:ide

在被調用的方法上增增長@Async的註解,無返回值實例片斷spa

@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"); 
} 

注意事項code

使用@Async註解的方法必須是直接被調用的那個方法,若是是一個內部調用方法的私有方法,該註解不會生效。xml

相關文章
相關標籤/搜索