1
votes

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.

  • I have an Enterprise Application project named P001_EAR.
  • I have a Dynamic Web Project named P001_WAR.
  • I have a EJB Project named P001_EJB.
  • I have a EJB Client Project named P001_EJBClient.

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:

  • META-INF
  • P001_EJB.jar
  • P001_WAR.war
  • P001_EJBClient.jar

Inside META-INF I have:

  • application.xml

Inside P001_EJB.jar I have:

  • META-INF\ejb-jar.xml
  • META-INF\jboss.xml
  • META-INF\MANIFEST.MF
  • com\p001\ejb\Test1SLBBean.class
  • com\p001\ejb\Test1SLBSession.class

Inside P001_WAR.war I have:

  • META-INF\MANIFEST.MF
  • WEB-INF\web.xml
  • WEB-INF\classes\com\p001\listener\P001Listener.class
  • WEB-INF\lib

Inside P001_EJBClient.jar I have:

  • META-INF\MANIFEST.MF
  • com\p001\ejb\Test1SLB.class
  • com\p001\ejb\Test1SLBHome.class
  • com\p001\ejb\Test1SLBLocal.class
  • com\p001\ejb\Test1SLBLocalHome.class
  • com\p001\ejb\Test1SLBUtil.class

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?

1
How does your deployment look like? what jars are in lib?Tomaz Cerar
@ctomc just updated the question to include deployment details. Please take a look.ChumboChappati
Just one remark, why are you using EJB 2.1 while you can use EJB 3.2 which is part of Java EE 7 ?Rémi Bantos
@Rémi I am testing EJB 2.x on WildFly 8.x as currently we use EJB 2.x on JBoss 4.x and we are looking at upgrading to WildFly 8.x but still stay at EJB 2.x (for some unknown reasons)ChumboChappati
Can you double check that your WAR's WEB-INF/lib is empty?jpkrohling

1 Answers

1
votes

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.