// this job can be processed.
BulkMutateJobPolicy policy = new BulkMutateJobPolicy();
policy.setPrerequisiteJobIds(new long[] {});
// Call mutate to create a new job.
SimpleMutateJob response =
mutateJobService.mutate(operations.toArray(new Operation[operations.size()]), policy);
long jobId = response.getId();
System.out.printf("Job with ID %d was successfully created.\n", jobId);
// Create selector to retrieve job status and wait for it to complete.
BulkMutateJobSelector selector = new BulkMutateJobSelector();
selector.setJobIds(new long[] {jobId});
// Poll for job status until it's finished.
System.out.println("Retrieving job status...");
SimpleMutateJob jobStatusResponse = null;
BasicJobStatus status = null;
for (int i = 0; i < RETRIES_COUNT; i++) {
Job[] jobs = mutateJobService.get(selector);
jobStatusResponse = (SimpleMutateJob) jobs[0];
status = jobStatusResponse.getStatus();
if (status == BasicJobStatus.COMPLETED || status == BasicJobStatus.FAILED) {
break;
}
System.out.printf("[%d] Current status is '%s', waiting %d seconds to retry...\n", i,
status.toString(), RETRY_INTERVAL);
Thread.sleep(RETRY_INTERVAL * 1000);
}
// Handle unsuccessful results.
if (status == BasicJobStatus.FAILED) {
throw new IllegalStateException("Job failed with reason: "
+ jobStatusResponse.getFailureReason());
}
if (status == BasicJobStatus.PENDING || status == BasicJobStatus.PROCESSING) {
throw new IllegalAccessException("Job did not complete within "
+ ((RETRIES_COUNT - 1) * RETRY_INTERVAL) + " seconds.");
}