35

I have been working with spring web applications using jetty/tomcat app server for around two years now, however the thing that eludes me still is how are multiple requests handled in these servers. I understand that spring is helpful in making singletons, but my understanding is just limited to that. Can someone point to any good resource that can help me understand how multiple requests are handled.

Ankit Dhingra
  • 6,534
  • 6
  • 31
  • 34

1 Answers1

39

This can be answered at so many levels I have been staring at it for two days trying to figure out how answer it...so I'll take a kinda high level shot at it.

There is this server port that jetty listens on and some number of acceptor threads whose job it is to get connection objects made between the client and server side. Once you have that connection it flows through the jetty handler architecture doing things like authentication perhaps, or pulling off a session id and attaching a session object to the request. Then it works its way into the servlet handler and the appropriate servlet is found and you start dealing with the servlet-api. At that point you have a thread allocated to your request for all of the time you are in the servlet-api, at least under servlet 2.5. In servlet 3.0 you have some async mechanisms available to you, or you can use jetty-continuations as a way to get async support on servlet 2.5 api.

Anyway, there is a thread pool that the server uses to allocate threads to those connectors which ultimately are the threads spending all their time in the servlet-api. The jetty continuations api and the newer servlet 3.0 support provide mechanisms to release threads back to the primary threadpool so they can spend time on accepting and processing other requests.

There is obviously a lot more going on under the covered related to usage of the nio api's and how jetty efficiently manages all of this stuff, but maybe this is enough to sate your initial question. If not, feel free to peruse the jetty docs (http://www.eclipse.org/jetty/documentation/current) or look to the jetty mailing lists. There has been some discussion on jetty-9 optimizations as it relates to under the covers with http, spdy, and websocket connection handling and processing in the blogs at Webtide (http://webtide.com/blogs).

jesse mcconnell
  • 7,102
  • 1
  • 22
  • 33
  • While I really appreciate the insight that you have provided. I am still curious about one thing. If there is a thread pool of request that are waiting to be executed, is only one thread currently through the piece of webapp code that I wrote in spring OR is it a possibility that I can have multiple copies of this code and I multiple requests can be served – Ankit Dhingra May 19 '12 at 08:05
  • 1
    servlets are not threadsafe and multiple threads can be walking through the same service() or doPost() methods, the 'state' is kept with the variables coming into the method and the thread itself. While you can make servlets themselves have state themselves and manage things with synchronized and whatnot you typically don't want your servlets to be single threaded, the whole idea is to be able to service multiple requests simultaneously and you can't do that when you have threads blocking on something as simple as a servlet. – jesse mcconnell May 19 '12 at 13:17
  • @jessemcconnell is there any documentation/write up/blog about the switch to asynchronous mode (and how it helps scare the number of requests) in servlet 3.0? – Ashwin Jan 23 '21 at 07:49