This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Web Services and the fly likes Inicialize variables on axis app inicialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Web Services
Bookmark "Inicialize variables on axis app inicialization" Watch "Inicialize variables on axis app inicialization" New topic
Author

Inicialize variables on axis app inicialization

alain martin
Ranch Hand

Joined: Apr 11, 2005
Posts: 43
I'm developen a axis web services aplication.

I want to initializate bd conexions only when the users do the first call to one webservice of my aplitation after restart tomcat.

Where write I the code to do it?
Thankss


<a href="http://www.hhdirecto.net" target="_blank" rel="nofollow">http://www.hhdirecto.net</a>
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
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.

For a quick explanation and example see:
Web services programming tips and tricks: Using SOAP headers with JAX-RPC: The ServiceLifecycle interface or Web Service Conversation Using JAX RPC
alain martin
Ranch Hand

Joined: Apr 11, 2005
Posts: 43
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.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
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.
alain martin
Ranch Hand

Joined: Apr 11, 2005
Posts: 43
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?

Thanks

PD: sorry my english is bad, I'm spanish
[ August 04, 2006: Message edited by: alain martin ]
 
GeeCON Prague 2014
 
subject: Inicialize variables on axis app inicialization