ClassCastException when narrowing remote home


EJB design: ClassCastException when narrowing remote home

  1. Hello all,

    I have two session beans, deployed in two separate ear-files. Bean 1 acts as a client to bean 2.

    Getting home of bean 2 in bean 1:

    Object o = ctx.lookup(jndiNameOfBean2);
    Bean2Home home = (Bean2Home) PortableRemoteObject.narrow(o, Bean2Home.class);

    I get a ClassCastException when trying to narrow o.

    If both beans are deployed in the same ear-file everything works fine.

    What is wrong here?


  2. The reason is probably because your Application Server has created different classloaders to load each deployed module. In this case, the same (home) classes are loaded by two different classloaders and hence are considered unequal.

    The solution as you found out is:
    (a) Use an EAR (or merge the JARs into one) in which case
        a single classloader will load the archive.
    (b) Or change the classloading policies of your EJB
        Container if possible to ensure that all deployed
        modules are loaded by the same classloader.

  3. So how do I design my system if an application consist of multiple modules that should be independently deployable?
    (Any module should also be able to use any remote SB in another deployed module.)