Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Custom Thread Pool in a Servlet

 
seshu Palamanti
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assume that there is server side servlet which processes in coming requests from the web and the servlet container(Tomcat) would create or pull a thread from the pool and pass that thread to the servlet. How can I pool those threads coming in so as to allow only one or certain number of those in coming requests in other words those threads access to another resource lets a dao object? Thanks in advance for your help
 
Ron McLeod
Bartender
Pie
Posts: 963
59
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never tried this with Servlets, but you should be able to limit concurrent access to a resource using a Semaphore.

Create a Semaphore as a Servlet class member (or in the ServletContext or ApplicationContext - depending on the scope of the resource), specifying the maximum number on concurrencies allowed. In the handler methods, try to aquire a permit; if successful continue processing; if a timeout occurs, respond with a 503 Service Unavailable or perform some other action. You would probably want to set your timeout fairly short.

For example:
private Semaphore resourceSemaphore = new Semaphore(MAX_CONCURRENT_USES);
// ...

   if (resourceSemaphore.tryAcquire(ACQUIRE_TIMEOUT, TimeUnit.MILLISECONDS)) {
      // code for success
      // ...
      resourceSemaphore.release();
   } else {
      // code for timed-out
   }
 
seshu Palamanti
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Ron,
I have access to Spring with out getting to deep into Spring I know this class ThreadPoolTaskExecutor can do the same and assuming if object can do the job a semphore and I have this object in servlet
My question is can i get hold of every thread that is getting into servlet by calling as Thread.currentThread gets current thread & if I execute it via ThreadPoolTaskExecutor it should restrict the number of threads executing. Will the code snippet capture all threads that run by?. Thanks again for your reply.
 
Tim Holloway
Saloon Keeper
Posts: 18303
56
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand what you're hoping to accomplish, but it sounds dangerous.

The threads in Tomcat's servlet thread pool (or any other Tomcat thread pool) should not be tampered with. To assure adherance to the J2EE spec AND to ensure proper performance and reliability, you should neither attempt to "capture" and hold one of the Tomcat pool threads nor spawn threads of any kind (pooled or not) as children of any the Tomcat pool threads. Servlet threads should be entered (via call from Tomcat to the servlet's service() method), processed, and returned from as quickly as possible and should not attempt to acquire thread-specific resources (including child threads) or block and wait on long-running operations.

If you want to create your own private thread pool - say for running long-running processes - do that from a ServletContextListener. You can create as many threads (and thread pools) as you like there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic