This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Have your service endpoint interface implement javax.xml.rpc.server.ServiceLifecycle and implement the init() method. While there is no guarantee, the init() function is usually only called once the first request against the service endpoint interface is received.
Sorry, but I didn't understand well. The class which implements ServiceLifecycle have to be the webservice class?
I have tried it, and init method runs after constructor, and anyway it is runned every call to webservice, not only with the first user.
Joined: Aug 19, 2005
Originally posted by alain martin: I have tried it, and init method runs after constructor, and anyway it is runned every call to webservice, not only with the first user.
This is just a reflection of the fact that Axis chooses to create a new Web service instance for each request. So in fact each Web Service instance only has one user, who is the first user of that instance.
Seems that in your case you need to manage something outside of the web service instances that Axis manages for you. In the init() method the container hands you an Object that you can cast to a javax.xml.rpc.server.ServletEndpointContext. From that you can use getServletContext() to get the interface to the web container javax.servlet.ServletContext. In the ServletContext you can use getAttribute() and setAttribute() to manage some application-wide name-value pairs. Using attributes a web service instance can find out if it is the first to instance to run and run some initialization code if necessary and store the resulting objects in the attributes while following instances can just grab these objects.
However you have to be careful as these application-wide attributes will be shared between all running web service instances. So you have to be sure to synchronize access to ServletContext and any attributes that are stored there (unless all instances are just reading from it; if somebody regularly performs read-write operation on the object then everybody has to synchronize access to the object):
Note that using lots of read-write operations on application-wide attributes will create a performance bottleneck.
Alternately you could try using class static variables in your web service class instead of attributes � but you should still wait until the init() method to initialize the shared objects, you still have to synchronize access to the shared objects, and the shared objects can still become a performance bottleneck.
Joined: Apr 11, 2005
Thank you, I think I will do it with a static variable.
I want save in it a connection pool (dataSource) with: DBCP
I'm going to do syncronized access only at getting connection
But using connetion and closing conexion is not need synchronized? true?
PD: sorry my english is bad, I'm spanish [ August 04, 2006: Message edited by: alain martin ]