Eager / auto loading of EJB / load EJB on startup (on JBoss)

Bozho picture Bozho · Apr 25, 2010 · Viewed 18k times · Source

EJBs seem to be loaded lazily - whenever accessed.

However, I want to initialize them eagerly - i.e. whenever the container starts-up. How is this achieved (in JBoss in particular)

This topic gives some hints, but isn't quite satisfactory.

Answer

Brett Kail picture Brett Kail · Apr 25, 2010

As of EJB 3.1, singleton beans can be notified of module start and stop:

@Singleton
@Startup
public class StartupBean {
    @PostConstruct
    private void postConstruct() { /* ... */ }

    @PreDestroy
    private void preDestroy() { /* ... */ }
}

Prior to EJB 3.1, there is no standard, EJB-only solution. I'd suggest adding a WAR to your EAR and using a servlet-context-listener.