You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

The lifecycle callback handler allows invoking methods (callbacks) on the instance when instance's state changed. For example, it allows invoking a start method when the instance becomes valid and a stop method when the instance becomes invalid. Moreover, this handler allows the creation of immediate component. This page presents how to use this handler.

Instance Lifecycle

iPOJO instances have a very simple lifecycle. This lifecycle contains two states: INVALID and VALID. Once an instance is created, this instance can only be valid if all its plugged handlers are valid. For example, an instance requiring a service (and so using the dependency handler) cannot be valid if the required service is unavailable. Indeed, the dependency handler will be invalid.

An instance starts and stops in the invalid state.

Lifecycle callback

This handler supports two kinds of callback. The INVALID=>VALID callback are invoked when the instance becomes valid (at starting or when an event allows the instance to become valid). The VALID=>INVALID callback are invoked when the instance becomes invalid (at stopping or when an event invalids the instance).

An example

Let's take an example. The following class requires a FooService and has two lifecycle callback start and stop.

public class Foo {
              FooService fs;
              private void start() {
                       // Starting method
                       //...
                       fs.foo();
                       //...
                }
                protected void stop() {
                        // Stopping method
			if(fs!=null) { fs.foo(); }
                }
}

For this class, we define the following component type:

<component className="...Foo">
       <dependency field="fs"/>
       <callback initial="INVALID" final="VALID" method="start"/>
       <callback initial="VALID" final="INVALID" method="stop"/>
</component>

When an instance of this component type is created, the start method is called as soon as the Foo Service (service dependency) becomes available. If the Foo Service is no more available or when the instance is stopped, the stop method is called.

The invoked methods have no argument, but could be private, protected or public. Moreover, the INVALID=>VALID method can use service dependency (the instance becomes valid means that all required services are available); however, in the stop method it is possible that one of these dependency can be null. Indeed, the departure of a service can be the cause of the instance invalidation.

Manage threads

One usage of lifecycle callback is when the instance needs to create threads. Indeed, the thread can be created in the INVALID=>VALID callback, and stopped in the VALID=>INVALID method. The next class shows an example of a class creating a thread.

public class HelloRequesterImpl implements Runnable {

    final static int DELAY=10000;
    HelloService\[\] m_hello;&nbsp; // Service Dependency
    boolean end;

    public void run() {
      while (\!end) {
        try {
        synchronized (this) {
          for(int i = 0; i < m_hello.length; i++) {
            System.out.println(m_hello\[i\].sayHello("Clement"));
          }
        }
        Thread.sleep(DELAY);
        } catch (InterruptedException ie) {
             /\* will recheck quit \*/
        }
  }
}

public void starting() {
      Thread T = new Thread(this);
      end = false;
      T.start();
}

public void stopping() { end = true; }

For this component type, the metadata should be :

<component className="... HelloRequesterImpl">
		<dependency field="HelloService"/>
		<callback initial="INVALID" final="VALID" method="starting"/>
		<callback initial="VALID" final="INVALID" method="stopping"/>
</component>

Immediate component

An instance of an immediate component type is instantiated as soon it becomes valid. It means that, when the instance becomes valid, the constructor of the implementation class is called. This can replace the INVALID=>VALID callback.

However as there is no destructor in Java, the VALID=>INVALID callback is necessary if some actions are needed when stopping.

Advanced features

Static callbacks

You can call a static method as a callback. By default, the callback is not static. To call a static method, you need to add the isStatic attribute in the callback metadata :

<callback initial="INVALID" final="VALID" method="start" isStatic="true"/>
<callback initial="VALID" final="INVALID" method="stop" isStatic="true/>

Callback on several objects

If you instance has created several objects (called the implementation class constructor several times), the callback is called on each object in the creation order.  

  • No labels