I want to test my mutate requests in BatchJobService using validateOnly=true before submitting. but I couldn't poll the job result using the batchId, it returns null from the following statement, is it possible to use validateOnly=true and test the mutate operations, and how can I poll the result?
BatchJobPage batchJobPage=batchJobService.get(selector);
this is my polling method, it is similar to the coding example.
private void pollJobResult(BatchJob batchJob,BatchJobServiceInterface batchJobService,BatchJobHelper batchJobHelper) throws Exception{
// Poll for completion of the batch job using an exponential back off.
int pollAttempts = 0;
boolean isPending = true;
Selector selector =
new SelectorBuilder()
.fields(BatchJobField.Id, BatchJobField.Status, BatchJobField.DownloadUrl,
BatchJobField.ProcessingErrors, BatchJobField.ProgressStats)
.equalsId(batchJob.getId())
.build();
do {
long sleepSeconds = (long) Math.scalb(30, pollAttempts);
logger.debug("Sleeping {} seconds...", sleepSeconds);
Thread.sleep(sleepSeconds * 1000);
BatchJobPage batchJobPage=batchJobService.get(selector);
if (batchJobPage!=null){
batchJob = batchJobPage.getEntries(0);
logger.info("Batch job ID {} has status {}.", batchJob.getId(), batchJob.getStatus());
pollAttempts++;
isPending = PENDING_STATUSES.contains(batchJob.getStatus());
}
} while (isPending && pollAttempts < MAX_POLL_ATTEMPTS);
if (isPending) {
throw new TimeoutException(
"Job is still in pending state after polling " + MAX_POLL_ATTEMPTS + " times.");
}
if (batchJob.getProcessingErrors() != null) {
int i = 0;
for (BatchJobProcessingError processingError : batchJob.getProcessingErrors()) {
logger.error( " Processing error {}: errorType={}, trigger={}, errorString={}, fieldPath={}"
+ ", reason=%s%n",
i++, processingError.getApiErrorType(), processingError.getTrigger(),
processingError.getErrorString(), processingError.getFieldPath(),
processingError.getReason());
}
}
/* if (batchJob.getDownloadUrl() != null && batchJob.getDownloadUrl().getUrl() != null) {
BatchJobMutateResponse mutateResponse =
batchJobHelper.downloadBatchJobMutateResponse(batchJob.getDownloadUrl().getUrl());
logger.info("Downloaded results from {}", batchJob.getDownloadUrl().getUrl());
for (MutateResult mutateResult : mutateResponse.getMutateResults()) {
String outcome = mutateResult.getErrorList() == null ? "SUCCESS" : "FAILURE";
logger.info(" Operation {} - {}", mutateResult.getIndex(), outcome);
}
}*/
if (batchJob.getDownloadUrl() != null && batchJob.getDownloadUrl().getUrl() != null) {
BatchJobMutateResponse mutateResponse =
batchJobHelper.downloadBatchJobMutateResponse(batchJob.getDownloadUrl().getUrl());
System.out.printf("Downloaded results from %s:%n", batchJob.getDownloadUrl().getUrl());
for (MutateResult mutateResult : mutateResponse.getMutateResults()) {
String outcome = mutateResult.getErrorList() == null ? "SUCCESS" : "FAILURE";
StringBuilder stringBuilder=new StringBuilder();
if (mutateResult.getErrorList()!=null){
for (ApiError error: mutateResult.getErrorList().getErrors()){
stringBuilder.append(error.getFieldPath()).append(" ").append(error.getErrorString()).append("\n");
}
}
logger.error(" Operation {} - status: {} reason:{} ", mutateResult.getIndex(), outcome, stringBuilder.toString());
}
}
}