我正在尝试创建一个restapi,它必须异步执行后台数据库获取操作,并且在不等待后台执行完成的情况下将事务id返回给调用者。我目前已经使用executor实现了这个。但是当父线程在可运行任务执行之前完成操作时,我得到一个空指针异常。有什么解决办法吗?在这里,甚至在db操作开始执行之前就调用了return responseentity。
@Configuration
@EnableAsync
public class AsynchConfig {
@Bean(name = "taskExecutor")
public Executor taskExecutor()
{
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(100);
executor.setThreadNamePrefix("CurrentThread -");
return executor;
}
rest api如下:
@ApiOperation("")
public ResponseEntity<?> uploadCSVSendCommunication(@RequestParam String parameters)
{
asyncConfig.taskExecutor().execute(() ->
{
//perform DB validations in a service
} );
asyncConfig.taskExecutor().execute(() ->
{
//Insert a record into transaction table
if(!statusId.isEmpty())
//set the status object;
});
return new ResponseEntity<>(statusObject, HttpStatus.OK);
}
暂无答案!
目前还没有任何答案,快来回答吧!