websphereejb-3.0ejb-3.1was

Exception in thread "P=69052:O=0:CT" java.lang.ClassCastException: org.omg.stub.java.rmi._Remote_Stub


I'm using was 9.0, EJB 3.0 and I written a RMI class to call the remote's EJB. When I run, the console shows me this error:

Exception in thread "P=69052:O=0:CT" java.lang.ClassCastException: org.omg.stub.java.rmi._Remote_Stub

The RMI call:

MyEjbRemote ejb = (MyEjbRemote) ctx.lookup(JNDI_NAME_EJB);

The remote class has the @Statless and @Remote tags. Does anyone could help me?


Solution

  • WebSphere uses RMI/IIOP to provide support for EJB remote interfaces, which requires a client side _Stub class that implements the EJB remote interface. When the ctx.lookup() occurs in a managed thread, the _Stub class would normally be generated for you automatically by the container and returned from the lookup, which could then be cast to the remote interface.

    Unfortunately, there are a variety of reasons this may not occur. For example, the lookup is performed from a thin client. For those scenarios, you will need to do one or both of the following:

    1 - Perform a PortableRemoteObject.narrow() :

    MyEjbRemote ejb = (MyEjbRemote) PortableRemoteObject.narrow(ctx.lookup(JNDI_NAME_EJB), MyEjbRemote.class);
    

    This typically solves issues where the Naming service may have cached a value that has not been narrowed, or perhaps has been narrowed using a different classloader.

    2 - Generate the _Stub class for the remote interface and package it with the application code that is performing the lookup. The createEJBStubs.sh/.bat command provided by WebSphere may be used to generate the _Stub class. Also, if the remote interface extends java.rmi.Remote, then you may instead use the RMIC command from the JDK with the -iiop option. Information about createEJBStubs may be found here:

    https://www.ibm.com/docs/en/was/9.0.5?topic=beans-create-stubs-command

    This solves issues where the thread performing the lookup is not a managed thread. A managed thread is a thread created by the container specifically for running an application. Managed threads contain application context information and have the ability to dynamically generate EJB artifacts, such as _Stub classes as needed.