views:

65

answers:

2

Hi. 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.

A: 

That isn't supposed to happen. Did you investigate the JBoss startup logs to make sure that it isn't restarting the webapp all the time? Did you also verify if the same webapp isn't definied multiple times in context.xml, possibly each on a different context path?

When in vain, best what I can suggest is to run the debugger on contextInitialized() method or adding Thread.dumpStack(); to the method so that you can learn what exactly is calling it all the times.

BalusC
I'm pretty sure its not restarting the app, as I would see that in my console tab in intellij. And my context.xml doesn't have much in it. Its just:<Context cookies="true" crossContext="true"><Manager pathname="SESSIONS.ser" /><InstanceListener>org.jboss.web.tomcat.security.RunAsListener</InstanceListener></Context>
Brian
A: 

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.

Brian