Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threading and Concurrency Within A Servlet

I have a web application that retrieves a (large) list of results from the database, then needs to pare down the list by looking at each result, and throwing out "invalid" ones. The parameters that make a result "invalid" are dynamic, and we cannot pass the work on to the database.

So, one idea is to create a thread pool and ExecutorService and check these results concurrently. But I keep seeing people saying "Oh, the spec prohibits spawning threads in a servlet" or "that's just a bad idea".

So, my question: what am I supposed to do? I'm in a servlet 2.5 container, so all the asynchrous goodies as part of the 3.0 spec are unavailable to me. Writing a separate service that I communicate with via JMS seems like overkill.

Looking for expert advice here.

Jason

like image 641
Jason Avatar asked May 12 '14 19:05

Jason


People also ask

What is servlet concurrency?

That means, that multiple requests to the same servlet may be executed at the same time.

What is thread in servlet?

The general pattern for a Servlet container is to use one Thread to handle one request. for ex i have 2 servlets servlet 1 and servlet 2 both are getting rquest from the same html form one is getting through anchor tag and another through form. When you submit the form , the browser sends an HTTP request.

What are the threading issues in servlets?

6.5. Most servlet threading problems occur when two or more threads make changes to the same resource. This might mean that two threads try to modify a file, or perhaps several threads all update the value of a shared variable at the same instant. This causes unpredictable behavior and can be very hard to diagnose.

How are servlets multithreaded?

The servlet is protected from concurrent call until the initialisation is complete. Once this has been done, all calls are executed simutaneously on your servlet. This means the service method can be invoked by two concurrent threads if two requests need to be processed at the same time.


1 Answers

Nonsense.

The JEE spec has lots of "should nots" and "thou shant's". The Servlet spec, on the other hand, has none of that. The Servlet spec is much more wild west. It really doesn't dive in to the actual operational aspects like the JEE spec does.

I've yet to see a JEE container (either a pure servlet container ala Tomcat/Jetty, or full boat ala Glassfish/JBoss) that actually prevented me from firing off a thread on my own. WebSphere might, it's supposed to be rather notorious, but I've not used WebSphere.

If the concept of creating unruly, self-managed threads makes you itch, then the full JEE containers internally have a formal "WorkManager" that can be used to peel threads off of. They just all expose them in different ways. That's the more "by the book-ish" mechanism for getting a thread.

But, frankly, I wouldn't bother. You'll likely have more success using the Executors out of the standard class library. If you saturate your system with too many threads and everything gets out of hand, well, that's on you. Don't Do That(tm).

As to whether an async solution is even appropriate, I'll punt on that. It's not clear from your post whether it is or not. But your question was about threads and Servlets.

Just Do It. Be aware it "may not be portable", do it right (use an Executor), take responsibility for it, and the container won't be the wiser, nor care.

like image 52
Will Hartung Avatar answered Nov 15 '22 02:11

Will Hartung