Skip Headers
Oracle® Containers for J2EE Developer's Guide
10g (10.1.3.1.0)

Part Number B28952-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

2 Developing Startup and Shutdown Classes

This chapter provides guidelines on developing startup and shutdown classes that are called after OC4J initializes or before OC4J terminates. Startup classes can start services and perform functions after OC4J initiates. Shutdown classes can terminate these services and perform functions before OC4J terminates.

When you compile these classes, the oc4j-api.jar file must be in a path specified in the Java CLASSPATH environment variable, such as ORACLE_HOME/j2ee/home/oc4j-api.jar.

OC4J deploys and executes the startup and shutdown classes based on configuration of these classes in the server.xml file.

This chapter includes these topics:

Developing Startup Classes

Startup classes are executed only once after OC4J initializes. They are not reexecuted every time the server.xml file is touched. A startup class implements the oracle.j2ee.server.OC4JStartup interface, which contains two methods:

In these methods, you can implement code for starting services, performing other initialization routines, ending services, and performing other termination routines.

Each method requires two arguments:

Both methods return a String value, which is currently ignored.


Note:

Oracle strongly recommends that you if give your startup class a constructor, you give it a public, no-argument constructor. Otherwise, java.lang.IllegalAccessException may be thrown when OC4J attempts to invoke a member method of this class.

After you create a startup class, you must configure it within the <startup-classes> element in the server.xml file. You can access this file through the Application Server Control Console by selecting Advanced Properties on the OC4J home page. Each OC4JStartup class is defined in a single <startup-class> element within the <startup-classes> element. Each <startup-class> element defines the following attributes:

In the <init-library> element in the server.xml file, you configure the directory where the startup class resides or the directory and JAR file where the class is archived. The path attribute can be fully qualified or relative to /j2ee/instance/config.

For example, the configuration for the TestStartup class is contained within a <startup-class> element in the server.xml file:

Add the following notation to the server.xml file to define the TestStartup class:

<startup-classes>
  <startup-class classname="test.oc4j.TestStartup" failure-is-fatal="true">
    <execution-order>0</execution-order>
    <init-param>
      <param-name>oracle.test.startup</param-name>
      <param-value>true</param-value>
     </init-param>
     <init-param>
      <param-name>startup.oracle.year</param-name>
      <param-value>2002</param-value>
     </init-param>
   </startup-class>
 </startup-classes>

The container provides the two initialization key-value pairs within the input Hashtable argument to the startup class.

The following example shows TestStartup, which implements the oracle.j2ee.server.OC4JStartup interface. The preDeploy method retrieves the key-value pairs from the hash table and prints them. The postDeploy method is a null method. The oc4j.jar file must be in the path that the Java CLASSPATH environment variable specifies when you compile TestStartup.

package text.oc4j;
import oracle.j2ee.server.OC4JStartup;

import javax.naming.*;
import java.util.*;

public class TestStartup implements OC4JStartup {

   //public, no-argument constructor
    public TestStartup() {
    }

    public String preDeploy(Hashtable args, Context context) throws Exception {
        // bind each argument using its name
        Enumeration keys = args.keys();
        while(keys.hasMoreElements()) {
            String key = (String)keys.nextElement();
            String value = (String)args.get(key);
            System.out.println("prop: " + key + " value: " + args.get(key));
            context.bind(key, value);
        }

        return "ok";
    }

    public String postDeploy(Hashtable args, Context context) throws Exception {
        return null;
    }
}

Assuming that the TestStartup class is archived in "../app1/startup.jar", you would modify the <init-library> element in the server.xml file as follows:

<init-library path="../app1/startup.jar" />

When OC4J starts, the preDeploy method of TestStartup is executed before any application is initialized. OC4J populates the JNDI context with the values from the hash table. If TestStartup throws an exception, then OC4J exits because the failure-is-fatal attribute was set to true.

Developing Shutdown Classes

Shutdown classes are executed before OC4J terminates. A shutdown class implements the oracle.j2ee.server.OC4JShutdown interface, which contains two methods, preUndeploy and postUndeploy, in which you can implement code for shutting down services or perform other termination routines.

Each method requires two arguments: a hash table that is populated from the configuration and a JNDI context to which you can bind to process values specified in key-value pairs.


Note:

Oracle strongly recommends that if you give your shutdown class a constructor, you give it a public, no-argument constructor. Otherwise, java.lang.IllegalAccessException may be thrown when OC4J attempts to invoke a member method of this class.

The implementation and configuration is identical to the shutdown classes as described in "Developing Startup Classes" with the exception that the configuration is defined within the <shutdown-classes> and <shutdown-class> elements and there is no failure-is-fatal attribute. Thus, the configuration for a TestShutdown class would be as follows:

<shutdown-classes>
  <shutdown-class classname="test.oc4j.TestShutdown">
    <execution-order>0</execution-order>
    <init-param>
      <param-name>oracle.test.shutdown</param-name>
      <param-value>true</param-value>
     </init-param>
     <init-param>
       <param-name>shutdown.oracle.year</param-name>
       <param-value>2002</param-value>
     </init-param>
   </shutdown-class>
 </shutdown-classes>

Assuming that the TestShutdown class is archived in "/j2ee/home/app1/shutdown.jar", add another <init-library> element in the server.xml file, as follows:

<init-library path="../app1/shutdown.jar" />