3 Replies Latest reply on Jun 17, 2010 8:03 PM by snacker

    PortableRemoteObject problems

    marcelk1607

      Hello,

      I have a simple Hello World bean and remote, but I cant get it to work.
      I keep on getting a ClassCastException in my client with PortableRemoteObject.narrow

      What am I missing here??


      package nl.mkoopman.helloworld;
      
      import java.util.Properties;
      
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.rmi.PortableRemoteObject;
      
      public class HelloWorldClient {
      
       public static void main(String[] args) throws NamingException {
       InitialContext context = getInitialContext();
       java.lang.Object ref = context.lookup("HelloBean/remote");
       HelloRemote helloRemote= (HelloRemote) PortableRemoteObject.narrow(ref, HelloRemote.class);
       System.out.println("Hello World says: "+helloRemote.sayHello());
      
       }
      
       private static InitialContext getInitialContext() throws NamingException
       {
       Properties properties = new Properties();
       properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
       properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
       properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
       return new InitialContext(properties);
       }
      
      }


      package nl.mkoopman.helloworld;
      
      import javax.ejb.Remote;
      
      @Remote
      public interface HelloRemote {
      
       public String sayHello();
      }
      


      package nl.mkoopman.helloworld;
      
      import javax.ejb.Stateless;
      
      @Stateless
      public class HelloBean implements HelloRemote {
      
       @Override
       public String sayHello() {
       return "Hello!";
       }
      }
      


      Exception in thread "main" java.lang.ClassCastException
       at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229)
       at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
       at nl.mkoopman.helloworld.HelloWorldClient.main(HelloWorldClient.java:22)
      Caused by: java.lang.ClassCastException: javax.naming.Reference cannot be cast to org.omg.CORBA.Object
       at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:212)
       ... 2 more
      


      16:30:17,257 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=helloWorld.jar,name=HelloBean,service=EJB3
      16:30:17,257 INFO [EJBContainer] STARTED EJB: nl.mkoopman.helloworld.HelloBean ejbName: HelloBean
      16:30:17,269 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
      
       HelloBean/remote - EJB3.x Default Remote Business Interface
       HelloBean/remote-nl.mkoopman.helloworld.HelloRemote - EJB3.x Remote Business Interface
      
      


        • 1. Re: PortableRemoteObject problems
          zecas

          Hi,

           

          I'm facing the same problem.

           

          I'm using JBoss 5.1.0.GA and getting the same problem.

           

          Have you found a solution? I've tried several solutions I found online, but with no success.

           

           

          Thanks

          • 2. Re: PortableRemoteObject problems
            snacker

            Have you tried not using "PortableRemoteObject.narrow(...)"?

             

            IIRC the EJB3 object being returned from the lookup(...) is already castable to your interface, so you shouldn't need to use the "narrow" method.

             

            What you could do is check to see if the reference returned is already assignable to your interface.

            If it is, then don't do the narrow.

            If it isn't then "try" the narrow.

             

            Something like...

             

            HelloRemote remote;

            if( ! HelloRemote.class.isAssignableFrom( ref ) ){

                remote = (HelloRemote) PortableRemoteObject.narrow( ref, HelloRemote.class );

            }else{

                remote = (HelloRemote) ref;

            }

            • 3. Re: PortableRemoteObject problems
              snacker

              Actually you must have found your error already... it looks like your lookup string is wrong "HelloBean/remote" should probabaly be "ejb/HelloBean" or something.