Friday, November 28, 2008

Axis2 services : LifeCycle Vs ServiceLifeCycle

Both the LifeCycle and the ServiceLifeCycle interfaces give the service developer additional control over service's life cycle.

LifeCycle interface introduces two methods, init(ServiceContext serviceContext) and destroy(ServiceContext serviceContext).

init() will be fired at the start of the session and destroy will be fired at the time the SessionContext object being garbage collected.

In the case of 'application' scope, if the service being in the repository before axis2 boots up - then init() method will be called during axis2 engine startup - if the service is deployed after axis2 engine started up, then init() method will be called at the time of the first request to the service.

To get access to these two methods you simply need to add them to your service implementation class.

Even you do not implement the LifeCycle interface - but have the above two methods in your service implementation - still your methods will be called by the axis2 engine. But it's highly recommended that - if you need access to these methods, you better keep the LifeCycle interface implemented.

In the case of ServiceLifeCycle interface - it introduces two methods startUp(ConfigurationContext configctx, AxisService service) and shutDown(ConfigurationContext configctx, AxisService service).

startUp() will be fired during the deployment time of the service and shutDown() will be fired during the system shut down time, irrespective of the service scope.

To get access to these two methods you need to do two things.

1. Keep ServiceLifeCycle interface implemented.
2. Add 'class' attribute to the <service> element in the services.xml and point it to the ServiceLifeCycle implementation.

Let me revisit one point that I mentioned, before.

If the service is deployed after axis2 engine started up, then LifeCyle interface's init() method will be called at the time of the first request to the service - but even in this case startUp() of ServiceLifeCycle will be called as soon as service got deployed.

Another difference is, you can have ServiceLifeCycle implementation apart from your service implementation - but in the case of LifeCycle , both should be the same.

<service name="SimpleService" class="org.apache.ws.axis2.SimpleService" scope="application">
<operation name="echo">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
<parameter name="ServiceClass" locked="false">org.apache.ws.axis2.SimpleService</parameter>
</service>

package org.apache.ws.axis2;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ServiceContext;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.engine.ServiceLifeCycle;
import org.apache.axis2.service.Lifecycle;

/**
* The service implementation class
*/
public class SimpleService implements ServiceLifeCycle, Lifecycle {

/**
* This is called when a new instance of the implementing class has been created.
* This occurs in sync with session/ServiceContext creation. This method gives classes
* a chance to do any setup work (grab resources, establish connections, etc) before
* they are invoked by a service request.
*/
public void init(ServiceContext serviceContext) throws AxisFault {
System.out.println("Lifecycle:::init()");

}

/**
* This is called when Axis2 decides that it is finished with a particular instance
* of the back-end service class. It allows classes to clean up resources.
*/
public void destroy(ServiceContext serviceContext) {
System.out.println("Lifecycle:::destroy()");
}

/**
* This will be called during the deployment time of the service.
* Irrespective of the service scope this method will be called
*/
public void startUp(ConfigurationContext configctx, AxisService service) {
System.out.println("ServiceLifeCycle:::startUp()");
}

/**
* This will be called during the system shut down time. Irrespective
* of the service scope this method will be called
*/
public void shutDown(ConfigurationContext configctx, AxisService service) {
System.out.println("ServiceLifeCycle:::shutDown()");

}

/**
* The echo method which will be exposed as the echo operation of the web
* service
*/
public String echo(String value) {
return value;
}

}

16 comments:

Salvador Limonez said...

Good to know. Thanks.

Anonymous said...

Hi,

this does not work for me (Axis 1.4.1).

The start-up & shutdown are not called at all.

I have a POJO webservice with scope="application".

Also the init() method is only called when the webservice is called and *after* the excution of the webservice, seems that this is a bug ?

Any suggestions ?

Prabath said...

Hi;

In the case of 'application' scope, if the service being in the repository before axis2 boots up - then init() method will be called during axis2 engine startup - if the service is deployed after axis2engine started up, then init() method will be called at the time of the first request to the service.

For startup() make sure you add 'class' attribute to the <service> element in the services.xml and point it to the ServiceLifeCycle implementation.

Thanks.
-Prabath

Anonymous said...

Hi,

thanks for your advice. It is working fine now, but I get following error when I start Tomcat (although the starup method is called - so no problem):

