Sun Java System Web Server 7.0 Update 7 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 Web Server. It includes the following sections:

Server Lifecycle Events

Web Server goes through different events in its lifecycle:

The LifecycleListener Interface

Web Server 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 Web Server 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. Web Server 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 

Remarks 

lifecycle-module.name

String  

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

Must be specified while registering this lifecycle module. 

lifecycle-module.class

String 

Fully qualified Java class name. 

Must implement the LifecycleListener interface.

lifecycle-module.enabled

Boolean 

true or false.

Default is true.

lifecycle-module.load-order

Integer 

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. 

lifecycle-module.is-failure-fatal

Boolean 

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.

lifecycle-module.class-path

String 

Optional  

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

lifecycle-module.description

Element  

Optional  

Describes the lifecycle module. 

property.name

String 

Optional 

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

property.value

String. 

Optional 

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

property.description

String  

Optional  

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.

<lifecycle-module>
<class-name>com.sun.ias.server.LifecycleListenerImpl</class-name>
<is-failure-fatal>false</is-failure-fatal>
<description>Sample lifecycle module</description>
<property>
<name>foo</name>
<value>fooval</value>
<property>
</lifecycle-module>

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:" + 
event.getData());

Properties props;

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

props = (Properties) event.getData();

//handle INIT_EVENT
return;
}

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

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