jQuery in Action, 3rd edition
The moose likes Web Services and the fly likes Tomcat and service instances Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "Tomcat and service instances" Watch "Tomcat and service instances" New topic

Tomcat and service instances

Marcel Bloemendaal

Joined: Aug 02, 2010
Posts: 1

I am somewhat new to webservices and tomcat, and I have a serious problem running a web service in Tomcat 5.0 jwsdp. (I know this is not the way to go, but due to circumstances, I have to use this platform) I'm using jwsdp to creat the service war. Whenever I start tomcat all goes well if I start with making just one call to my service. The main service class is instantiated and if I, after a second or 2 start making more calls to the service it keeps using this instance as expected. However, if after starting tomcat, several calls are quickly made to the service, tomcat seems to create several instances of it. I don't know why, and I really don't want this, since the service communicates with a multiuser server and only one instance can successfully log in to this server.
It gets weirder however, because to check if multiple instances are indeed created, I added a static variable numInstances to my main class, that I increase by one every time the class is instantiated, and then output to a log file. The log file then reads something like:

instance 1 created,
instance 1 created,
instance 1 created,
instance 1 created,
instance 4 created,
instance 4 created,
instance 4 created,
instance 4 created,
instance 3 created,
instance 3 created,
instance 3 created,
instance 3 created,
instance 2 created,
instance 2 created,
instance 2 created,
instance 2 created,

I am completely clueless as to how this can happen. It seems as if there are multiple instances of the entire service in which again multiple instances of the class??? Can anyone shed some light on this?

Thnx in advance..

Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
I don't know the details of why the phenomenon you describe occurs, but some of the explanation may be:
- The servlet programming model does not manage threads and thus a servlet, which is the base of web services that are not implemented using EJBs, may receive multiple concurrent requests.
- If several request are issued quickly to a Tomcat server, then several different threads are assigned to handle the requests.
I imagine you could limit the size of the threadpool from which these threads are taken, but I do not think that is a good idea - better alternative below.
- Even if you had an EJB implementing your web service (except with a singleton EJB), there would still be the possibility of concurrent connections to the multiuser server.
Only allowing one single request at a time to be processed by the web service will create a bottleneck, I suspect.

Depending on whether you need to return a result from the web service or not, I would consider two alternatives:

No result needed:
The web service places a request into a queue of some sort and then returns. A single thread operating independent of the web service pulls one task at a time from the queue and contacts the multiuser server.

Result needed:
Again, use the queue and a single thread contacting the multiuser service, but each task also has a correlation identifier to be able to determine which client is to receive what result.
Use either callback to client or client polling to retrieve the result.

Regarding the instance numbers: Multiple threads are probably executing in the block of code which prints the "instance x created" message and increments the counter. One thread executes the print statement, then is suspended and another thread executes the print statement. The first thread were never able to increment the counter prior to being suspended, so you see the same number multiple times.
Best wishes!

My free books and tutorials: http://www.slideshare.net/krizsan
I agree. Here's the link: http://aspose.com/file-tools
subject: Tomcat and service instances
It's not a secret anymore!