ChumboChappati
ChumboChappati

Reputation: 1544

Why I am getting ClassCastException when accessing EJB 2.1 in Wildfly 8.2.1?

I am working with Java 1.7, XDoclet 1.2.3, WildFly 8.2.1.Final, Dynamic Web Module 2.5, EJB 2.1 in Eclipse Luna.

In P001_EJB I create a XDoclet Stateless Session Bean (EJB 2.1).

This is its remote interface:

package com.p001.ejb;

/**
 * Remote interface for Test1SLB.
 * @generated 
 * @wtp generated
 */
public interface Test1SLB extends javax.ejb.EJBObject
{
   /**
    * <!-- begin-xdoclet-definition -->
    * @generated //TODO: Must provide implementation for bean method stub    */
   public java.lang.String foo( java.lang.String param )
      throws java.rmi.RemoteException;    
}

This is its home interface:

package com.p001.ejb;

/**
 * Home interface for Test1SLB.
 * @generated 
 * @wtp generated
 */
public interface Test1SLBHome extends javax.ejb.EJBHome
{
   public static final String COMP_NAME="java:comp/env/ejb/Test1SLB";
   public static final String JNDI_NAME="Test1SLB";

   public com.p001.ejb.Test1SLB create()
      throws javax.ejb.CreateException,java.rmi.RemoteException;
}

In P001_WAR I created a Listener class named P001Listener; In its contextInitialized method I am trying to call foo method of Test1SLB EJB. This is its code:

public class P001Listener implements ServletContextListener {

    public P001Listener() {
    }

    public void contextInitialized(ServletContextEvent sce)  { 
         System.out.println("P001Listener.java: contextInitialized(ServletContextEvent sce): inside");
         String test1SLBJNDIName = null;
         Class test1SLBHomeClass = null;
         InitialContext initialContext = null;
         Object namedObject = null;
         Object ejbHomeObject = null;
         Test1SLBHome test1SLBHome = null;
         Test1SLB test1SLB = null;
         String rtnValue = null;


         try {

            test1SLBJNDIName = "java:global/P001_EAR/P001_EJB/Test1SLB!com.p001.ejb.Test1SLB";
            System.out.println("P001Listener.java: contextInitialized(ServletContextEvent sce): test1SLBJNDIName=" + test1SLBJNDIName);

            test1SLBHomeClass = Test1SLBHome.class;
            System.out.println("P001Listener.java: contextInitialized(ServletContextEvent sce): test1SLBHomeClass=" + test1SLBHomeClass);

            initialContext = new InitialContext();
            System.out.println("P001Listener.java: contextInitialized(ServletContextEvent sce): initialContext=" + initialContext);

            namedObject = initialContext.lookup(test1SLBJNDIName);
            System.out.println("P001Listener.java: contextInitialized(ServletContextEvent sce): namedObject=" + namedObject);

            ejbHomeObject = PortableRemoteObject.narrow(namedObject, test1SLBHomeClass);
            System.out.println("P001Listener.java: contextInitialized(ServletContextEvent sce): ejbHomeObject=" + ejbHomeObject);

            test1SLBHome = (Test1SLBHome) ejbHomeObject;
            System.out.println("P001Listener.java: contextInitialized(ServletContextEvent sce): test1SLBHome=" + test1SLBHome);

            test1SLB = test1SLBHome.create();
            System.out.println("P001Listener.java: contextInitialized(ServletContextEvent sce): test1SLB=" + test1SLB);

            rtnValue = test1SLB.foo("pagal");
            System.out.println("P001Listener.java: contextInitialized(ServletContextEvent sce): rtnValue=" + rtnValue);

        } catch (NamingException ne) {
            ne.printStackTrace();

        } catch (ClassCastException cce) {
            cce.printStackTrace();

        } catch (RemoteException re) {
            re.printStackTrace();

        } catch (CreateException ce) {
            ce.printStackTrace();

        }
    }

    public void contextDestroyed(ServletContextEvent sce)  { 
        System.out.println("P001Listener.java: contextDestroyed(ServletContextEvent sce): inside");
    }

}

I deploy the P001_EAR on WildFly. This is how the deployment looks:

P001_EAR.ear

Inside P001_EAR.ear I have:

Inside META-INF I have:

Inside P001_EJB.jar I have:

Inside P001_WAR.war I have:

Inside P001_EJBClient.jar I have:

I ran WildFly. In the server.log file I see that the EJB is deployed successfully:

2015-12-08 11:21:58,671 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-3) JNDI bindings for session bean named Test1SLB in deployment unit subdeployment "P001_EJB.jar" of deployment "P001_EAR.ear" are as follows:

java:global/P001_EAR/P001_EJB/Test1SLB!com.p001.ejb.Test1SLBHome java:app/P001_EJB/Test1SLB!com.p001.ejb.Test1SLBHome java:module/Test1SLB!com.p001.ejb.Test1SLBHome java:jboss/exported/P001_EAR/P001_EJB/Test1SLB!com.p001.ejb.Test1SLBHome java:global/P001_EAR/P001_EJB/Test1SLB!com.p001.ejb.Test1SLBLocalHome java:app/P001_EJB/Test1SLB!com.p001.ejb.Test1SLBLocalHome java:module/Test1SLB!com.p001.ejb.Test1SLBLocalHome java:global/P001_EAR/P001_EJB/Test1SLB!com.p001.ejb.Test1SLB java:app/P001_EJB/Test1SLB!com.p001.ejb.Test1SLB java:module/Test1SLB!com.p001.ejb.Test1SLB java:jboss/exported/P001_EAR/P001_EJB/Test1SLB!com.p001.ejb.Test1SLB java:global/P001_EAR/P001_EJB/Test1SLB!com.p001.ejb.Test1SLBLocal java:app/P001_EJB/Test1SLB!com.p001.ejb.Test1SLBLocal java:module/Test1SLB!com.p001.ejb.Test1SLBLocal

