Contained WithinFind More DocumentationFeatured Support Resources | Descargar este libro en PDF (1319 KB)
Chapter 7 Developing Lifecycle ListenersThis 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 EventsSun Java System Web Server 7.0 goes through different events in its lifecycle:
The LifecycleListener InterfaceSun 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 ClassThe 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 ContextThe 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 ModuleServer 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
Considerations for Lifecycle ModulesWhen using keep the following points in mind of lifecycle module:
Sample Lifecycle ConfigurationThe 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;
}
}
}
|
||||||||||||||||||||||||||||||||||||||||||||