Reputation: 2160
I want to execute some methods as soon as the .war file is deployed by Tomcat or JBoss, how can I do it?
I tried ServletContextListener
but it's not working.
Thanks.
Upvotes: 0
Views: 1681
Reputation: 2160
OK I resolved this, this works with JBoss:
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Startup;
import javax.ejb.Singleton;
@Singleton
@Startup
public class InitializerEjb {
@PostConstruct
public void init() {
SMTPServer smtp_server = SMTPServer.getInstance();
smtp_server.start();
}
}
And this works with Tomcat:
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class Initializer implements ServletContextListener {
@Override
public final void contextInitialized(final ServletContextEvent sce) {
SMTPServer smtp_server = SMTPServer.getInstance();
smtp_server.start();
}
}
Upvotes: 1
Reputation: 4218
Have you tried adding your methods in a Servlet and then run it on startup
In your Web.xml:
<servlet>
<servlet-name>YourServlet</servlet-name>
<servlet-class>com.your.domain.YourServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Upvotes: 3