4

Given a microservice in Spring Boot, it offers 2 end-points to be consumed from 2 separate system. One of this system is critical while the other one is not. I would like to prevent the "not critical" one to consume (due to unexpected problems) all the threads (or many) of the HTTP thread pool, so I would like to configure separated thread pools for each one of these end-points.

Is that possible?

Mr.Eddart
  • 10,050
  • 13
  • 49
  • 77

2 Answers2

2

There are multiple ways to do this. Using DeferredResult is probably the easiest way:

@RestController
public class Controller {

    private final Executor performancePool = Executors.newFixedThreadPool(128);
    private final Executor normalPool = Executors.newFixedThreadPool(16);

    @GetMapping("/performance")
    DeferredResult<String> performanceEndPoint() {
        DeferredResult<String> result = new DeferredResult<>();
        performancePool.execute(() -> {
            try {
                Thread.sleep(5000); //A long running task
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            result.setResult("Executed in performance pool");
        });
        return result;
    }

    @GetMapping("/normal")
    DeferredResult<String> normalEndPoint() {
        DeferredResult<String> result = new DeferredResult<>();
        normalPool.execute(() -> result.setResult("Executed in normal pool"));
        return result;
    }
}

You immediately release the Tomcat thread by returning a DeferredResult from a controller, allowing it to serve other requests. The actual response is written to the user when the .setResult method is called.

DeferredResult is one of the many ways you can perform asynchronous request processing in Spring. Check out this section of the docs to learn more about the other ways:

https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-async

alchemist
  • 71
  • 1
  • 2
  • 7
0

Not sure you can prevent, but you can surely increase the thread pool capacity. By default, tomcat (if default server) can handler 200 simultaneous requests , you can increase that number

Check if this article helps

https://stackoverflow.com/questions/46893237/can-spring-boot-application-handle-multiple-requests-simultaneously#:~:text=Yes%2C%20Spring%20boot%20can%20handle,can%20handle%20200%20simultaneous%20requests.&text=However%2C%20you%20can%20override%20this,tomcat.

Harsh
  • 812
  • 1
  • 10
  • 23