我了解异步服务的一些概念,但还没有编写过很多代码,因此特别是java中的机制对我来说是新的。基本上,我有一个长期运行的服务,我想分岔到另一个线程,并能够使用不同的服务检查它的状态。现在,我可以开始工作了,我还没有办法检查。但更糟糕的是:
在邮局 asyncUploadSoftLayerFile
下:
@RestController
@RequestMapping("/")
public class MyController {
...
@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/async-files")
public DeferredResult<ResponseEntity<JobExecutionResult>> asyncUploadSoftLayerFile(@RequestParam MultipartFile file) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, IOException, JobExecutionAlreadyRunningException{
logger.info("Received async-CompleteableFuture request");
DeferredResult<ResponseEntity<JobExecutionResult>> output = new DeferredResult<ResponseEntity<JobExecutionResult>>();
ForkJoinPool.commonPool().submit(() -> {
logger.info("Processing in separate thread: Thread-ID="+Thread.currentThread().getId());
JobExecutionResult jobExecutionResult = null;
try {
myReallyLongRunningProcess();
} catch (JobParametersInvalidException | JobExecutionAlreadyRunningException | JobRestartException
| JobInstanceAlreadyCompleteException | IOException e) {
logger.error("Error processing /async-files upload in Thread-ID: "+Thread.currentThread().getId(),e);
throw new RuntimeException(e);
}
if (!"COMPLETED".equals(jobExecutionResult.getExitStatus())) {
throw new UploadFileException(file.getOriginalFilename() + " exit status: " + jobExecutionResult.getExitStatus());
}
ResponseEntity<JobExecutionResult> responseEntity = ResponseEntity.ok(jobExecutionResult);
output.setResult(responseEntity);
});
return output;
}
}
spring确实做了延迟的事情,我可以看到它将工作转移到了另一个线程。但它并没有返回给打电话的人。相反,我看到:
2021-07-27 05:20:00 DEBUG o.s.web.servlet.DispatcherServlet - Exiting but response remains open for further handling
所以它只是部分地满足了我的需求。我如何让spring将工作转移到另一个线程(理想情况下能够通过另一个web服务调用引用该进程),但立即向web浏览器返回某种响应?
这是开始工作的一部分,但要跟踪完成的百分比。
暂无答案!
目前还没有任何答案,快来回答吧!