Sunday, September 28, 2008

Service Life Cycle Management in Apache Axis2

Apache Axis2 provide a service life cycle management feature which can be used when we want to hook some logic when the service being deployed or when the system is shutting down. ServiceLifeCycle interface provides two methods startUp and shutDown which will be called during the service deployment time and during the system down.

So to use service life cycle management in Axis2, first we need a class implementing the ServiceLifeCycle interface. For the simplicity I will you the service implementation class.

package org.wso2.training;

import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.engine.ServiceLifeCycle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MyService implements ServiceLifeCycle {

private static final Log log = LogFactory.getLog(MyService.class);

public MyService() {
log.info("My service instance is created ...");
}

public int add(int a, int b) {
return a + b;
}

public void shutDown(ConfigurationContext configctx, AxisService service) {
log.info( service.getName() + " is shutting down ...");

}

public void startUp(ConfigurationContext configctx, AxisService service) {
log.info(service.getName() + " is starting up ...");
}

}
Then we need to define the ServiceLifeCycle management class in in the services.xml. You can do this using the class attribute of the service element.

<service name="MyService" class="org.wso2.training.MyService">
<parameter name="ServiceClass"
locked="false">org.wso2.training.MyService</parameter>
<operation name="add">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
</service>



Now if you deploy the service, you can see that startUp method being called.





and if we shutdown the server, we can see that shutDown method is being called.





Life cycle of the Axis Service and the service implementation class object is different. And Service Life Cycle management has no relation with the life cycle of the service implementation class object. Life time of the service implementation class object depends on the scope of the service. And in the above case, the service will be in default scope that is request scope and service implementation class object will be created for each request.

0 comments: