2 Replies Latest reply on Jul 31, 2008 4:42 PM by sd dsf

    JBoss 5.0.0.CR1 - EJB Lookup Successful but ClassCastExcept

    sd dsf Newbie

      Trying out simple prototype

      ---- SETUP ------

      enterpriseproject.ear contains
      DynamicWebProject.ear
      EjbProject.jar

      DynamicWebProject.ear contains
      SecondServlet

      EjbProject.jar contains
      ThirdSessionBean (Bean Implementation)
      ThirdSession (business interface)

      ------- ThirdSessionBean Snippet -----

      package com.tsb.test;
      import javax.ejb.Remote;
      import javax.ejb.Stateless;

      @Stateless
      @Remote(ThirdSession.class)

      public class ThirdSessionBean implements ThirdSession{
      public String sayHello(String name) {
      return "Hello From Third Session Bean " + name + " ";
      }

      }

      ------ ThirdSession Snippet -------

      package com.tsb.test;

      /* This is the business interface */


      public interface ThirdSession
      {
      public String sayHello(String name);


      }

      --- SecondServlet Snippet -------


      final String jndiName = "enterpriseproject/ThirdSessionBean/remote";

      try {
      Context ic;
      ic = new InitialContext();
      System.out.println("onject returned is " + ic.lookup(jndiName));
      //SecondSessionTestRemote foo = (SecondSessionTestRemote) ic.lookup(jndiName);

      // ThirdSession foo = (ThirdSession) ic.lookup(jndiName);
      Object foo1 = ic.lookup(jndiName);

      org.jboss.ejb3.proxy.JBossProxy zoo = (org.jboss.ejb3.proxy.JBossProxy) foo1;

      System.out.println("This works");


      ThirdSession foo = (ThirdSession) foo1;

      String s = foo.sayHello("Hello Foo on JBoss!");
      System.out.println(foo + " echo returned " + s);


      } catch (NamingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }

      --------------------- end of code snippets -------

      JNDIView of the EJB is

      Global JNDI Namespace


      +- ClusteredConnectionFactory (class: org.jboss.jms.client.JBossConnectionFactory)
      +- XAConnectionFactory (class: org.jboss.jms.client.JBossConnectionFactory)
      +- UserTransactionSessionFactory (proxy: $Proxy83 implements interface org.jboss.tm.usertx.interfaces.UserTransactionSessionFactory)
      +- console (class: org.jnp.interfaces.NamingContext)
      | +- PluginManager (proxy: $Proxy81 implements interface org.jboss.console.manager.PluginManagerMBean)
      +- UUIDKeyGeneratorFactory (class: org.jboss.ejb.plugins.keygenerator.uuid.UUIDKeyGeneratorFactory)
      +- enterpriseproject (class: org.jnp.interfaces.NamingContext)
      | +- ThirdSessionBean (class: org.jnp.interfaces.NamingContext)
      | | +- remote (proxy: $Proxy112 implements interface com.tsb.test.ThirdSession,interface org.jboss.ejb3.proxy.JBossProxy)

      +- topic (class: org.jnp.interfaces.NamingContext)
      +- queue (class: org.jnp.interfaces.NamingContext)
      | +- ExpiryQueue (class: org.jboss.jms.destination.JBossQueue)
      | +- DLQ (class: org.jboss.jms.destination.JBossQueue)
      +- ConnectionFactory (class: org.jboss.jms.client.JBossConnectionFactory)
      +- UserTransaction (class: org.jboss.tm.usertx.client.ClientUserTransaction)
      +- jmx (class: org.jnp.interfaces.NamingContext)
      | +- invoker (class: org.jnp.interfaces.NamingContext)
      | | +- RMIAdaptor (proxy: $Proxy80 implements interface org.jboss.jmx.adaptor.rmi.RMIAdaptor,interface org.jboss.jmx.adaptor.rmi.RMIAdaptorExt)
      | +- rmi (class: org.jnp.interfaces.NamingContext)
      | | +- RMIAdaptor[link -> jmx/invoker/RMIAdaptor] (class: javax.naming.LinkRef)
      +- ClusteredXAConnectionFactory (class: org.jboss.jms.client.JBossConnectionFactory)
      +- TomcatAuthenticators (class: java.util.Properties)
      +- HiLoKeyGeneratorFactory (class: org.jboss.ejb.plugins.keygenerator.hilo.HiLoKeyGeneratorFactory)

      --------------- Eclipse Console -------

      12:05:18,856 INFO [JBossASKernel] Created KernelDeployment for: EJBProject.jar
      12:05:18,856 INFO [JBossASKernel] installing bean: jboss.j2ee:ear=enterpriseproject.ear,jar=EJBProject.jar,name=ThirdSessionBean,service=EJB3
      12:05:18,856 INFO [JBossASKernel] with dependencies:
      12:05:18,856 INFO [JBossASKernel] and demands:
      12:05:18,856 INFO [JBossASKernel] jboss.ejb:service=EJBTimerService
      12:05:18,857 INFO [JBossASKernel] and supplies:
      12:05:18,857 INFO [JBossASKernel] Class:com.tsb.test.ThirdSession
      12:05:18,857 INFO [JBossASKernel] jndi:ThirdSessionBean
      12:05:18,857 INFO [JBossASKernel] jndi:enterpriseproject/ThirdSessionBean/remote-com.tsb.test.ThirdSession
      12:05:18,857 INFO [JBossASKernel] jndi:enterpriseproject/ThirdSessionBean/remote
      12:05:18,857 INFO [JBossASKernel] Added bean(jboss.j2ee:ear=enterpriseproject.ear,jar=EJBProject.jar,name=ThirdSessionBean,service=EJB3) to KernelDeployment of: EJBProject.jar
      12:05:18,967 INFO [EJBContainer] STARTED EJB: com.tsb.test.ThirdSessionBean ejbName: ThirdSessionBean
      12:05:19,012 INFO [TomcatDeployment] deploy, ctxPath=/DynamicWebProject, vfsUrl=enterpriseproject.ear/DynamicWebProject.war
      12:06:44,247 INFO [TomcatDeployment] undeploy, ctxPath=/DynamicWebProject, vfsUrl=enterpriseproject.ear/DynamicWebProject.war
      12:06:44,256 INFO [EJBContainer] STOPPED EJB: com.tsb.test.ThirdSessionBean ejbName: ThirdSessionBean
      12:06:44,624 INFO [STDOUT] ======> Creating interceptor metadata bridge
      12:06:44,650 INFO [JBossASKernel] Created KernelDeployment for: EJBProject.jar
      12:06:44,651 INFO [JBossASKernel] installing bean: jboss.j2ee:ear=enterpriseproject.ear,jar=EJBProject.jar,name=ThirdSessionBean,service=EJB3
      12:06:44,651 INFO [JBossASKernel] with dependencies:
      12:06:44,651 INFO [JBossASKernel] and demands:
      12:06:44,651 INFO [JBossASKernel] jboss.ejb:service=EJBTimerService
      12:06:44,651 INFO [JBossASKernel] and supplies:
      12:06:44,651 INFO [JBossASKernel] Class:com.tsb.test.ThirdSession
      12:06:44,651 INFO [JBossASKernel] jndi:ThirdSessionBean
      12:06:44,651 INFO [JBossASKernel] jndi:enterpriseproject/ThirdSessionBean/remote-com.tsb.test.ThirdSession
      12:06:44,651 INFO [JBossASKernel] jndi:enterpriseproject/ThirdSessionBean/remote
      12:06:44,651 INFO [JBossASKernel] Added bean(jboss.j2ee:ear=enterpriseproject.ear,jar=EJBProject.jar,name=ThirdSessionBean,service=EJB3) to KernelDeployment of: EJBProject.jar
      12:06:44,759 INFO [EJBContainer] STARTED EJB: com.tsb.test.ThirdSessionBean ejbName: ThirdSessionBean
      12:06:44,785 INFO [TomcatDeployment] deploy, ctxPath=/DynamicWebProject, vfsUrl=enterpriseproject.ear/DynamicWebProject.war
      12:07:30,999 INFO [STDOUT] onject returned is jboss.j2ee:ear=enterpriseproject.ear,jar=EJBProject.jar,name=ThirdSessionBean,service=EJB3
      12:07:31,000 INFO [STDOUT] This works
      12:07:31,000 ERROR [[SecondServlet]] Servlet.service() for servlet SecondServlet threw exception
      java.lang.ClassCastException: $Proxy112 cannot be cast to com.tsb.test.ThirdSession
      at SecondServlet.doGet(SecondServlet.java:55)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)

      ---------------- Question --------

        • 1. Re:  JBoss 5.0.0.CR1 - EJB Lookup Successful but ClassCastEx
          sd dsf Newbie

          No Idea how I missed my question. Sorry

          Question is

          Above is the console output when I deployed to code and executed the SecondServlet

          The servlet is successful in doning the JNDI lookup and obtains Proxy112 object.

          As per JNDIView of the server Proxy112 implements the interfaces $Proxy112 implements interface com.tsb.test.ThirdSession,interface org.jboss.ejb3.proxy.JBossProxy

          In my servlet I can cast Proxy112 to org.jboss.ejb3.proxy.JBossProxy however I can not cast the same object to com.tsb.test.ThirdSession and get exception java.lang.ClassCastException: $Proxy112 cannot be cast to com.tsb.test.ThirdSession

          Can someone please help me and let me know what am I doing wrong?

          • 2. Re:  JBoss 5.0.0.CR1 - EJB Lookup Successful but ClassCastEx
            sd dsf Newbie

            After multiple trials and some hints from other posts I figured out the solution to the project.

            Basically the ThirdSession.class (a business interface for EJB) was getting loaded 2 times. One was from EJB project and another from WAR project. Even though my Servlet is making a remote EJB call I modified my eclipse setup so that the business interface class is not loaded as a part of WAR file and only loaded as a part of EJB jar file. Wallah, it worked as expected.

            If I deploy the dynamic project along with the business interface in another JBOSS instance then also the code works indicating that one JVM can not have more than one class loaded thru different deployed components.

            Seems to be problem in the jboss implementation. This means the the person who is building need to know beforehand how the deployment is going to be. Even though the EJB invocation is thru remote invocation, if EJB and its client are in same JBOSS JVM then care has to be taken so that class is not loaded multiple times. As FYI I never faced similar problem while working with Websphere.