Hi ranchers. I have some trouble understanding the added value of async
servlets / filters. Here is the deal: I have a servlet, and it has to wait for some action, like a return from a database or a web service endpoint, which can take time. So, instead of wasting the
thread serving the request, my servlet starts async processing, and its thread returns immediately ready to serve other requests. This is supposed to add to server scalability and so on.
What I don't seem to grasp is how it is supposed to work? Most of the examples I have seen either start a new thread that does stuff to the AsyncContext instance returned, and simply starts it (see
Tomcat 7 examples in $CATALINA_HOME/webapps/examples/WEB-INF/classes/async) or use some ThreadExecutor created by themselves, and there even is a AsyncContext.start(Runnable) method to take care of the thread and make it managed and possibly add some
Java EE services to it (like security context propagation). So, this new thread has to wait instead of the container-managed one (but it can also be container-managed!). So what's the point of this? Wouldn't it be easier to just add threads to the container, so that it has more of them to handle the requests? Of course, you can't just add threads with no limits, that's why this async stuff was invented, right? But async requests require new threads anyways - how does this differ? If the threads are not container managed, I try to create more and more, and kill the machine. If they are container-managed, they wait whereas other requests could be served by them.
I am obviously missing something important here, as I must be wrong in my understanding. Please, ranchers, guide me to understanding async servlets!