I thought Axis2 uses by default Http12 protocol ?

Any suggestion ?

4-dec-2008 9:18:09 org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
4-dec-2008 9:18:09 org.apache.coyote.http11.Http11Protocol init
SEVERE: Error initializing endpoint
java.net.BindException: Address already in use: JVM_Bind.null.:8080
at org.apache.tomcat.util.net.JIoEndpoint.init(JIoEndpoint.java:502)
at org.apache.coyote.http11.Http11Protocol.init(Http11Protocol.java:176)
...
4-dec-2008 9:18:09 org.apache.catalina.startup.Catalina load
SEVERE: Catalina.start
LifecycleException: Protocol handler initialization failed: java.net.BindException: Address already in use: JVM_Bind:8080
at org.apache.catalina.connector.Connector.initialize(Connector.java:1060)
...

Prabath said...

SimpleAxis2Server by default is running on port 8080 - it seems you try to start Tomcat on the same port.

Thanks.
- Prabath

Anonymous said...

Hi,
thanks again for you advice. I'm not calling the bin\axis2server.bat before starting tomcat so I don"t this this is caused by SimpleAxis2Server instance.

Can you tell me how I can be sure that SimpleAxis2Server is not started or how I can find out what is causing this error ? Any suggestions ?

Thx.

hideki said...

Hi All,

thx you very much for the article, but I experience following problem:

I have a service in soapsession scope.

The underlying class implements the org.apache.axis2.engine.LifeCycle class.

When a session starts the init()method is called.
When a session end the destroy() method is called.
So far so good :-)

Now when the session expires (time out) the shutdown() is *not* called.

This is quire annoying as I need to to some 'clean-up'.

How can the session be notified it has been expired ?

Any ideas ?

Regards,

H.

crystal said...

Thank you so much!!polo shirt men'ssweate,Burberry Polo Shirts lacoste sweater, ralph lauren Columbia Jackets,ski clothing. Free Shipping, PayPal Payment. Enjoy your shopping experience on mensclothingus.com.You can find the father who desire fashionable, intellectual mens clothing simultaneously.
http://blog.livedoor.jp/lljj332
http://shoes-puma.jugem.jp
http://poloshirts--myfashion.blogspot.com
http://blades.blogsome.com
http://gillettefusion.edublogs.org

crystal said...

Awesome!!!Best wishes for you !!cheap polo shirts is the father of the summer should be prepared to most commonly used item, it has both style and shape of Ralph Lauren Polo, and vest with a random function polo ralph lauren, so that in the short-sleeved apply to both on many occasions, the pink and black color men's polo shirts brought into effect, lightweight cotton, linen texture to demonstrate masculine temperament and sense of fashion exhaustively.

venus said...

God bless you!I really agree with your opinions.Also,there are some new fashion things here,gillette razor blades.gillette mach3 razor bladesfor men.As for ladies,gillette venus razor blades must the best gift for you in summer,gillette fusion blades are all the best choice for you.

crystal said...

Perfect!!You are a outstanding person!Have you ever wore chaussures puma,Here are the most popular puma CAT,Puma shoes store gives some preview of puma speed cat,and casual but no sweat puma basket.
http://community.fox2now.com/venusjj
http://cheappolos.blog.drecom.jp
http://d.hatena.ne.jp/crystal666
http://www2.atword.jp/pumaspeed
http://www.seriousblogging.com/basketspuma

crystal said...

Do not mean bad.Thank you so much!I just want to show some fashion things to all of you.I like puma speed, puma femmes and other puma shoes. These puma sport items are at store recently and available for anyone.

crystal said...

Fantastic!God bless you!Meanwhile,you can visit my China Wholesale,we have the highest quality but the lowest price fashion products wholesale from China.Here are the most popular China Wholesale productsfor all of you.You can visit http://chinaclothes.net.Also the polo clothing is a great choice for you.
Puma sport
polo shirts
polo shirts
gillette razor blades
Queen--ugg boots
original polo clothing

gordo said...

it works for me only on "bottom up" approach. I'm trying to create a webservice from WSDL. I implemented the ServiceLifeCicle and added to services.xml but startup and shutdown not are called. Any suggestions?

gordo said...
This comment has been removed by the author.
gordo said...

I found the problem. I was editing the file in the wrong folder. The right is in WebContent / WEB-INF / services.