2

I'm working on a legacy system where they have Thread.sleep in the controller. The scenario here is, once the request is received it polls another service until the criteria are met. The problem here is request processing thread is blocked because of polling

I'm trying to replace it with DeferredResult which avoids blocking and uses callback-based method. The polling will be in a separate thread and once completed setResult will be called and response will be given to the user.

Does it actually make sense to use DeferredResult for polling with an interval? Is there any impact on performance on load?

This is code:

    while (status.equals("RUNNING")) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log.error("Error while polling for status setting Thread to sleep.", e);
        }
        status = requestStatus();
    }

The one which I'm trying to improve is based on this example

Thiru
  • 2,541
  • 4
  • 25
  • 39
  • Why don't you create another Thread inline (anonymous class) and then make it sleep? – Ademir Constantino Sep 21 '18 at 14:12
  • Do you have a simple code example you can share? Its going to be difficult for someone to answer definitively without. – theMayer Sep 21 '18 at 14:16
  • Have updated the question @theMayer – Thiru Sep 21 '18 at 14:20
  • @AdemirConstantino what will happen to the current HTTP thread? – Thiru Sep 21 '18 at 14:22
  • OK, but that doesn't answer my question. What is wrong with your code? – theMayer Sep 21 '18 at 14:22
  • There is nothing wrong with the code. My question is , will using `DeferredResult` by any chance improve the performance of the service or not? – Thiru Sep 21 '18 at 14:24
  • @Thiru you need to lock the response until the response is ready. So with that you don't lock a 'FrontController'. – Ademir Constantino Sep 21 '18 at 14:24
  • I don't think that is a valid question. Whether or not to use `DeferredResult` is a behavioral question, not a performance question. What behavior do you need? If you have a performance issue, what performance issue do you have? – theMayer Sep 21 '18 at 14:24

0 Answers0