Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java web-application initialization and shutdown

I'm trying to implements initialization and shutdown of a webapp. That includes initialization and shutdown of:

  • Hibernate (v3.6);
  • C3P0 (v0.9.1.2);
  • EHCache (v2.3.0);
  • Quartz (1.8.4);
  • Other tasks specific to my webapp;

Using Tomcat 5.5.30 and Java 6. My idea is to avoid resource leaking, mostly because of the redeploy of the webapp in the development environment.

How should I implement this?

like image 501
ChRoNoN Avatar asked Dec 06 '10 11:12

ChRoNoN


2 Answers

Usually for Web initialization and shutdown, you will write a ServletContextListener.

The steps to do this are:

  1. Write a class that implements javax.Servlet.ServletContextListener
  2. Add a tag to web.xml deployment descriptor to register the class you've just created
  3. Deploy your application

When you deploy your application, contextInitialized method will be called. You can place all initialization you want here. On application shutdown contextDestroyed method will be called.

like image 70
Pablo Santa Cruz Avatar answered Sep 28 '22 00:09

Pablo Santa Cruz


Its also possible to use the HTTP Servlet instead but the listener is a better option.

You have to extend a class with HttpServlet and setting the following stuff to your web.xml:

<servlet>
    <servlet-name>StartupServlet</servlet-name>
    <servlet-class>your.package.servlets.StartupServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

The class can overwrite the init and the destroy method.

like image 25
ziodraw Avatar answered Sep 28 '22 00:09

ziodraw