spring 通過TaskExecutor實現異步
配置類
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
/**
* Created by RZL on 2018/4/13.
*/
@Configuration
@EnableAsync//開啓異步註解任務支持
public class TaskExecutorConfig implements AsyncConfigurer {
/**
* 配置類實現AsyncConfigurer接口並重寫getAsyncExecutor方法,並返回一個ThreadPoolTaskExecutor
* 這樣就獲取到了一個基於線程池TaskExecExceutor
* @return
*/
@Override
public Executor getAsyncExecutor(){
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
//核心線程數
taskExecutor.setCorePoolSize(5);
//最大線程數
taskExecutor.setMaxPoolSize(10);
//隊列大小
taskExecutor.setQueueCapacity(20);
taskExecutor.initialize();
return taskExecutor;
}
}
業務類
import com.example.myproject.async.AsyncDemo1;
import com.example.myproject.async.AsyncDemo2;
import com.example.myproject.async.AsyncDemo3;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by RZL on 2018/4/13.
*/
@Slf4j
@Service
public class AsyncService {
@Autowired
private AsyncDemo1 asyncDemo1;
@Autowired
private AsyncDemo2 asyncDemo2;
@Autowired
private AsyncDemo3 asyncDemo3;
public void AsyncService(){
asyncDemo1.demo1();
asyncDemo2.demo2();
asyncDemo3.demo3();
}
}
異步任務類
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* Created by RZL on 2018/4/13.
*/
@Async//註解在類級別,則表明該類所有的方法都是異步,這裏的方法自動被注入使用ThreadpoolTaskExecutor作爲TaskExecutor
@Slf4j
@Component
public class AsyncDemo1 {
public void demo1(){
for(int i=0;i<5;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("demo1:{}",i);
}
}
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* Created by RZL on 2018/4/13.
*/
@Slf4j
@Component
public class AsyncDemo2 {
public void demo2(){
for(int i=0;i<5;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("demo2:{}",i);
}
}
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* Created by RZL on 2018/4/13.
*/
@Async
@Slf4j
@Component
public class AsyncDemo3 {
public void demo3(){
for(int i=0;i<5;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("demo3:{}",i);
}
}
}
控制層
@RequestMapping("async")
public void async(){
asyncService.AsyncService();
}
輸出結果
2018-04-13 10:34:40.142 INFO 6160 --- [nio-8888-exec-1] com.example.myproject.async.AsyncDemo2 : demo2:0
2018-04-13 10:34:40.144 INFO 6160 --- [lTaskExecutor-1] com.example.myproject.async.AsyncDemo1 : demo1:0
2018-04-13 10:34:41.142 INFO 6160 --- [nio-8888-exec-1] com.example.myproject.async.AsyncDemo2 : demo2:1
2018-04-13 10:34:41.144 INFO 6160 --- [lTaskExecutor-1] com.example.myproject.async.AsyncDemo1 : demo1:1
2018-04-13 10:34:42.142 INFO 6160 --- [nio-8888-exec-1] com.example.myproject.async.AsyncDemo2 : demo2:2
2018-04-13 10:34:42.144 INFO 6160 --- [lTaskExecutor-1] com.example.myproject.async.AsyncDemo1 : demo1:2
2018-04-13 10:34:43.142 INFO 6160 --- [nio-8888-exec-1] com.example.myproject.async.AsyncDemo2 : demo2:3
2018-04-13 10:34:43.144 INFO 6160 --- [lTaskExecutor-1] com.example.myproject.async.AsyncDemo1 : demo1:3
2018-04-13 10:34:44.142 INFO 6160 --- [nio-8888-exec-1] com.example.myproject.async.AsyncDemo2 : demo2:4
2018-04-13 10:34:44.144 INFO 6160 --- [lTaskExecutor-1] com.example.myproject.async.AsyncDemo1 : demo1:4
2018-04-13 10:34:45.148 INFO 6160 --- [lTaskExecutor-2] com.example.myproject.async.AsyncDemo3 : demo3:0
2018-04-13 10:34:46.148 INFO 6160 --- [lTaskExecutor-2] com.example.myproject.async.AsyncDemo3 : demo3:1
2018-04-13 10:34:47.148 INFO 6160 --- [lTaskExecutor-2] com.example.myproject.async.AsyncDemo3 : demo3:2
2018-04-13 10:34:48.148 INFO 6160 --- [lTaskExecutor-2] com.example.myproject.async.AsyncDemo3 : demo3:3
2018-04-13 10:34:49.148 INFO 6160 --- [lTaskExecutor-2] com.example.myproject.async.AsyncDemo3 : demo3:4
從打印結果來看 demo1與demo2同時進行輸出結果,demo3是在前兩者運行完畢後再輸出的。
這是因爲在demo1中加入了@Async註解,所以會與demo2同時進行,而demo2中沒有加入@Async註解,所以demo3只有在deno2運行結束後纔會運行