Callable asynchronous request to improve system throughput
Scenario query agent distribution information, paging query, download query, detailed query, the code is as follows:
Note: It can improve the throughput of the interface, and improve the performance of the interface to a certain extent.
Why do you say that? There are many factors that affect the performance of the interface, such as: bandwidth, code quality, and database bottlenecks.
All without considering other conditions, the performance of the interface can be improved
@PostMapping(value = "/findAll")
@LogAnnotion
public Callable<UnionResp> findAll(@RequestBody UnionReq<ShareConfigQry> req) throws IOException {
return () -> {
ShareConfigQry dto = reqUtils.getContentObject(req, ShareConfigQry.class).orElseThrow(() ->new ShareException(RespCodeEnum.PARAMS_EXCEPTION.getCode(),"请求参数不能为空"));
validateParam(dto);
QueryTypeEnum queryTypeEnum = QueryTypeEnum.getByCode(dto.getQueryFlag());
UnionResp response = null;
switch (queryTypeEnum) {
case PAGE:
Integer pageNum = Optional.ofNullable(req.getPage()).map(Page::getPageNum).orElse(Constants.DEFAULT_PAGE_MIN);
Integer pageSize = Optional.ofNullable(req.getPage()).map(Page::getPageSize).orElse(Constants.DEFAULT_PAGE_MAX);
response = msoShareConfigService.page(dto,pageNum,pageSize);
break;
case DETAIL:
response = msoShareConfigService.getById(dto.getId());
break;
case DOWNLOAD:
Integer downLoadMin = Constants.DOWN_LOAD_MIN;
Integer downLoadMax = Constants.DOWN_LOAD_MAX;
response = msoShareConfigService.page(dto, downLoadMin, downLoadMax);
break;
default:
break;
}
return response;
};
}
Non-lamdom expressions are more intuitive:
@PostMapping(value = "/findAll")
@LogAnnotion
public Callable<UnionResp> findAll(@RequestBody UnionReq<ShareConfigQry> req) throws IOException {
Callable<UnionResp> callable = new Callable<UnionResp>() {
@Override
public UnionResp call() throws Exception {
ShareConfigQry dto = reqUtils.getContentObject(req, ShareConfigQry.class).orElseThrow(() ->new ShareException(RespCodeEnum.PARAMS_EXCEPTION.getCode(),"请求参数不能为空"));
validateParam(dto);
QueryTypeEnum queryTypeEnum = QueryTypeEnum.getByCode(dto.getQueryFlag());
UnionResp response = null;
switch (queryTypeEnum) {
case PAGE:
Integer pageNum = Optional.ofNullable(req.getPage()).map(Page::getPageNum).orElse(Constants.DEFAULT_PAGE_MIN);
Integer pageSize = Optional.ofNullable(req.getPage()).map(Page::getPageSize).orElse(Constants.DEFAULT_PAGE_MAX);
response = msoShareConfigService.page(dto,pageNum,pageSize);
break;
case DETAIL:
response = msoShareConfigService.getById(dto.getId();
break;
case DOWNLOAD:
Integer downLoadMin = Constants.DOWN_LOAD_MIN;
Integer downLoadMax = Constants.DOWN_LOAD_MAX;
response = msoShareConfigService.page(dto, downLoadMin, downLoadMax);
break;
default:
break;
}
return response;
}
};
return callable;
}