4 Replies Latest reply on Mar 4, 2009 8:12 AM by robert.geisler

    ClassCastException on Stateless Session Bean

    robert.geisler

      hello everybody,

      currently we are migrating our EJB 2.1 (about 850 EJBs) to EJB 3.0. till now there have not been too many problems, but today i meet a special challeng when i migrated a Stateful Session Bean.

      the SFSB, lets call it MyStatefulSessionBean implements a Remote Business Interface (MyStatefulSession) and gets deployed in a EAR (MyEAR.ear). from a standalone client i can lookup, cast and use the SFSB successfully.
      otherwise we deploy a WAR, too. from there i can lookup the SFSB, but cannot use it, because casting results in ClassCastException:

      Context context = ContextFactory.getInstance().getContext();
      MyStatefulSession session = (MyStatefulSession) context.lookup("MyEAR/MyStatefulSession/remote");

      $Proxy123 cannot be cast to xzy.MyStatefulSession


      the MyStatefulSession.class is packaged in server/MyServer/lib/MyLibrary.jar and in our EAR, too.
      i tried to remove all duplicate interfaces and classes from the EAR, but than i get java.lang.VerifyError -that i cannot understand- in arbitrary implementation classes (even in EJB 2.1 SLSB).
      2009-03-04 11:48:41,837 WARN [org.jboss.ejb.EJBDeployer] Verify failed; continuing
      java.lang.VerifyError: (class: a/b/c/ArbitraryBean, method: getSomething signature: (Ljava/lang/String;ILx/y/z/SomeDTO;)Ljava/util/ArrayList;) Incompatible argument to function
       at java.lang.Class.getDeclaredMethods0(Native Method)
       at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
       at java.lang.Class.privateGetPublicMethods(Unknown Source)
       at java.lang.Class.getMethods(Unknown Source)
       at org.jboss.verifier.strategy.AbstractVerifier.hasEJBCreateMethod(AbstractVerifier.java:687)
       at org.jboss.verifier.strategy.EJBVerifier21.verifySessionBean(EJBVerifier21.java:858)


      my idea is that all classes and interfaces that will be used from EAR and WAR, should go to server/MyServer/lib/, implementation classes should remain in EAR, servlets should go into WAR.
      is that right? if so, we have to refactor our ANT builds, i guess ; )

      anyway... i wonder why our packaging strategy do not show any conflicts for Stateless Session Beans. maybe someone can help me with this...?

      thanks in advance
      robert

        • 1. Re: ClassCastException on Stateless Session Bean
          jaikiran

           

          the MyStatefulSession.class is packaged in server/MyServer/lib/MyLibrary.jar and in our EAR, too.


          Ideally, nothing from your application should go in the server/< serverName>/lib folder.

          The bean interfaces can be packaged in a jar (MyLibrary.jar) and the jar can be placed in the EAR/lib folder. That's it. Now this jar (in the EAR/lib folder) will be available to all the components within the EAR. So your EJBs and the servlets in the WAR (inside this EAR) can use these interfaces.


          • 2. Re: ClassCastException on Stateless Session Bean
            robert.geisler

            thank you very, very much for your quick response, jaikiran.

            the EAR/lib folder is new for Java EE 1.5, i think. thats why we are not using it so far. but that could be a good solution. thanks.
            another question: if our WAR is not packaged into our EAR, then EAR/lib will not work for us, right? where are we supposed to put our library in this case? or should our WAR always be packaged into our EAR?

            regards
            robert

            • 3. Re: ClassCastException on Stateless Session Bean
              jaikiran

               

              "robert.geisler" wrote:

              another question: if our WAR is not packaged into our EAR, then EAR/lib will not work for us, right? where are we supposed to put our library in this case? or should our WAR always be packaged into our EAR?

              regards
              robert


              In such scenarios, you can package the bean interfaces in a jar and place that jar in the WAR/WEB-INF/lib folder. Additionally, the EAR containing the beans to will need to have that jar in it's EAR/lib folder so that the beans can be deployed.


              • 4. Re: ClassCastException on Stateless Session Bean
                robert.geisler

                ok, i'll try it this way and let you know what happened ; )
                thank you again