How to poll asyncBatchAnnotateFilesAsync (Vision API) using getMetadata() - Java

Viewed 93

I am parsing a large PDF using Google Vision API asyncBatchAnnotateFilesAsync using Java Now I want to poll the request every 1 second to check the current status of the Async annotation request using getMetadata() method for the long-running operation Async call.

If I print(response.GetMetadat()), I get this output : [status=PENDING, info=[inputFuture=[com.google.api.core.AbstractApiFuture$InternalSettableFuture@ac76b42[status=PENDING]], function=[com.google.api.core.ApiFutures$ApiFunctionToGuavaFunction@268aa98c]]]

But I want to only capture status (status=PENDING) part of getMetadata(). I am not sure what variable datatype should I declare in Java to capture output of getMetadata() so I could parse it to get just "Status"

1 Answers

OperationFuture.getMetadata() offers the isDone() method to check for the completion of the batch job. The method returns (Boolean) true if the job is completed, false if the job is running. The job completion may be due to normal termination, an exception, or cancellation. Refer to the below code sample.

while(!response.getMetadata().isDone()){
    System.out.println("In Progress...");
    TimeUnit.SECONDS.sleep(1); // One second delay
}

However, if you want to parse the returned protobuf message, the toString() method can be used. The output of response.getMetadata().toString() is a string that can be checked for the presence of PENDING as a substring.

while(response.getMetadata().toString().contains("PENDING")) // Checking if the job is in the pending state.
{
    System.out.println("In Progress...");
    TimeUnit.SECONDS.sleep(1); // One second delay
}

if(response.getMetadata().toString().contains("SUCCESS"))
{
    System.out.println("\nProcess Done!");
}

if(response.getMetadata().toString().contains("FAILURE"))
{
    System.out.println("\nProcess Failed!");
}
Related