Search code examples
javapolling

Polling for a job status java


I'm looking for a way to check a job status over a period of time: If during the polling I get a result that the job was completed, I return it, otherwise I keep polling until the period I set is over and return a failure result. I know how to do this using a timer and a while loop. Is there a better way of doing it? Thanks in advance


Solution

  • Better way to pool for the result should be using CompletionService.

    Since you are already using asynchronous way of communication, make your thread implement Callable and implement call method similar to what you would do in a run method.

    Now when you use completion service and to your executor service just say submit. With completion Service you get a blocking queue on which as and when you get the result completion service will put the result onto Queue and you can then do whatever you want with the result.