There are requirements for some projects to automatically instantiate and execute a method on a Java class referred as Startup class during the initialization of the J2EE application server. In this startup class you can initialize environment constants, services, and make calls to other classes.
Starting with OC4J 9.0.4 provides a facility whereby a Java class (or classes) can be executed when the OC4J server is started or shutdown. The class can provide actions that can be called before and/or after the deployed applications are initialized.
For a Startup class you can specify initialization parameters via its configuration file, which can then be bound into the JNDI tree. The bound objects can be accessed from any of the applications deployed in the OC4J instance.
Steps in Building and Deploying a Startup class
- Write and compile your startup class. An OC4J Startup class needs to implement com.evermind.server.OC4JStartup interface and needs to implement either preDeploy or postDeploy method. For a shutdown class you have to implement the com.evermind.server.OC4JShutdown interface.
The following snippet of an example startup class shows the implementation of the required interface and the preDeploy method:
public class Oracle9iASStartupSample implements OC4JStartup { public String preDeploy(Hashtable args, Context context) throws Exception { System.out.println(getClass().getName()+ " preDeploy method called"); }
- Compile the startup class and package the class in a JAR file. To compile startup and shutdown classes, you must include the oc4j.jar library in your classpath.
- Modify the server.xml for your OC4J instance file and add the the following tag to include the jar file in your library path as shown below. Note that this assume you have copied hte startup-class.jar file into the j2ee/home/applications directory of the OC4J instance you are using.
<init-library path="../applications/startup-class.jar" />
- Specify your startup class name and any initialization parameters. In this example, these parameters will be bound to the JNDI tree and will then be accessible from any application running on the OC4J instance
<startup-classes> <startup-class classname="startup.Oracle9iASStartupSample" failure-is-fatal="true"> <execution-order>0</execution-order> <init-param> <param-name>sampleName</param-name> <param-value>HowTo-Startup</param-value> </init-param> <init-param> <param-name>IsThisANewSample</param-name> <param-value>True</param-value> </init-param> </startup-class> </startup-classes>
You can download the full code example from OTN
11:15:26 AM
|