Call method in EJB on JBoss startup

Elijah picture Elijah · Jun 8, 2010 · Viewed 17.9k times · Source

I'm looking for an entry point in an EJB deployed on JBoss.

Servlets have the load-on-startup tag to use in its web.xml.

I'm searching for similar init() functionality for an EJB.

Answer

ewernli picture ewernli · Jun 8, 2010

That didn't exist for EJB until 3.1. With EJB 3.1 you can use a singleton bean to simulate that:

From Application Startup / Shutdown Callbacks:

   @Startup
   @Singleton
   public class FooBean {

       @PostConstruct 
       void atStartup() { ... }

       @PreDestroy
       void atShutdown() { ... }

   }

Otherwise, you will need to rely on the good old trick to use a ServletContextInitializer.

There are some application-specific extension, e.g. lifecycle listener for Glassfish. Maybe there's such a thing for JBoss.

But if I were you I would try to rely on standard features as much as possible. The problem with non-standard extension is that you never know exactly what can be done or not, e.g. can you start transaction or not, etc.