But I get java.lang.ClassCastException on this line:

ejbHomeObject = PortableRemoteObject.narrow(namedObject, test1SLBHomeClass);

This is the server.log:

2015-12-08 11:21:59,158 INFO [stdout] (MSC service thread 1-9) P001Listener.java: contextInitialized(ServletContextEvent sce): inside

2015-12-08 11:21:59,159 INFO [stdout] (MSC service thread 1-9) P001Listener.java: contextInitialized(ServletContextEvent sce): test1SLBJNDIName=java:global/P001_EAR/P001_EJB/Test1SLB!com.p001.ejb.Test1SLB

2015-12-08 11:21:59,161 INFO [stdout] (MSC service thread 1-9) P001Listener.java: contextInitialized(ServletContextEvent sce): test1SLBHomeClass=interface com.p001.ejb.Test1SLBHome

2015-12-08 11:21:59,164 INFO [stdout] (MSC service thread 1-9) P001Listener.java: contextInitialized(ServletContextEvent sce): initialContext=javax.naming.InitialContext@2db02a6a

2015-12-08 11:21:59,171 INFO [org.jboss.ejb.client] (MSC service thread 1-9) JBoss EJB Client version 2.0.1.Final 2015-12-08 11:21:59,177 INFO [stdout] (MSC service thread 1-9) P001Listener.java: contextInitialized(ServletContextEvent sce): namedObject=Proxy for remote EJB StatelessEJBLocator{appName='P001_EAR', moduleName='P001_EJB', distinctName='', beanName='Test1SLB', view='interface com.p001.ejb.Test1SLB'}

2015-12-08 11:21:59,197 ERROR [stderr] (MSC service thread 1-9) java.lang.ClassCastException

2015-12-08 11:21:59,198 ERROR [stderr] (MSC service thread 1-9) at org.jboss.com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:246)

2015-12-08 11:21:59,200 ERROR [stderr] (MSC service thread 1-9) at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:158)

2015-12-08 11:21:59,201 ERROR [stderr] (MSC service thread 1-9) at com.p001.listener.P001Listener.contextInitialized(P001Listener.java:59)

2015-12-08 11:21:59,202 ERROR [stderr] (MSC service thread 1-9) at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:173)

2015-12-08 11:21:59,204 ERROR [stderr] (MSC service thread 1-9) at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:194)

2015-12-08 11:21:59,206 ERROR [stderr] (MSC service thread 1-9) at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:87)

2015-12-08 11:21:59,208 ERROR [stderr] (MSC service thread 1-9) at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.start(UndertowDeploymentService.java:72)

2015-12-08 11:21:59,210 ERROR [stderr] (MSC service thread 1-9) at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)

2015-12-08 11:21:59,211 ERROR [stderr] (MSC service thread 1-9) at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)

2015-12-08 11:21:59,212 ERROR [stderr] (MSC service thread 1-9) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

2015-12-08 11:21:59,214 ERROR [stderr] (MSC service thread 1-9) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

2015-12-08 11:21:59,215 ERROR [stderr] (MSC service thread 1-9) at java.lang.Thread.run(Thread.java:745)

2015-12-08 11:21:59,216 ERROR [stderr] (MSC service thread 1-9) Caused by: java.lang.ClassCastException: com.sun.proxy.$Proxy21 cannot be cast to org.omg.CORBA.Object

2015-12-08 11:21:59,218 ERROR [stderr] (MSC service thread 1-9) at org.jboss.com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:225)

2015-12-08 11:21:59,219 ERROR [stderr] (MSC service thread 1-9) ... 11 more

What I am doing wrong to get this error message?

Thanks

UPDATE

I have found a solution. When I change this code:

test1SLBJNDIName = "java:global/P001_EAR/P001_EJB/Test1SLB!com.p001.ejb.Test1SLB";

to this code:

test1SLBJNDIName = "java:global/P001_EAR/P001_EJB/Test1SLB!com.p001.ejb.Test1SLBHome";

then it worked. So basically I am now looking up JNDI Name of Home and then casting it to Home Class.

In the old JBoss 4.2.X, I look up the JNDI Name Test1SLB and then cast it to Home Class and it worked. So was there 1 JNDI Name Test1SLB used for both Remote and Home in the old JBoss 4.2.X?

Upvotes: 1

Views: 1751

Answers (1)

ChumboChappati
ChumboChappati

Reputation: 1544

I have found a solution. When I change this code:

test1SLBJNDIName = "java:global/P001_EAR/P001_EJB/Test1SLB!com.p001.ejb.Test1SLB";

to this code:

test1SLBJNDIName = "java:global/P001_EAR/P001_EJB/Test1SLB!com.p001.ejb.Test1SLBHome";

then it worked. So basically I am now looking up Home and then casting it to Home Class.

In the old JBoss 4.2.X, I look up the JNDI Name Test1SLB and then cast it to Home Class and it worked. So there was 1 JNDI Name Test1SLB used for both Remote and Home in the old JBoss 4.2.X.

Upvotes: 1

Related Questions