This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Configuring a single servlet instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Configuring a single servlet instance" Watch "Configuring a single servlet instance" New topic
Author

Configuring a single servlet instance

Henry Cho
Greenhorn

Joined: Nov 10, 2003
Posts: 2
I was wondering how to configure my app server to instantiate only one instance of a particular servlet. I need to ensure that no matter how many requests come in for a particular servlet, that the requests get processed one at a time (I'm calling a third party library that is not thread-safe). I have defined my servlet as SingleThreadModel which means that only one request will be processed by a particular instance but that does not guarantee that the app server will not create multiple servlet instances, making it possible that two requests will be processed by separate instances.

I believe the answer is app server-specific, so I would like the answer for Weblogic.
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Originally posted by Henry Cho:
(I'm calling a third party library that is not thread-safe).


If the third party API is not threadsafe, how about synchronizing the calls to it?


Alco-Haul: We move spirits.
Demented Deliberations of a Dilettante
Henry Cho
Greenhorn

Joined: Nov 10, 2003
Posts: 2
It seems to me that it would be more efficient to make the app server serialize the calls rather than create new threads which would just block on synchronized library wrapper code.
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
Well, what you are looking for is a way to force the server so that requests are handled strictly in sequence. That can never be better than synchronization blocks.

Think of it another way. If you do want to implement the sequential handling, the way would be to add the 'synchronized' keyword to the service() method. That way, the requests will be forced to be served one by one. Once you do that, implementing the SingleThreadModel is redundant. We have achieved your end goal and can do without the overhead of multiple servlet instantiations.

Now think of removing the synchronization from the complete method and only applying it to the API calls. Do you see how this is better?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Configuring a single servlet instance
 
Similar Threads
Servlet Multiple instances?
Can we make " singelton servlet" ?
Servlet Lifecycle
Servlet in different JVMs
****ServletContext and WebApplication confusion****