Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Queuing incoming requests to a servlet

 
Nash Buckley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to queue incoming requests to a servlet and execute only a maximum of n requests at any time. I was thinking of using threads and queues but found out that is not recommended. Any suggestions?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to read the documentation for the container you're using.
Most have a maxThreads type of setting that does just this.
No need to code anything into your app.
 
Nash Buckley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Setting the maxThreads parameter would affect all web applications that are deployed on that instance of the server. Unfortunately, I do not have control over the server.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, the maxThreads can be applied (for example) to the connector, so that the available worker threads is defined for that port.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13077
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This appears to be an obvious application for a filter. See the javax.servlet.Filter interface documentation for examples or take a look at Sun's introductory page.

A filter could track all pending requests and create a queue or send an error message as desired.

Bill
 
Nash Buckley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@David: I will have to read the server documentation for defining connector and see if I can apply it.

@William: This sounds interesting, I need to look into ways as of how filters track pending requests. After all my main objective is to queue request and execute them accordingly.
 
Nash Buckley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a filter written by Tim Vernum as part of Jetty 7 that uses a list to queue requests and then resume them. However, it uses a deprecated interface called Continuation and suggests that the suspend/resume methods of the HttpServlet to be used. The problem is that these methods are part of resin implementation of the ServletRequest and not part of the standard Servlet API

Any suggestion as of how to perform the suspend and resume methods?
The code of the filter is here:
http://mortbay.org/jetty/jetty-7/xref/org/mortbay/servlet/ThrottlingFilter.html
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13077
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just thinking out loud here...

Seems to me that if you had a filter that incremented a counter when a request came in and decremented it when a response was sent, for each request you could look at that counter and:

1. counter below set level - dispatch to chain
2. counter at or above set level, sleep the Thread for n seconds and loop until 1.
3. counter at max - dispatch to a "too busy, try again later" page

Bill
 
Nash Buckley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, here is the doFinal method:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic