Question

I was testing an EJB 2.x application. I created 2 module separately:

EJB module: contains a simple stateless session bean

Web module: contains a single servlet page to lookup EJB module. I was using Jboss 4.2.3.

First, I deployed the EJB module and the deployment went well. Second, I deployed the web module and the deployment went well.

Then I used the following code to look up the EJB module:

Context c = new InitialContext();
Object o = c.lookup("HelloJNDI"); // Line 1
HelloLocalHome rv = (HelloLocalHome) o; // Line 2
HelloLocal local =  rv.create(); 

The lookup went well (Line 1), but Line 2 produced a class cast exception.

Then I test the above code in 2 scenarios:

  1. I packaged the EJB and the Web module into a single EAR module. Then, deployed this EAR module in JBoss 4.2.3, and the lookup code above worked like a charm.

  2. I tried to use JBoss 5, and even deployed the EJB module and the Web module separately, the lookup code above worked great.

So, why when I deployed the 2 modules separately in JBoss 4, things did not work out? I use local JNDI lookup only because the 2 modules were deployed in the same container. Was I missing something or this is a flaw in JBoss 4?

Was it helpful?

Solution

Try using the following code instead of your cast:

HelloLocalHome rv = (HelloLocalHome)javax.rmi.PortableRemoteObject.narrow(o, HelloLocalHome.class);

If that does now work, then you have the infamous classloading problem. (One of the reasons JBoss 5's default server uses a different classloading mechanism). Easiest is to indeed place them in a single EAR (so the Home and remote classes are loaded once). The Home interface class in your web application is loaded by a different classloader than the one being returned by JNDI.

You can also remove the interfaces from your WAR file's classes or lib dirrectory

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top