6 Replies Latest reply on Apr 28, 2010 2:42 AM by nk_2010

    EJB3: cannot be cast

    marwestermann

      Hi,

      I've a problem and hope anyone can help me.

      I've a EJB3 module deployed to a JBoss JBoss-4.2.2-GA. The module consists the following classes/interfaces:

      package com.zoo;
      
      import javax.ejb.Remote;
      
      @Remote
      public interface Tiger {
       public String roar();
      }
      

      package com.zoo;
      
      import javax.ejb.Stateless;
      
      @Stateless
      public class TigerBean implements Tiger {
       public String roar() {
       return "Roar!!!";
       }
      }
      


      After deploying the module the global JNDI Namespace is like:

      +- TopicConnectionFactory (class: org.jboss.naming.LinkRefPair)
       +- jmx (class: org.jnp.interfaces.NamingContext)
       | +- invoker (class: org.jnp.interfaces.NamingContext)
       | | +- RMIAdaptor (proxy: $Proxy47 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)
       +- TigerBean (class: org.jnp.interfaces.NamingContext)
       | +- remote (proxy: $Proxy71 implements interface com.zoo.Tiger,interface org.jboss.ejb3.JBossProxy)
       +- HTTPXAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
       +- ConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)
       +- Tiger (class: org.jnp.interfaces.NamingContext)
       +- UserTransactionSessionFactory (proxy: $Proxy14 implements interface org.jboss.tm.usertx.interfaces.UserTransactionSessionFactory)
       +- HTTPConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)
       +- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
       +- TransactionSynchronizationRegistry (class: com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple)
       +- UserTransaction (class: org.jboss.tm.usertx.client.ClientUserTransaction)
       +- UILXAConnectionFactory[link -> XAConnectionFactory] (class: javax.naming.LinkRef)
       +- UIL2XAConnectionFactory[link -> XAConnectionFactory] (class: javax.naming.LinkRef)
       +- queue (class: org.jnp.interfaces.NamingContext)
       | +- A (class: org.jboss.mq.SpyQueue)
       | +- testQueue (class: org.jboss.mq.SpyQueue)
       | +- ex (class: org.jboss.mq.SpyQueue)
       | +- DLQ (class: org.jboss.mq.SpyQueue)
       | +- D (class: org.jboss.mq.SpyQueue)
       | +- C (class: org.jboss.mq.SpyQueue)
       | +- B (class: org.jboss.mq.SpyQueue)
       +- topic (class: org.jnp.interfaces.NamingContext)
       | +- testDurableTopic (class: org.jboss.mq.SpyTopic)
       | +- testTopic (class: org.jboss.mq.SpyTopic)
       | +- securedTopic (class: org.jboss.mq.SpyTopic)
       +- console (class: org.jnp.interfaces.NamingContext)
       | +- PluginManager (proxy: $Proxy48 implements interface org.jboss.console.manager.PluginManagerMBean)
       +- UIL2ConnectionFactory[link -> ConnectionFactory] (class: javax.naming.LinkRef)
       +- HiLoKeyGeneratorFactory (class: org.jboss.ejb.plugins.keygenerator.hilo.HiLoKeyGeneratorFactory)
       +- UILConnectionFactory[link -> ConnectionFactory] (class: javax.naming.LinkRef)
       +- QueueConnectionFactory (class: org.jboss.naming.LinkRefPair)
       +- UUIDKeyGeneratorFactory (class: org.jboss.ejb.plugins.keygenerator.uuid.UUIDKeyGeneratorFactory)
      


      After that I developed a test-Client with the following code:
      public class Visitor {
      
       public void run() {
      
       try {
       InitialContext ctx = new InitialContext();
       Tiger bean = (Tiger) ctx.lookup("TigerBean");
      
       System.out.println(bean.roar());
       //System.out.println(Visitor.class.getSimpleName());
       }
       catch (Exception e){
       e.printStackTrace();
       }
       }
      
      
      
       public static void main(String[] args) {
       new Visitor().run();
      
       }
      
      }
      


      Trying to run it I get the following exception:

      java.lang.ClassCastException: org.jnp.interfaces.NamingContext cannot be cast to com.zoo.Tiger

      Can me somebody help with that case.

      Thx Marco

        • 1. Re: EJB3: cannot be cast
          waynebaylor

          what are your jndi properties set to? are you using a jndi.properties file?

          • 2. Re: EJB3: cannot be cast
            marwestermann

            hi,

            yes my jndi.properties looks like:

            java.naming.provider.url=localhost:1099
            java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
            java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
            jnp.socket.Factory=org.jnp.interfaces.TimedSocketFactory
            


            • 3. Re: EJB3: cannot be cast
              jaikiran

               

              +- TigerBean (class: org.jnp.interfaces.NamingContext)
              | +- remote (proxy: $Proxy71 implements interface com.zoo.Tiger,interface org.jboss.ejb3.JBossPr
              oxy)


              So your lookup string should be TigerBean/remote

              Tiger bean = (Tiger) ctx.lookup("TigerBean/remote");




              • 4. Re: EJB3: cannot be cast
                marwestermann

                Thank you it works..

                I tried several times "TigerBean/Remote" but then I got a NameNotFoundException..

                my fault..

                thanks again

                Marco

                • 5. Re: EJB3: cannot be cast
                  jaikiran

                   

                  "MarWestermann" wrote:


                  I tried several times "TigerBean/Remote" but then I got a NameNotFoundException..



                  The JNDI names are case-sensitive. The r in Remote should have been in lower case.

                  "MarWestermann" wrote:

                  Thank you it works..


                  Great !!

                  • 6. Re: EJB3: cannot be cast

                    Hi Jaikiran,

                     

                    I do have same problem, Pls see below -

                     

                    java.lang.ClassCastException: org.jnp.interfaces.NamingContext cannot be cast to com.tarangtech.mtg.authentication.AuthenticationRemote
                         com.tarangtech.framework.ejb.clients.util.CommonPaymentAction.getAuthentication(CommonPaymentAction.java:34)
                         com.tarangtech.mtg.struts.AuthenticationAction.execute(AuthenticationAction.java:24)

                    Here is my lookup code -

                    public static final String RemoteJNDIName =  "AuthenticationBean/remote";
                    AuthenticationRemote authEjb = (AuthenticationRemote)ServiceLocator.getEJB(AuthenticationBean.RemoteJNDIName);
                    System.out.println("EJB Look up successfull ");  

                    my jmx-console -

                     +- AuthenticationBean (class: org.jnp.interfaces.NamingContext)
                      |   +- remote (class: org.jnp.interfaces.NamingContext)
                      |   |   +- UserTransaction[link -> UserTransaction] (class: javax.naming.LinkRef)
                      |   |   +- metaData (class: org.jboss.metadata.client.jboss.JBossClientMetaData)
                      |   |   +- env (class: org.jnp.interfaces.NamingContext)
                      |   |   |   +- com.tarangtech.mtg.authentication.AuthenticationBean[link -> AuthenticationBean/remote] (class: javax.naming.LinkRef)
                      |   |   +- classPathEntries (class: java.util.ArrayList)
                    I have verified for jar file incompatabilities with
                    Jboss server & my application. Don't get the mistake, request you to help me to proceed further...

                    Thanks,
                    NK