Callable异步请求,提高系统的吞吐量
场景查询代理商分润信息,分页查询、下载查询、明细查询,代码如下:
注:能提高接口的吞吐量,在一定程度上能提高接口的性能。
为什么这么说呢? 影响接口的性能的因素有很多,如:带宽、代码质量、数据库瓶颈
所有不考虑其他情况下,可以提高接口的性能
@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;
};
}
非lamdom表达式更直观:
@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;
}