I have completed the following Java Servlet construction: [I believe the main idea is that of consumer-producer]
-
A Java servlet that receives multiple http POST requests
-
It puts all requests into a Queue [ConcurrentLinkedQueue]
-
All requests in the Queue are processed by a single and independent thread (like an engine). This thread works independently of the servlets.
There is a good reason for the separate thread, since I need data from several and different http requests to do the processing. The Queue waits until it has enough requests and then starts the processing.
Now I’m in the final step: Once e.g. http request No1 has been processed by the independent thread engine, I need to notify the specific servlet thread (e.g. No1) where it came from so that I can reply, i.e. send the response back to the corresponding client.
How do I solve this threading issue? How can the single thread engine notify the correct servlet thread each time? How should I code this?
I don’t know where are these requirements coming from, but there are several other, much simpler approaches to this problem:
Use
ExecutorService. Simply submit a task to the pool and block on returnedFutureobject. Very simple and effective. Once your task is ready,Future.get()will return the resultWith Servlet 3.0 you can put whole processing in asynchronous thread. This is much more scalable. Basically you are submitting a task and releasing HTTP thread immediately. The asynchronous thread does not only process items in that queue but also returning HTTP response via
AsyncContextobject.If you really need to use a queue and a separate thread, have a look at Java locks and conditions. But that’s much more low-level work.