1,需求場景
後臺數據處理需要多個遠程調用,數據拉取推送等IO操作很耗時或者時間預估困難,手動重新推送或拉取數據等操作。
2,處理器異步處理請求
@RestController
@RequestMapping("/pulldata")
@Slf4j
public class PullOrderController {
@Autowired
private TestService testService;
/**
* 定義任務線程池,手動處理的情況比較少,不需要太多線程數
*/
private static ExecutorService threadPool = new ThreadPoolExecutor(1, 5, 1000, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
/**
* 拉取調撥單
*
* @param sapRequestPO
* @return
*/
@PostMapping(value = "test")
public Result list(@RequestBody SapRequestPO sapRequestPO) {
log.debug("主線程開始");
Callable<Boolean> callable = new Callable<Boolean>() {
@Override
public Boolean call(){
log.debug("副線程開始");
boolean res = testService.pullStockOutOrderByQm(
sapRequestPO.getFromWho(),
sapRequestPO.getShopno(),
sapRequestPO.getSaledate(),
sapRequestPO.getOrderno());
log.debug("副線程結束");
return res;
}
};
threadPool.submit(callable);
log.debug("主線程結束");
return Result.success();
}
}