Sun Java System Web Server 7.0 Developer's Guide to Java Web Applications

Chapter 7 Developing Lifecycle Listeners

This chapter provides a basic overview, and a description of various features of lifecycle listeners in Sun Java System Web Server 7.0. It includes the following sections:

Server Lifecycle Events

Sun Java System Web Server 7.0 goes through different events in its lifecycle:

The LifecycleListener Interface

Sun Java System Web Server 7.0 enables you to write classes and customize various phases of the server lifecycle. For instance, you may have a startup code that ensures a remote data source is available for the applications. Such classes are notified by server lifecycle events. The Sun Java System Web Server 7.0 defines a LifecycleListener interface that users can implement and register with the Server.

The syntax of this interface is as follows public void handleEvent(LifecycleEvent event): receives a lifecycle event.

In its event parameter, the programmatic interface for LifecycleListener provides the following features to the implementation classes:

The LifecycleEvent Class

The LifecycleEventclass is an interface from the point of view to the developer, even if programmatically it is a class. This interface is the means by which these events are represented. This class informs you of the kind of event that happened through the getEventType() method and the data associated with the event (through the getData() method).

The Server Lifecycle Event Context

The LifecycleEventContext interface provides an access to the server runtime environment including the JNDI naming context and logging service. The following methods are defined in this interface:

The following two methods are also used by this interface to keep backward compatibility with the 6.1 version of Web Server:

Deploying a Lifecycle Module

Server lifecycle listener classes are visible in the serve applications management area. You can add, delete, update, enable, and disable listener classes and set their parameters. Sun Java System Web Server 7.0 will not support dynamic deployment of startup and shutdown classes. Any changes to these classes or their configuration requires server restart.

Table 7–1 Elements of the lifecycle

Configurable element / attribute 

Data type and Units 

Range of values 



Any non-null/non-empty unique string in lifecycle modules.  

Must be specified while registering this lifecycle module. 



Fully qualified Java class name. 

Must implement the LifecycleListener interface.



true or false.

Default is true.



0-100 Reserved. 100-MAXINT.  

Order of loading the lifecycle event listeners in numerical order. Choose a load-order greater than or equal to 100 to avoid conflicts with internal lifecycle modules.


true or false

If you want the server to treat exceptions thrown from the listener classes as fatal and prevent continuation of normal startup, set this element to true.




Points to the user-specified classpath for the listener class. 




Describes the lifecycle module.



User-specified parameter name. Part of the property element.




User-specified parameter value. Part of the property element.




User-specified description. Part of the property element.

Considerations for Lifecycle Modules

When using keep the following points in mind of lifecycle module:

Sample Lifecycle Configuration

The following example shows a portion of the server.xml that defines a lifecycle listener.

<description>Sample lifecycle module</description>

The following example shows a sample LifecycleListener implementation

*PROPERITARY/CONFIDENTIAL. Use of this product is subject to license terms
*Copyright 2006-2007 by SunMicrosystems, Inc.,
*4150 Network Circle, Santa Clara, California, 95054, U.S.A
*All rights reserved.
package com.sun.ias.server;
import java.util.Properties; 
import java.util.logging.Level;  
import com.sun.appserv.server.LifecycleEventContext; 
import com.sun.appserv.server.ServerLifecycleException; 
import com.sun.appserv.server.LifecycleEvent; 
import com.sun.appserv.server.LifecycleListener;
  * LifecycleListenerImpl is a dummy implementation for the LifecycleListener 
  * interface.
  * This implementation stubs out various lifecycle interface methods.
public class LifecycleListenerImpl implements LifecycleListener {
/** receive a server lifecycle event
* @param event associated event
* @throws <code>ServerLifecycleException</code> for exception condition.
* /
public void handleEvent(LifecycleEvent event) throws ServerLifecycleException {
LifecycleEventContex ctx=event.getLifecycleEventContext();

ctx.log(level.INFO, "got event" + event.getEventType() + "event data:" + 

Properties props;

if (Lifecycleevent.INIT_EVENT == event.getEventType()) {
System.out.println("LifecycleListener: INIT_EVENT");

props = (Properties) event.getData();

//handle INIT_EVENT

if (LifecycleEvent.STARTUP_EVENT == event.getEventType()) {
System.out.println("LifecycleListener: START_EVENT");
if (LifecycleEvent.READY_EVENT == event.getEventType()) {
System.out.println("LifecycleListener: READY_EVENT");
//handle READY_EVENT

if (LifecycleEvent.SHUTDOWN_EVENT == event.getEventType()) {
System.out.println("LifecycleListener: SHUTDOWN_EVENT");
} if (LifecycleEven.TERMINATION_EVENT == event.getEventType()) {
System.out.println("LifecycleListener: TERMINATION_EVENT");