Why is contextInitialized() called multiple times?

TomahawkPhant picture TomahawkPhant · Aug 22, 2010 · Viewed 9.5k times · Source

I'm running a Stripes web app on Jboss 4.2.3.GA and am trying to call a method when I start JBoss. I created a ServletContextListener like so:

public class TimerContextListener implements ServletContextListener {

    @Inject
    private TimerManager timerManager;

    public void contextInitialized(ServletContextEvent servletcontextevent) {
        ((Injector) servletcontextevent.getServletContext().getAttribute(GuiceServletContextListener.KEY)).injectMembers(this);
        timerManager.stopAllTimers();
        timerManager.startTimer();
    }

    public void contextDestroyed(ServletContextEvent servletcontextevent) {

    }
}

and I added an entry in web.xml like so:

<listener>
        <listener-class>com.lawless.web.servletContextListeners.TimerContextListener</listener-class>
    </listener>

but contextInitialized() is getting called 3 times when I start my server. Any idea what the issue could be? Thanks.

Answer

TomahawkPhant picture TomahawkPhant · Aug 23, 2010

Ok I figured it out. It was being called 3 times because I had 3 virtual hosts defined in my jboss-web.xml. Not sure why it causes that behavior though. If anyone can explain the reason I would appreciate it.