• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Async servlets (Servlet 3.0) and threads

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic