Auto-Starting EJBs

June 13th, 2009

Taking the Scenic Route

An issue that recently gave me something to gnaw on was that of auto-starting an EJB: I wanted to achieve the effect that an EJB within a Java enterprise application would become active (and in that special case, connect to another component on another machine) as soon as it was deployed (or the container was started). If possible, I wanted the solution to be container independent. After a lot of googling and trying, I found a solution that works, but it is probably one of the most awkward hacks to solve a problem that sounds as simple as this.

First and foremost: What I tried first was using the @PostConstruct annotation on a method that should be called after deployment – but that doesn’t work, because the EJB is not guaranteed to be instantiated directly after deployment. As a matter of fact, at least the JBoss I was using lazily instantiates the EJB the moment it is first invoked.

After a while, I came across this thread in Sun’s forum, in which a Sun developer states that better support for this use case is planned for a future EJB version, and currently the only way to achieve the desired effect is to rely on a ServletContextListener, which provides a contextInitialized() method that is called as soon as a servlet context is initialized. However, like the “servlet” implies, there needs to be a web application to use this listener – so I ended up adding a web application to my enterprise application with no content besides that context listener that would poke the EJB in order to get started. Now although this method is far from elegant, at least it works reliably and most possibly independent of the container used; at least I can verify its functionality on the JBoss 5 used in my test system.

Tags: , ,

One Response to “Auto-Starting EJBs”

  1. architecture

    [...] Jens-Fabian Goetzmann · Thoughts on (Web) Development, Software Architecture and Other Stuff · Home Information Hiding 2009 Review ». Auto-Starting EJBs. June 13th, 2009. Taking the Scenic Route. An issue that recently gave me … [...]…

Leave a Reply

© 2009 – 2010 Jens-Fabian Goetzmann — Impress — Powered by WordPressEntries (RSS) / Comments (RSS).