I am adding a new endpoint to a Jersey-based web-service. The logic backing the endpoint needs to make between 10 to 50 calls to another service. The calls are independent and can be parallelized, so I was thinking of using executor service to distribute the work across multiple threads.
I am wondering if I should instantiate an executorService for each request or should there be a shared executorService instance across the webapp. In the later case, how would I decide the number of threads it should run?
No. In general, you should NOT instantiate the
executorService
for each web request (option-1) because the server will soon run out of memory and also, thread pool creation on the fly is costly (time-consuming).So you need to go with the shared instance (option-2) of the
executorService
by creating it during server startup & configure the thread pool size according to your requirements and by conducting the performance tests.You can refer here to understand on the thread pools.