WSO2Con 2013 CFP Banner

What is ServiceLifeCycle and How to Use it?

Discuss this article on Stack Overflow
By Deepal Jayasingha
  • 5 Dec, 2006
  • Level: 
  • Reads: 13227

ServiceLifeCycle is a way to manage life cycle of a particular service you deploy in Axis2. For example, let's say you want to create a database connection at the server startup time and you need to use the connection throughout the lifetime of the services. And furthernore, assume that when the system goes down, you want to close the connection. Such situations may be a good example for the usage of service life cycle.

Deepal Jayasingha

WSO2 Inc.

Do not confuse service life cycle with session management. A session itself has its own life cycle but the service life cycle is different from that.

Session management is something to do with context like OperationContext , ServiceContext etc (You can learn more about session manegment here). However, service life cycle is something to do with AxisService. There will be different number of instances of the above contexts depending on the session scope, but there will be only one instance of AxisService for a particular a service.

To have service life cycle management support you need to implement org.apache.axis2.engine.ServiceLifeCycle interface. And need to specify the implementation class name in the services.xml.

The above interface has only two methods. One will be called at the service deployment time, while other will be called at the system shutdown time.

package org.apache.myservice;

public class MyServiceLifeCycle implements ServiceLifeCycle {

public void startUp(ConfigurationContext configctx, AxisService service) {
// Open up the DB connection
// Store the connection as a parameter in AxisService
}

public void shutDown(ConfigurationContext configctx, AxisService service){
// retrieve the parameter from AxisService and close the connection.
}
}

In the above case your services.xml is as follows;

<service name="MyService"
class="org.apache.myservice.MyServiceLifeCycle" >
<!--Rest of the elements goes here-->
</service>

Applies To:

Apache Axis2/Java 1.1 or later