13 Programming Application Life Cycle Events
This chapter includes the following sections:
-
Examples of Configuring Life Cycle Events with and without the URI Parameter
-
Understanding Application Life Cycle Event Behavior During Re-deployment
-
Programming Application Version Life Cycle Events
Note:
Application-scoped startup and shutdown classes have been deprecated as of release 9.0 of WebLogic Server. The information in this chapter about startup and shutdown classes is provided only for backwards compatibility. Instead, you should use life cycle listener events in your applications.
- Understanding Application Life Cycle Events
Application life cycle listener events provide handles on which developers can control behavior during deployment, undeployment, and redeployment. Learn how you can use the application life cycle listener events. - Registering Events in weblogic-application.xml
You must register the application life cycle listener events in theweblogic-application.xml
deployment descriptor in order to use them. - Programming Basic Life Cycle Listener Functionality
You can create a listener by extending the abstract class (provided with WebLogic Server)weblogic.application.ApplicationLifecycleListener
. The container then searches for your listener. - Examples of Configuring Life Cycle Events with and without the URI Parameter
You can configure application life cycle events with or without using the URI parameter in theweblogic-application.xml
deployment descriptor file. - Understanding Application Life Cycle Event Behavior During Redeployment
Application life cycle events are only triggered if a full redeployment of the application occurs. During a full redeployment of the application—provided the application life cycle events have been registered—the application life cycle first commences the shutdown sequence, next re-initializes its classes, and then performs the startup sequence. - Programming Application Version Life Cycle Events
Learn how to create applications that respond to WebLogic Server application version life cycle events.
Understanding Application Life Cycle Events
Application life cycle listener events provide handles on which developers can control behavior during deployment, undeployment, and redeployment. Learn how you can use the application life cycle listener events.
Four application life cycle events are provided with WebLogic Server, which can be used to extend listener, shutdown, and startup classes. These include:
-
Listeners—attachable to any event. Possible methods for Listeners are:
-
public void preStart(ApplicationLifecycleEvent evt) {}
The preStart event is the beginning of the prepare phase, or the start of the application deployment process.
-
public void postStart(ApplicationLifecycleEvent evt) {}
The postStart event is the end of the activate phase, or the end of the application deployment process. The application is deployed.
-
public void preStop(ApplicationLifecycleEvent evt) {}
The preStop event is the beginning of the deactivate phase, or the start of the application removal or undeployment process.
-
public void postStop(ApplicationLifecycleEvent evt) {}
The postStop event is the end of the remove phase, or the end of the application removal or undeployment process.
-
-
Shutdown classes only get postStop events.
Note:
Application-scoped shutdown classes have been deprecated as of release 9.0 of WebLogic Server. Use life cycle listeners instead.
-
Startup classes only get preStart events.
Note:
Application-scoped shutdown classes have been deprecated as of release 9.0 of WebLogic Server. Use life cycle listeners instead.
For Startup and Shutdown classes, you only implement a
main{}
method. If you implement any of the methods provided for Listeners, they are ignored.No
remove{}
method is provided in theApplicationLifecycleListener
, because the events are only fired at startup time during deployment (prestart and poststart) and shutdown during undeployment (prestop and poststop).
Parent topic: Programming Application Life Cycle Events
Registering Events in weblogic-application.xml
You must register the application life cycle listener events in the weblogic-application.xml
deployment descriptor in order to use them.
See Enterprise Application Deployment Descriptor Elements. Define the following elements:
-
listener
—Used to register user defined application life cycle listeners. These are classes that extend the abstract base classweblogic.application.ApplicationLifecycleListener
. -
shutdown
—Used to register user-defined shutdown classes. -
startup
—Used to register user-defined startup classes.
Parent topic: Programming Application Life Cycle Events
Programming Basic Life Cycle Listener Functionality
You can create a listener by extending the abstract class (provided with WebLogic Server) weblogic.application.ApplicationLifecycleListener
. The container then searches for your listener.
You override the following methods provided in the WebLogic Server ApplicationLifecycleListener
abstract class to extend your application and add any required functionality:
-
preStart{}
-
postStart{}
-
preStop{}
-
postStop{}
Example 13-1 illustrates how you override the ApplicationLifecycleListener. In this example, the public class MyListener extends ApplicationLifecycleListener.
Example 13-1 MyListener
import weblogic.application.ApplicationLifecycleListener; import weblogic.application.ApplicationLifecycleEvent; public class MyListener extends ApplicationLifecycleListener { public void preStart(ApplicationLifecycleEvent evt) { System.out.println ("MyListener(preStart) -- we should always see you.."); } // preStart public void postStart(ApplicationLifecycleEvent evt) { System.out.println ("MyListener(postStart) -- we should always see you.."); } // postStart public void preStop(ApplicationLifecycleEvent evt) { System.out.println ("MyListener(preStop) -- we should always see you.."); } // preStop public void postStop(ApplicationLifecycleEvent evt) { System.out.println ("MyListener(postStop) -- we should always see you.."); } // postStop public static void main(String[] args) { System.out.println ("MyListener(main): in main .. we should never see you.."); } // main }
Example 13-2 illustrates how you implement the shutdown class. The shutdown class is attachable to preStop and postStop events. In this example, the public class MyShutdown
does not extend ApplicationLifecycleListener
because a shutdown class declared in the weblogic-application.xml
deployment descriptor does not need to depend on any WebLogic Server-specific interfaces.
Example 13-2 MyShutdown
import weblogic.application.ApplicationLifecycleListener; import weblogic.application.ApplicationLifecycleEvent; public class MyShutdown { public static void main(String[] args) { System.out.println ("MyShutdown(main): in main .. should be for post-stop"); } // main }
Example 13-3 illustrates how you implement the startup class. The startup class is attachable to preStart and postStart events. In this example, the public class MyStartup
does not extend ApplicationLifecycleListener
because a startup class declared in the weblogic-application.xml
deployment descriptor does not need to depend on any WebLogic Server-specific interfaces.
Example 13-3 MyStartup
import weblogic.application.ApplicationLifecycleListener; import weblogic.application.ApplicationLifecycleEvent; public class MyStartup { public static void main(String[] args) { System.out.println ("MyStartup(main): in main .. should be for pre-start"); } // main }
Parent topic: Programming Application Life Cycle Events
Configuring a Role-Based Application Life Cycle Listener
You can configure an application life cycle event with role-based capability where a user identity can be specified to startup and shutdown events using the run-as-principal-name
element. However, if the run-as-principal-name
identity defined for the application life cycle listener is an administrator, the application deployer must have administrator privileges; otherwise, deployment will fail.
The identity specified here should be a valid user name in the system. If run-as-principal-name
is not specified, the deployment initiator user identity will be used as the run-as
identity for the execution of the application life cycle listener.
Parent topic: Programming Basic Life Cycle Listener Functionality
Examples of Configuring Life Cycle Events with and without the URI Parameter
You can configure application life cycle events with or without using the URI parameter in the weblogic-application.xml
deployment descriptor file.
The following examples illustrate how you configure application life cycle events in the weblogic-application.xml
deployment descriptor file. The URI parameter is not required. You can place classes anywhere in the application $CLASSPATH
. However, you must ensure that the class locations are defined in the $CLASSPATH
. You can place listeners in APP-INF/classes
or APP-INF/lib
, if these directories are present in the EAR. In this case, they are automatically included in the $CLASSPATH
.
The following example illustrates how you configure application life cycle events using the URI parameter. In this case, the archive foo.jar
contains the classes and exists at the top level of the EAR file. For example: myEar/foo.jar
.
Example 13-4 Configuring Application Life Cycle Events Using the URI Parameter
<listener> <listener-class>MyListener</listener-class> <listener-uri>foo.jar</listener-uri> </listener> <startup> <startup-class>MyStartup</startup-class> <startup-uri>foo.jar</startup-uri> </startup> <shutdown> <shutdown-class>MyShutdown</shutdown-class> <shutdown-uri>foo.jar</shutdown-uri> </shutdown>
The following example illustrates how you configure application life cycle events without using the URI parameter.
Example 13-5 Configuring Application Life Cycle Events without Using the URI Parameter
<listener> <listener-class>MyListener</listener-class> </listener> <startup> <startup-class>MyStartup</startup-class> </startup> <shutdown> <shutdown-class>MyShutdown</shutdown-class> </shutdown>
Parent topic: Programming Application Life Cycle Events
Understanding Application Life Cycle Event Behavior During Redeployment
Application life cycle events are only triggered if a full redeployment of the application occurs. During a full redeployment of the application—provided the application life cycle events have been registered—the application life cycle first commences the shutdown sequence, next re-initializes its classes, and then performs the startup sequence.
For example, if your listener is registered for the full application life cycle set of events (preStart, postStart, preStop, postStop), during a full re-deployment, you see the following sequence of events:
-
preStop{}
-
postStop{}
-
Initialization takes place. (Unless you have set debug flags, you do not see the initialization.)
-
preStart{}
-
postStart{}
Parent topic: Programming Application Life Cycle Events
Programming Application Version Life Cycle Events
Learn how to create applications that respond to WebLogic Server application version life cycle events.
- Understanding Application Version Life Cycle Event Behavior
- Types of Application Version Life Cycle Events
- Example of Production Deployment Sequence When Using Application Version Life Cycle Events
Parent topic: Programming Application Life Cycle Events
Understanding Application Version Life Cycle Event Behavior
WebLogic Server provides application version life cycle event notifications by allowing you to extend the ApplicationVersionLifecycleListener
class and specify a life cycle listener in weblogic-application.xml
. See Enterprise Application Deployment Descriptor Elements and Examples of Configuring Life Cycle Events with and without the URI Parameter.
Application version life cycle events are invoked:
-
For both static and dynamic deployments.
-
Using either anonymous ID or using user identity.
-
Only if the current application is versioned; otherwise, version life cycle events are ignored.
-
For all application versions, including the version that registers the listener. Use the
ApplicationVersionLifecycleEvent.isOwnVersion
method to determine if an event belongs to a particular version. See theApplicationVersionLifecycleEvent
class for more information on types of version life cycle events.
Parent topic: Programming Application Version Life Cycle Events
Types of Application Version Life Cycle Events
Four application version life cycle events are provided with WebLogic Server:
-
public void
preDeploy(ApplicationVersionLifecycleEvent evt)
-
The
preDeloy
event is invoked when an application version deploy or redeploy operation is initiated.
-
-
public void
postDeploy(ApplicationVersionLifecycleEvent evt)
-
The
postDeloy
event is invoked when an application version is deployed or redeployed successfully.
-
-
public void
preUndeploy(ApplicationVersionLifecycleEvent evt)
-
The
preUndeloy
event is invoked when an application version undeploy operation is initiated.
-
-
public void
postDelete(ApplicationVersionLifecycleEvent evt)
-
The
postDelete
event is invoked when an application version is deleted.Note:
A
postDelete
event is only fired after the entire application version is completely removed. It does not include a partial undeploy, such as undeploying a module or from a subset of targets.
-
Parent topic: Programming Application Version Life Cycle Events
Example of Production Deployment Sequence When Using Application Version Life Cycle Events
The following table provides an example of a deployment (V1), production redeployment (V2), and an undeploy (V2).
Table 13-1 Sequence of Deployment Actions and Application Version Life Cycle Events
Deployment action | Time | Version V1 | Version V2 |
---|---|---|---|
Deployment of Version V1 |
T0 |
|
|
Deployment of Version V1 |
T1 |
Deployment starts. |
|
Deployment of Version V1 |
T2 |
Application life cycle listeners for V1 are registered. |
|
Deployment of Version V1 |
T3 |
V1 is active version, Deployment is complete. |
|
Deployment of Version V1 |
T4 |
|
|
Deployment of Version V1 |
T5 |
Application Listeners gets |
|
Production Redeployment of Version V2 |
T6 |
|
|
Production Redeployment of Version V2 |
T7 |
Application version listener receives |
|
Production Redeployment of Version V2 |
T8 |
Deployment starts. |
|
Production Redeployment of Version V2 |
T9 |
Application life cycle listeners for V2 are registered. |
|
Production Redeployment of Version V2 |
T10 |
If deploy(V2) succeeds, V1 ceases to be active version. |
If deploy(V2) succeeds, V2 replaces V1 as active version. Deployment is complete. |
Production Redeployment of Version V2 |
T11 |
Note: This event occurs even if the deployment fails. |
|
Production Redeployment of Version V2 |
T12 |
Application version listener gets |
|
Production Redeployment of Version V2 |
T13 |
Application listeners gets |
|
Production Redeployment of Version V2 |
T14 |
If deploy(V2) succeeds, V1 begins retirement. |
|
Production Redeployment of Version V2 |
T15 |
Application listeners for V1 are unregistered. |
|
Production Redeployment of Version V2 |
T16 |
V1 is retired. |
|
Undeployment of V2 |
T17 |
|
|
Undeployment of V2 |
T18 |
Application listeners gets |
|
Undeployment of V2 |
T19 |
Undeployment begins. |
|
Undeployment of V2 |
T20 |
V2 is no longer active version. |
|
Undeployment of V2 |
T21 |
Application version listeners for V2 are unregistered. |
|
Undeployment of V2 |
T22 |
Undeployment is complete. |
|
Undeployment of V2 |
T23 |
If the entire application is undeployed, Note: This event occurs even if the undeployment fails. |
Parent topic: Programming Application Version Life Cycle Events