• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Custom Thread Pool in a Servlet

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 3868
541
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 25646
183
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic