Inside Scoop on J2EE : Tips and tricks on J2EE and Oracle Application Server by Debu Panda
Updated: 11/18/2004; 5:19:20 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, September 21, 2004

 

Several enterprise applications require Startup Class i.e. a class that needs to be executed when a container starts up. Most of the J2EE vendors support startup classes, but the problem is that these are vendor specific.

 

However most of the functionalities of startup classes can be achieved using different features supported by J2EE. I recommend using these methods instead of vendor specific extensions unless you really require these.

 

Some of the other options are: using Servlet Context Listener, auto-loading of Servlet using load-on-startup and other option is using auto-loading of application clients.

 

Servlet Context Listener

Application event listeners are classes that implement one or more of the Servlet event listener interfaces. They are instantiated and registered in the web container at the time of the deployment of the web application. The event listener gets invoked automatically when a certain lifecycle events occur e.g. when a Servlet context is created.

·         Write your ServletContextListener that implements your business logic

public class MyContextListener

   implements ServletContextListener {

public void contextInitialized(ServletContextEvent event) {

  ServletContext context = event.getServletContext();

  // Perform business logic

 

 }

 

  • Register the Servlet Context Listener in your web.xml as follows:

 

<listener>
          <listener-class>
              
MyContextListener
          </listener-class>
</listener>

 

The contextInitialized method will be executed whenever the ServletContext is initialized i.e. deployed/started.

 

 

Load-on-Startup for your Servlet

You can implement the logic for your startup class in the init() method of a Servlet and have it automatically loaded whenever the web module is started/loaded.

·         Write your Servlet that implements the business logic in the init() method:

public class QuartzServlet extends GenericServlet {

public void init(ServletConfig config) throws ServletException {

super.init(config);

System.out.println("Scheduling Job ..");

// Implement businesss logic 

}

public void service(ServletRequest arg0, ServletResponse arg1)

throws ServletException, IOException {

}

public String getServletInfo() {

return null;

}

}

·         Set load-on-startup for the Servlet in the web.xml as follows:

<servlet>

<servlet-name>QuartzInitializer</servlet-name>

<display-name>Quartz Initializer Servlet</display-name>

<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

 

  • Mark your web module to be automatically started when your container starts. For example, if you are using OC4J you have to do the following:

 

<web-app application="quartz" name="quartz-web" load-on-startup="true" root="/quartz" />

 

Your business logic in the init() method of your Servlet will be executed whenever your web module is deployed.

Automatically Executing an Application Client

  • Write and compile your startup class as an application client. This class must contain a main method that will be called by OC4J during startup.

 

public class MyStartup {

 

   

    public static void main(String[] args) {

      System.out.println("***** STARTUP begin ********************");

    

      // Implement business logic

     

 

      return;

    }

}

 

  • Create a META-INF/Manifest.mf file for the application-client, for example

Manifest-Version: 1.0
Main-Class:
MyStartup

  • Create a deployment descriptor for the application client as follows:

<application-client>
<display-name>
MyStartup
</display-name>
</application-client>

  • Create the application-client jar file for your startup class with the above Manifest and deployment descriptor.
  • Create an EAR file for the application-client module with the an OC4J-specific deployment descriptor orion-application.xml as follows:

<orion-application>
<client-module path="startup-class-client.jar" auto-start="true" user="anonymous" />
</orion-application>

  • Build your EAR and deploy your application with auto-start set to "true".

 Your  application client will be executed when application is deployed or restarted.

 

 

 


8:06:24 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.
 


September 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    
Aug   Oct