7 Replies Latest reply on Jul 30, 2007 12:00 PM by broneo

    ClassCastException on remote calling of an EJB3.0

    broneo

      Hello,

      i`m relativly new to Java and working on a project in which i`d like to call an EJB from a Desktop client. UNfortunately now i'm stuck. As far as i can tell the connection works fine. I' getting also something back from the server. As i understand it is the reference object of the searched bean. The problem is, that i get a ClassCastException if i try to cast this object to an BeanRemote object. This is my client code:

      public static void main(String args[]) throws NamingException, RemoteException {
      
       Hashtable env = new Hashtable();
       env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
       env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
       env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
      
       final Context ic = new InitialContext(env);
       Object client = ic.lookup("ClientBean");
       ClientLocal home = (ClientLocal)client; //This line causes The error
       System.out.println("ClientBean is bound to: " + client);
      
       java.awt.EventQueue.invokeLater(new Runnable() {
       public void run() {
       new Client().setVisible(true);
       }
       });
       }
      
       // Variables declaration - do not modify
       private javax.swing.JLabel jLabel1;
       private javax.swing.JButton showButton;
       private javax.swing.JTextField textFieldClient;
       private javax.swing.JButton updateButton;
       // End of variables declaration
      
      }
      


      The output i get:

      compile:
      run:
      2007-07-27 10:29:46.504 java[363] CFLog (0): CFMessagePort: bootstrap_register(): failed 1103 (0x44f), port = 0x10303, name = 'java.ServiceProvider'
      See /usr/include/servers/bootstrap_defs.h for the error codes.
      2007-07-27 10:29:46.505 java[363] CFLog (99): CFMessagePortCreateLocal(): failed to name Mach port (java.ServiceProvider)
      Exception in thread "main" java.lang.ClassCastException: org.jnp.interfaces.NamingContext
      at com.planetactive.ejb.client.Client.main(Client.java:106)

      Java Result: 1
      BUILD SUCCESSFUL (total time: 3 seconds)

      If i uncomment the described line, this is my output. As you can see i get

      org.jnp.interfaces.NamingContext@2cc95d

      back from my lookup.

      compile:
      run:
      2007-07-27 10:31:03.198 java[364] CFLog (0): CFMessagePort: bootstrap_register(): failed 1103 (0x44f), port = 0x10303, name = 'java.ServiceProvider'
      See /usr/include/servers/bootstrap_defs.h for the error codes.
      2007-07-27 10:31:03.198 java[364] CFLog (99): CFMessagePortCreateLocal(): failed to name Mach port (java.ServiceProvider)
      ClientBean is bound to: org.jnp.interfaces.NamingContext@2cc95d
      BUILD SUCCESSFUL (total time: 5 seconds)

      I've searched many forums for this problem. Seems te be an often seen problem. Unfortunately i couldn't find a solution. I hope you can help me to fix this.

      I'm using Jboss 4.2.0 GA and Netbeans.

      Thanks in advance

        • 1. Re: ClassCastException on remote calling of an EJB3.0
          peterj

          I suspect that you should be looking up:

          Object client = ic.lookup("ClientBean/remote");

          One way to verify this is to use the listAll method from JNDIView to dump the JNDI tree.

          • 2. Re: ClassCastException on remote calling of an EJB3.0
            broneo

            First of all thanks for the fast response.

            I tried the idea you had before. If i search for ClientBean/remote it is shown an error which says that the lookup didn`t work.

            As far as i can tell the lookup works fine as i get

            org.jnp.interfaces.NamingContext@e32802

            if i do the lookup without the casting. As i understood this is the reference to my bean. (please correct me if i`m wrong)
            I also find the Bean in the JMX console of my Application server. So i think it is really the casting that throws the error.

            Is it possible that some libs are missing?

            • 3. Re: ClassCastException on remote calling of an EJB3.0
              peterj

              The NamingContext object is similar to a directory on a disk, while the EJB is a file within a directory. Essnetially, you have not gone far enough down the naming tree to find the object you want.

              What are you seeing in JNDIView? Please post the directory tree you are seeing.

              • 4. Re: ClassCastException on remote calling of an EJB3.0
                broneo

                This is the part where the bean is listed. I hope it`s correct.

                Global JNDI Namespace

                +- TopicConnectionFactory (class: org.jboss.naming.LinkRefPair)
                +- jmx (class: org.jnp.interfaces.NamingContext)
                | +- invoker (class: org.jnp.interfaces.NamingContext)
                | | +- RMIAdaptor (proxy: $Proxy48 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)
                +- TEMControlTest (class: org.jnp.interfaces.NamingContext)
                +- ClientBean (class: org.jnp.interfaces.NamingContext)
                | +- remote (proxy: $Proxy66 implements interface com.planetactive.ejb.server.ClientRemote,interface org.jboss.ejb3.JBossProxy,interface javax.ejb.EJBObject)

                +- HTTPXAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
                +- ConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)
                +- UserTransactionSessionFactory (proxy: $Proxy14 implements interface org.jboss.tm.usertx.interfaces.UserTransactionSessionFactory)
                +- HTTPConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)


                Thank you for your help.

                • 5. Re: ClassCastException on remote calling of an EJB3.0
                  peterj

                  As you can see from the JNDI tree, the object you want is named "ClientBean/remote". That is what you should look up. If that name is causing an error, please psot the stack trace.

                  • 6. Re: ClassCastException on remote calling of an EJB3.0
                    broneo

                    Hello PeterJ,

                    first of all thanks a lot for your help.
                    This morning ich changed the lookupt name to "ClientBean/remote" as you proposed. Surprisingly this time ist works well.

                    This is the output i get:

                    run-jar:
                    ClientBean is bound to: jboss.j2ee:jar=TEM-server.jar,name=ClientBean,service=EJB3
                    log4j:WARN No appenders could be found for logger (org.jboss.security.SecurityAssociation).
                    log4j:WARN Please initialize the log4j system properly.
                    getClient returns: John Doe

                    I can`t tell why it works this time. I changed nothing except this line:

                    ClientRemote client = (ClientRemote)ic.lookup("ClientBean");


                    to this:

                    ClientRemote client = (ClientRemote)ic.lookup("ClientBean/remote");


                    The last time i tried this i got an error.

                    Thank you for your help.

                    • 7. Re: ClassCastException on remote calling of an EJB3.0
                      broneo

                      Hello again,

                      after trying to establich a database connection i get the error i got before when using ClientBean/remote.

                      This is my source code:

                      Hashtable env = new Hashtable();
                       env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
                       env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
                       env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
                      
                       final Context ic = new InitialContext(env);
                       ClientRemote client = (ClientRemote)ic.lookup("ClientBean/remote");
                       System.out.println("ClientBean is bound to: " + client);
                       TemControlClient test = client.getClient(1);
                       System.out.println("getClient returns: " + test);
                       test = client.getClient(3);
                       System.out.println("getClient returns: " + test);


                      and this is the output i got:

                      Exception in thread "main" javax.naming.NameNotFoundException: remote not bound
                      at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
                      at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
                      at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
                      at org.jnp.server.NamingServer.lookup(NamingServer.java:296)
                      at org.jnp.server.NamingServer.lookup(NamingServer.java:270)
                      at sun.reflect.GeneratedMethodAccessor81.invoke(Unknown Source)
                      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                      at java.lang.reflect.Method.invoke(Method.java:585)
                      at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
                      at sun.rmi.transport.Transport$1.run(Transport.java:153)
                      at java.security.AccessController.doPrivileged(Native Method)
                      at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
                      at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:466)
                      at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:707)
                      at java.lang.Thread.run(Thread.java:613)
                      at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
                      at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
                      at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:126)
                      at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
                      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:625)
                      at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
                      at javax.naming.InitialContext.lookup(InitialContext.java:351)
                      at com.planetactive.ejb.client.Main.main(Main.java:42)
                      Java Result: 1

                      The exception is thrwon in this line:

                      ClientRemote client = (ClientRemote)ic.lookup("ClientBean/remote");


                      So i think my program can`t find the JNDI Name.

                      Thnks for your help in advance.