File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Queuing requests Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Queuing requests" Watch "Queuing requests" New topic

Queuing requests

Robin Heard

Joined: Sep 18, 2009
Posts: 18

This is the scenario I'm trying to achieve:

10 users use a java servlet to request data.
The data can only be retrieved by the server by running a script that generally take between 1-5 minutes.
What would be the best way to implement a queuing system so that when the first user sends a request the server goes ahead and starts the script. If during that time a second and third user issues a request they are queued in the order they came in as?

Are there any frameworks for this?

jagrutkumar dalwadi
Ranch Hand

Joined: Mar 26, 2009
Posts: 83

From your question, i seems that if particular resource has accessed, queuing mechanism needs to be set.

Servlets runs in multi threading environment. Here thread means user asking for resource. Please refer single threaded behavior of servlet.

If your requirement is for only one resource, you can create ServletRequestListener class which will handle such users. It will call for each and every request coming from user.
Jayesh A Lalwani
Saloon Keeper

Joined: Jan 17, 2008
Posts: 2746

What I would do is implement a Producer-Consumer pattern. You can have a queue that holds requests. A background thread waits for requests to appear on the queue. When a requests come, it picks the first one from the queue and executes it. After execution is done it checks for more requests in the queue. More requests = pop the next one execute it. No more requests = go back to sleep. The servlet should simply put the request in the queue and return back a message to the user that says "Your request is in queue. It is the Nth request in the queue, and it will approximately take T minutes". Provide another servlet that will allow the user to check the status of their request

I agree. Here's the link:
subject: Queuing requests
It's not a secret anymore!