3

I have a function I want to execute straight after tomcat has started and loaded all its attributes successfully. I don't want to use ServletContextListener as this will require the function to start before tomcat starts. Can someone suggest how to go about this?

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Bitmap
  • 12,402
  • 16
  • 64
  • 91
  • 2
    How can the `ServletContextListener` be called *before* tomcat starts, that doesn't make any sense. It's called when the servlet context gets started. – skaffman Jan 20 '11 at 13:13
  • I think what I mean to say is the function will starts before startup finishes. ServletContextListener does does not wait till complete bootstrap of tomcat and there my function will executes on the cause of bootsrap. – Bitmap Jan 20 '11 at 13:20
  • @Bitmap: I disagree, that's not what happens. Where's your evidence for this? – skaffman Jan 20 '11 at 13:28
  • Because when I print message in contextInitialized, it gets printed before Hibernate Bindings gets initialized way before the tomcat message 'Server Started in %%% ms' gets displayed. – Bitmap Jan 20 '11 at 14:36
  • so you want hibernate started before some webApp that defines binidngs? what do you use as container? – bestsss Jan 20 '11 at 15:45
  • I want tomcat to finish startup and call a function to execute – Bitmap Jan 20 '11 at 19:43
  • How do you initialize Hibernate and spring? See my update btw – Bozho Jan 20 '11 at 22:46

3 Answers3

7

ServletContextListener.contextInitialized(..) is the method that is called after all servlets and filters have initialized for a given application.

  • if there are multiple ServletContextListeners, some of them are called before the others (logically)
  • if there are multiple applications (hence multiple contexts) some of them are started before others.

Update I will now assume your setup, although you didn't share it:

  • you start spring via a listener (and not with a servlet)
  • you configure hibernate within spring

In that case, you have two options:

  • define your ServletContextListener after the one for spring in web.xml, thus guaranteeing it will be invoked after it
  • use spring's lifecycle processor
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • You were right with: define your ServletContextListener after the one for spring in web.xml – Bitmap Jan 21 '11 at 10:44
0

You could create a startup servlet and then add that to the end of your web.xml:

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


public class MyStartupServlet extends HttpServlet {

    public void init(ServletConfig config) throws ServletException {
        try {
             //  Startup code here
        } catch (Exception e){
            // Log exception
        }
    }

    public java.lang.String getServletInfo() {
        return "StartupServlet";
    }
}
Zeki
  • 5,107
  • 1
  • 20
  • 27
0

I think JMX Tomcat supports can meet your requirement, even no ServletContextListener is deployed in container.

卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130