JBoss.org Community Documentation

13.3. Service lifecycle

So far we have discussed how to define deployment/undeployment actions for beans together with callback methods for notification when other beans have been deployed/undeployed. These features are useful for adding behaviours to POJOs but what about adding behaviour to services?

As explained in the previous chapter services have a lifecycle consisting of 4 stages; create, start, stop and destroy. The reason for this is because we want the ability to start and stop a deployed service repeatably at runtime to make best use of valuable resources such as CPU and memory. At each point in this lifecycle the microcontainer will look for a method to call in order to perform appropriate actions. For example starting a service may require that an object is bound into JNDI. By default these methods are named after the lifecycle stage that they represent.


public class LifecycleBean {
    public void create() {
        ...
    }

    public void start() {
        ...
    }

    public void stop() {
        ...
    }

    public void destroy() {
        ...
    }
} 

If methods like these are defined in your bean then they will be called as the bean is deployed and undeployed. Specifically the create and start methods are called as the bean reaches the CREATE and START states during deployment. The stop and destroy methods are called when the bean passes through the START and CREATE states during undeployment. Only those methods that are defined will be called. For example if you omit the start method then the bean will move to the START state without anything happening.

If you want a different method to be called for any of the stages then you can provide an annotation or XML element to specify which one to use.


public class Example {
    @Create
    public void initialize() {
        ...
    }

    @Start
    public void go() {
        ...
    }

    @Stop
    public void halt() {
        ...
    }

    @Destroy
    public void remove() {
        ...
    }
}

<bean name="Name1" class="com.acme.Example">
    <create method="initialize"/>
    <start method="go"/>
    <stop method="halt"/>
    <destroy method="remove"/>
</bean> 

You can also specify parameters if necessary.


public class Example {
    @Start
    public void go(@StringValue("MyService") String serviceName,
                   @StringValue("5") Integer priority) {
        ...
    }
}

<bean name="Name1" class="com.acme.Example">
    <start method="go">
        <parameter>MyService</parameter>
        <parameter>5</parameter>
    </start>
</bean> 

Sometimes you may want lifecycle methods to be ignored. This can be done using the ignore attribute which if set to true prevents the microcontainer from calling the method when the corresponding lifecycle stage is reached.


public class Example {
    @Stop(ignored="true")
    public void halt() {
        ...
    }
}

<bean name="Name1" class="com.acme.Example">
    <start method="stop" ignored="true"/>
</bean>