我写了一个处理列表列表的春季批处理作业。
阅读器返回列表列表。处理器在每个 ListItem 上工作并返回已处理的 List。Writer 从 List of List 将内容写入 DB 和 sftp。
我有一个用例,我从 spring 批处理器调用 Async REST api。在 ListenableFuture 响应中,我实现了 LitenableFutureCallback 来处理成功和失败,它按预期工作,但在异步调用返回某些内容之前,ItemProcessor 不会等待来自异步 api 的回调并将对象(列表)返回给 writer。
我不确定如何实现和处理来自 ItemProcessor 的异步调用。
我确实阅读了有关 AsyncItemProcessor 和 AsyncItemWriter 的信息,但我不确定这是否是我应该在这种情况下使用的东西。
我还想过从 AsyncRestTemplate 对 ListenableFuture 响应调用 get(),但根据文档,它会阻塞当前线程,直到收到响应。
我正在寻求有关如何实现此功能的帮助。下面的代码片段:
处理器:
public class MailDocumentProcessor implements ItemProcessor<List<MailingDocsEntity>, List<MailingDocsEntity>> {
... Initialization code
@Override
public List<MailingDocsEntity> process(List<MailingDocsEntity> documentsList) throws Exception {
logger.info("Entering MailingDocsEntity processor");
List<MailingDocsEntity> synchronizedList = Collections.synchronizedList(documentsList);
for (MailingDocsEntity mailingDocsEntity : synchronizedList) {
System.out.println("Reading Mailing id: " + mailingDocsEntity.getMailingId());
..code to get the file
//If the file is not a pdf convert it
String fileExtension = readFromSpResponse.getFileExtension();
String fileName = readFromSpResponse.getFileName();
byte[] fileBytes = readFromSpResponse.getByteArray();
try {
//Do checks to make sure PDF file is being sent
if (!"pdf".equalsIgnoreCase(fileExtension)) {
//Only doc, docx and xlsx conversions are supported
...Building REquest object
//make async call to pdf conversion service
pdfService.convertDocxToPdf(request, mailingDocsEntity);
} else {
logger.error("The file cannot be converted to a pdf.\n"
);
}
}
} catch (Exception ex){
logger.error("There has been an exception while processing data", ex);
}
}
return synchronizedList;
}
}
异步 PdfConversion 服务类:
@Service
public class PdfService{
@Autowired
@Qualifier("MicroServiceAsyncRestTemplate")
AsyncRestTemplate microServiceAsyncRestTemplate;
public ConvertDocxToPdfResponse convertDocxToPdf(ConvertDocxToPdfRequest request, MailingDocsEntity mailingDocsEntity){
ConvertDocxToPdfResponse pdfResponse = new ConvertDocxToPdfResponse();
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<?> entity = new HttpEntity<>(request, headers);
ListenableFuture<ResponseEntity<ConvertDocxToPdfResponse>> microServiceResponse = microServiceAsyncRestTemplate.postForEntity(batchMailProcessingConfiguration.getPdfUrl(), entity, ConvertDocxToPdfResponse.class);
ConvertDocxToPdfResponse resultBody = microServiceResponse.get().getBody();
microServiceResponse.addCallback(new ListenableFutureCallback<ResponseEntity<ConvertDocxToPdfResponse>>() {
@Override
public void onSuccess(ResponseEntity<ConvertDocxToPdfResponse> result) {
...code to do stuff on success
}
@Override
public void onFailure(Throwable ex) {
pdfResponse.setMessage("Exception while retrieving response");
}
});
} catch (Exception e) {
String message = "There has been an error while issuing a pdf generate request to the pdf micro service";
pdfResponse.setMessage(message);
logger.error(message, e);
}
return pdfResponse;
}
}
我最初的批处理作业是同步的,我正在转换为异步以加快处理速度。我确实尝试寻找类似的问题,但找不到足够的信息。任何指示或帮助都将受到高度赞赏。
谢谢!!