Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inicialize variables on axis app inicialization

 
alain martin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic