0

i have a jersey service which contains a timer task, which polls for a particular value. i want to return a response only when i get a particular value from the polling. is there any way i can accomplish that?

My code is:

@Path("/poll")
public class PollService{
    String response = null;
    @GET
    @Produces(javax.ws.rs.core.MediaType.TEXT_PLAIN)
    public String pollResponse(@Context HttpServletRequest request){
       MyTimer poller = new MyTimer();
       final Timer timer = new Timer();
       timer.scheduleAtFixedRate(poller, 0, 5000);
       return response;
    }
    private class MyTimer extends TimerTask{
         @Override
         public void run(){
             //Poll
             //Change value of response upon condition
         }
    }

I don't want the service to return any value until the value of "response" variable is set to say "SUCCESS". what may I do to accomplish that?

Gaurav Sood
  • 680
  • 4
  • 17
  • 38

2 Answers2

1

One of the approach you may take:

  1. Divide your request into two requests
  2. First request will accept the request and will send back 202 Accepted response. 202 resposne will provide a status/polling url to poll for the async task result
  3. Second request will poll on the status/polling url provided. In response to it, you may return the response as 200 OK with body IN_PROGRESS or actual task result if finished.
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

you can use Callable whose call method returns value.Please take a look on Interface CompletionService & Class ExecutorCompletionService.

Anil
  • 1,470
  • 5
  • 24
  • 35