Doing work at server startup and shutdown
- I. Introduction
- II. Using startup and shutdown classes
- Startup classes
- Writing startup classes
- Registering startup classes
- Shutdown classes
I. Introduction
WebLogic provides a mechanism for performing tasks when the WebLogic
Server starts up or is gracefully shut down. Your server-side application may
perform any task from a Java class, which has access to all of WebLogic's
server-side services. An example use might be to set up an triggered
action that's performed at regular intervals throughout the life of the server.
II. Using startup and shutdown classes
Startup classes
To perform a task when the WebLogic Server is started, you write a startup
class and register it in the weblogic.properties file. Startup classes are automatically
loaded and executed when the WebLogic Server is started, or restarted. Startup
classes are only loaded and invoked after all other server initialization has
completed.
Writing startup classes
Follow these steps to write a startup class:
- Your startup class must implement the interface
weblogic.common.T3StartupDef. It must implement the startup() method, and the setServices() method. Additionally, your
class must have a public default constructor.
Here is a template of the startup class with comments where you add your
own code:
import java.util.Hashtable;
import weblogic.common.*;
public class MyStartup implements T3StartupDef {
public StartupTest() {}
private T3ServicesDef services;
public void setServices(T3ServicesDef services) {
this.services = services;
}
public String startup(String name, Hashtable args) throws Exception {
// Write your startup code here...
return "ok";
}
}
The startup() method takes two
arguments, defined here:
- String name
- A String holding the name of
the startup class as registered in the weblogic.properties file. You can incorporate this into your
startup class logic to create a reusable class that is uniquely identified by
its virtual name in the properties file.
- Hashtable args
- A Hashtable containing the
initialization arguments, registered in the weblogic.properties file. You can use the startup arguments to
allow the logic of your startup class to be configured at deployment time.
The startup() method must return
a String that is written to the
WebLogic log file.
The setServices() method is invoked
prior to the startup() method. Your
startup class can store a reference to the T3ServicesDef object factory, so that it had access to the
WebLogic services directly, without requireing a JNDI lookup.
- Set up your development environment, as described in Setting your development environment.
Now, compile the startup class and place it under the classpath directory
pointed to by the environment variable SERVER_CLASSES. This directory is included in the server
classpath. Here is an example compile line on NT:
$ javac -d %SERVER_CLASSES% MyStartup.java
- Register your startup class in the weblogic.properties file. See the
Administrators Guide section
Registering startup and shutdown classes for details.
- Start the WebLogic Server. Your startup class is
invoked after the server has completely initialized, but
immediately before the server begins listening on its
TCP/IP
listen port. The startup class is guaranteed to run before the WebLogic
Server starts servicing clients.
Shutdown classes
- weblogic.common.T3ShutdownDef
Shutdown classes work the same way as start up classes. Shutdown
operations are automatically loaded and executed when the WebLogic
Server is shutdown with the weblogic.Admin shutdown command.
You write a shutdown class by implementing the interface weblogic.common.T3ShutdownDef.
Shutdown functions might include such things as writing a message to
the log file or cleaning up resources.
Details on registering a shutdown class can be found in the Administrators
Guide, under the section
Registering startup and shutdown classes.
