Inside Scoop on J2EE : Tips and tricks on J2EE and Oracle Application Server by Debu Panda
Updated: 11/18/2004; 5:19:22 PM.

 

Subscribe to "Inside Scoop on J2EE" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 
 

Tuesday, August 31, 2004

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    comment []

© Copyright 2004 Debu Panda.

PS: These are my own thoughts and not of my employer ..



Click here to visit the Radio UserLand website.
 


August 2004
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Jul   Sep