【发布时间】:2019-09-27 04:51:06
【问题描述】:
我使用 Spring Boot 创建了 Web 服务,其中有一个休息控制器,它通过基于供应商的 JDBC 驱动程序访问数据库并获取记录。在此过程中检索的记录数超过 80K 记录。因此,当我们以 client 的身份访问 rest 端点时,我们会遇到超时错误。
我已尝试使用以下教程设置异步调用。但不幸的是,其余调用仍然超时。
https://howtodoinjava.com/spring-boot2/enableasync-async-controller/
控制器
@RequestMapping(value = "/v1/lr/fullpositionasync", produces = {APPLICATION_JSON_UTF8_VALUE}, method = RequestMethod.GET)
@ResponseBody
public CompletableFuture<List<Position>> retrieveTradePositionsFullAsync(HttpServletRequest request, HttpServletResponse response) throws ExecutionException, InterruptedException {
CompletableFuture<List<Position>> positionList =null;
try {
positionList = positionService.getFullPosition();
}
catch(Exception e){
log.info("Error Occurred in Controller is:"+e.getMessage());
}
CompletableFuture.allOf(positionList).join();
log.info(String.valueOf(positionList.get()));
return positionList;
}
服务
@Service
@Slf4j
public class PositionServiceImpl implements PositionService {
@Autowired
private PositionDao positionDao;
@Async("asyncExecutor")
@Override
public CompletableFuture<List<Position>> getFullPosition() {
List<Position> fullpositionList = null;
log.info("Getting the full Position process started");
fullpositionList = positionDao.retrieveData();
log.info("Total Positions retrieved:"+fullpositionList.size());
try {
log.info("Thread is about to sleep 1000 milliseconds");
Thread.sleep(1000);
}catch(InterruptedException e){
log.info(e.getMessage());
}
log.info("Full Positions retrieval completed");
return CompletableFuture.completedFuture(fullpositionList);
}
}
配置
@Configuration
@EnableAsync
@Slf4j
public class AsyncConfiguration
{
@Bean(name = "asyncExecutor")
public Executor asyncExecutor()
{
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(20);
executor.setMaxPoolSize(1000);
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.setThreadNamePrefix("AsynchThreadForEndPoint-");
executor.initialize();
log.info("Executor is :"+executor.toString());
return executor;
}
}
道
@Repository
public class PositionDaoImpl implements PositionDao {
@Autowired
private JdbcTemplate jdbcTemplate;
private static final String ALL_POSITION_QUERY = "call AllPositionProcedure()";
public List<Position> retrieveData() {
return jdbcTemplate.query(ALL_POSITION_QUERY, new BeanPropertyRowMapper(Position.class));
// List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
}
【问题讨论】:
-
嗨!请问你想用这些记录做什么?那么你返回 80K 记录,它们是如何使用的呢?在 UI 中显示?还是出口?返回 80K 记录的目的是什么?
-
我正在尝试创建 JSON 输出,因为另一个 Web 服务正在尝试访问我们的服务以获取数据。这是一天加载的开始。
-
您能否使用分块响应和分页浏览 JDBC 结果,在处理完每个页面后将其发送回客户端?
-
但是执行数据库查询的 JDBC 供应商驱动程序返回单个数据块,而不是通过结果分页
标签: rest spring-boot asynchronous