quarta-feira, 4 de setembro de 2013

NameNotFoundException from a ServletContextListener implementation

When I've started migrating some applications from JBoss AS 4.2 to EAP 6.1 I got a really tough behavior making me review all settings and implementations more than once for a while to figure out what was happening.

Basically, there was a ServletContextListener implementation which performs a look up to a Stateless Session Bean in its contextInitialized method. Nothing new, nothing different from what you've already seen...

public void contextInitialized(ServletContextEvent arg0) {

   try {

      Context ctx = new InitialContext();

      Object o = ctx.lookup("java:app/my-ejb/MyBean!com.blogspot.brmachado.service.MyBeanLocal");


   } catch(Exception e) {



I saw all application EJBs being deployed successfully in the log, as well as it's JNDI bindings but even so there were a javax.naming.NameNotFoundException in the contextInitialized method. In AS 4.x and 5.x the application was deployed fine, but why NameNotFoundException was being thrown in EAP 6?

Next step in the troubleshooting process was to verify in the console if the JNDI bind was really there, and it was. Then I create a simple jsp with exaclty the same code and it was able to lookup successfully.

JBoss AS 7 and JBoss EAP 6, differently from the older versions, start all services in parallel and for some reason the Stateless Session Bean hasn't have it's JNDI bind in time. Note that the application.xml was ok, with the ejb jar coming first, even so it seemed that the order wasn't being making a difference:
Then I saw a "light in the end of the tunnel" with the "initialize-in-order" attribute added to application.xml, it forces JBoss to keep the application.xml ordering.


By setting initialize-in-order attribute, ejb.jar was deployed before the .war and the ServletContextListener implementation with the Stateles Session Bean worked just fine.

More about this in the EAP 6 Migration Guide.

Hope it helps you guys to save some time.