I have some java code which calls an asynchronous REST API to finish a task that takes a while to complete, this call returns a task ID. We would need to use the task ID and trigger another REST call to retrieve the task completion status. Please suggest the best mechanism to poll the status using java.

Member Avatar for Kubilay Doğukan

Found some examples and simplified it according to your need. Modify it to fit your code. Hope this is what you are looking for.

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class AsynchronousWorker {
    public AsynchronousWorker() {
    }

    public static void main(String[] args) {
    ExecutorService es = Executors.newFixedThreadPool(3);
        final Future future = es.submit(new Callable() {
                    public Object call() throws Exception {
                        //REST API call for Task ID
                        return null;
                    }
                });
        try {
            future.get(); // blocking call - the main thread blocks until task is done
                          //make sure you get the Task ID
            ...
            //REST API call for status.
        } catch (InterruptedException e) {
        } catch (ExecutionException e) {
        }
    }
}

Hey thanks for your suggestion. I will try this out and get back!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.