There are a few ways to do this, such as using ServletContextListener , startup servlets (see load-on-startup
in the specification ), resource adapters in full profile environments , EJB 3.1 @Startup , etc.
The decision of which to use varies from design to design and from the various most common approaches are these:
@WebListener
public class YourStartupApplication implements ServletContextListener {
@Inject
private Service service;
@Override
public void contextInitialized(final ServletContextEvent sce) {
service.doSomethingOnStartup();
}
@Override
public void contextDestroyed(final ServletContextEvent sce) {
service.doSomethingOnShutdown();
}
}
@Startup
@Singleton
public class YourStartupApplication {
@Inject
private Service service;
@PostConstruct
public void startup() {
service.doSomethingOnStartup();
}
@PreDestroy
public void shutdown() {
service.doSomethingOnShutdown();
}
}