7 Replies Latest reply on Jun 30, 2005 12:14 PM by elmosca

    Beta1: wrong object class when getting session bean from con

    elmosca

      Hi all,

      JBoss: 4.0.3RC1 (graphical install) or 4.0.2 (manually installing ejb3.deployer, etc)
      EJB3: beta1
      JDK 5.0

      first of all, I have to say that the application I am developing work ok using EJB3 preview 5 and Jboss 4.0.1sp1.
      This application has several session beans (stateless and stateful). Each bean has a interface with the @Remote annotation. Deploy goes ok, without problems neither warnings...
      Now, when inside the application (a war) I try to access to a bean the wrong class is returned, while if I get the bean from a client (in a suite of tests that I have outside JBoss everything goes ok).

      Let's illustrate the situation:

      I have:

      @Statefull
      public class UserHandlerBean extends UserHandler implements Serializable {
      ...
      }
      
      @Remote
      public interface UserHandler {
      ...
      }


      Now, I access to the bean (from the webapp inside Jboss or a client test) like this:

      InitialContext ctx = new InitialContext();
      UserManager um = (UserManager) ctx.lookup(UserManager.class.getName());


      From the client wverything runs smoothly, but, from the webapp I get the exception:

      java.lang.ClassCastException: $Proxy103
      ...


      Something happens here, the class that the webapp obtains from the sever InitialContext is of class $Proxy103 and does not implement UserManager :-(

      Any hint will be appreciated,

      Regards,

      Bruno

        • 1. Re: Beta1: wrong object class when getting session bean from
          fushion

          Instead of

          UserManager um = (UserManager) ctx.lookup(UserManager.class.getName());


          Maybe you should try

          UserHandler uh = (UserHandler) ctx.lookup(UserHandler.class.getName());


          :-)


          • 2. Re: Beta1: wrong object class when getting session bean from
            fushion

            ... and why are you extending your UserHandler interface instead of implementing it?

            Maybe you should post some better (correct) samples so we can have a proper look at them.

            • 3. Re: Beta1: wrong object class when getting session bean from
              elmosca

              Ups, sorry :-( I wrote it by heart as I am not at the office, and I wanted to make the code more readable using classes with common names... and the effect has been the oposite :-(

              Of course, Fushion I get the instance of the been doing:

              UserHandler uh = (UserHandler) ctx.lookup(UserHandler.class.getName());


              and of course, UserHandlerBean implements UserHandler, Serializable...

              The good thing in all this is that it seems that I am the only one having this problem.... :-/

              Thanks and regards,

              Bruno

              • 4. Re: Beta1: wrong object class when getting session bean from
                elmosca

                Well, now I have it running in jboss4.0.1sp1 again (but with the EJB3.0 beta1). Should be ok, until I can upgrade again,

                Regards,

                Bruno

                • 5. Re: Beta1: wrong object class when getting session bean from
                  elmosca

                  I am having more problems with jboss4.0.1sp1 so I've decided to give a new try to jboss4.0.3RC1 to see if I find where I might have something wrong...
                  I've tried to see why the server is throwing a ClassCastException when doing from a webapp (and not from a standalone client outside Jboss):

                  my.company.UserHandler uh = (my.company.UserHandler) ctx.lookup(my.company.UserHandler.class.getName());


                  I've tried to see what king of object is returning ctx.lookup, and the results have been those:

                  Object class returned by ctx.lookup -> $Proxy127
                  (obj instanceof my.company.UserHandler) -> returns false

                  But, surprisingly, if I get the interfaces of this object using reflection I get two interfaces:

                  - my.company.UserHandler
                  - org.jboss.ejb3.JBossProxy

                  Weird, if one of the interfaces is UserHandler (as expected) why is the cast throwing an exception, how can the instanceof return false if one of the interfaces implemented by the Proxy is UserHandler?
                  Another question, how can the application from the server fail and the standalone test client work? Could it be a classpath issue?

                  The only difference I see is that in the server I have to copies of UserHandler (one inside the *.par file) and another inside a jar (the jar that my application has in the WEB-INF/lib to be able to use the session bean).

                  Any help will be appreciated,

                  Bruno


                  • 6. Re: Beta1: wrong object class when getting session bean from
                    kabirkhan

                    With the tomcat classloader settings that ship with 4.0.3 you will always load the copy from WEB-INF/lib from the web layer, and this uses a different classloader from the one used inside the ejb layer. Two classes loaded by different classloaders are not the same to the java runtime, and will cause runtime exceptions when you try to assign a reference to one to the other.

                    Try removing UserHandler from the jar in WEB-INF/lib, this will make sure that there is only one copy of the class ever loaded.

                    Take a look at
                    http://wiki.jboss.org/wiki/Wiki.jsp?page=ClassLoadingConfiguration
                    and
                    http://wiki.jboss.org/wiki/Wiki.jsp?page=JBossClassLoadingUseCases

                    (The classloading settings for the web layer changed after 4.0.1, which is why you are suddenly having these problems w/ 4.0.3)

                    • 7. Re: Beta1: wrong object class when getting session bean from
                      elmosca

                      Thanks Kabir!, that was exactly the cause...

                      Regards,

                      Bruno

                      p.s. now there is another happy person in the World ...