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.
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.
Joined: Sep 11, 2002
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?