0

My "queryService.query()" make take a time that exceeds a limit specified by the requirement. I want to be able to skip the execution of the current function all after 1 second and then continue to process of the next "inputInfo". If a task takes too long, I want to skip it. What's the best way to do this?

  public Response getResponse(InputInfo inputInfo) {

        Preconditions.checkNotNull(inputInfo);
        LOGGER.info("inputInfo: " + inputInfo);

        Response response = queryService.query(inputInfo);

        return response;
    }
user697911
  • 10,043
  • 25
  • 95
  • 169
  • You can't cancel it unless the service cooperates with you. But you can skip it by running it on another thread and waiting up to a second for it to complete. – shmosel May 18 '18 at 18:32
  • @shmosel, what i want is to discard the current task and continue with the next one after a specified time. – user697911 May 18 '18 at 18:34
  • @shmosel can you please write an answer as you suggested? Thank you. – user697911 May 18 '18 at 18:37
  • Do you need to cancel the task or just ignore it? Can you tell us more about this query service? – shmosel May 18 '18 at 18:38
  • The queryService takes the input, processes it, and then return a Response object. If it takes too long, I want to be able to skip the current task, so that it won't continue to consume resources, and free up resources to process the next input. – user697911 May 18 '18 at 18:50
  • What's the difference between skip and cancel? – user697911 May 18 '18 at 18:50
  • Whether you're ok with letting the task complete in the background. – shmosel May 18 '18 at 18:51
  • You cannot really force the called method to terminate without changing the method, but you can return from the call on the timeout (letting the original call run in the background until it completes) and do something else. If this is ok, then also please clarify if you want to call next task on the same input or this same task on next input, so clarify what should be done next. Then I guess I could help. – Oleg Sklyar May 18 '18 at 19:03
  • @OlegSklyar, I want it to release the resources occupied and process next input on the same task. – user697911 May 18 '18 at 21:24

0 Answers0