controller异步处理请求

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();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章