users@glassfish.java.net

Conjoint using of EJB3 and EJB2

From: Dr. Martin H. Ludwig <develop_at_gedim.de>
Date: Mon, 05 Jan 2009 15:43:00 +0100

Hello,

first: Sorry for crossposting to the users- and the ejb-list, but I was
not shure about the correct list because there is very low traffic on
the ejb-list.

I have the following problem:

I made two beans for glassfish: one with annotations and only two
classes (Bean2 and Bean2Impl) and the other one with a
deployment-descriptor and three classes (SimpleBeanHome, SimpleBean and
SimpleBeanImpl). I deployed these classes in two different EAR-files.

I made a client first talking to the EJB3-bean and then to the
EJB2-bean. The first connection works well, the second connection gives
to following exception:

java.lang.ClassCastException:
com.webage.ejbs._SimpleBeanHome_DynamicStub cannot be cast to
com.webage.ejbs.SimpleBean
    at com.webage.client.TestClient.runTest(TestClient.java:25)
    at com.webage.client.TestClient.main(TestClient.java:33)

Has anyone an idea for a solution or an example?

Thank you for your help!

Martin



Here the content of the files:

ejb-jar.xml:
---
<?xml version="1.0" encoding="ASCII"?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" version="3.0">
  <display-name>GFT1EJB</display-name>
    <enterprise-beans>
      <session>
          <description>SimpleBean</description>
          <display-name>SimpleBean</display-name>
          <ejb-name>SimpleBean</ejb-name>
          <home>com.webage.ejbs.SimpleBeanHome</home>
          <remote>com.webage.ejbs.SimpleBean</remote>
          <ejb-class>com.webage.ejbs.SimpleBeanImpl</ejb-class>
          <session-type>Stateless</session-type>
          <transaction-type>Container</transaction-type>
      </session>
      </enterprise-beans>
</ejb-jar>
---
sun-ejb-jar.xml:
---
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application
Server 9.0 EJB 3.0//EN"
"http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd">
<sun-ejb-jar>
  <enterprise-beans>
    <ejb>
      <ejb-name>SimpleBean</ejb-name>
      <jndi-name>ejb/SimpleBean</jndi-name>
    </ejb>
  </enterprise-beans>
</sun-ejb-jar>
---
SimpleBean.java:
---
package com.webage.ejbs;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface SimpleBean extends EJBObject {
    public String sayHello(String name) throws RemoteException;
}
---
SimpleBeanHome.java:
---
package com.webage.ejbs;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface SimpleBeanHome extends EJBHome {
    public static final String JNDI_NAME = "SimpleBeanHome";
    SimpleBean create() throws CreateException, RemoteException;
}
---
SimpleBeanImpl.java:
---
package com.webage.ejbs;
import java.rmi.RemoteException;
import javax.ejb.*;
public class SimpleBeanImpl implements SessionBean {
    private static final long serialVersionUID = -5166616433705338964L;
    protected SessionContext ejbContext;
    public String sayHello(String name){
        return "Guten Tag 2 " + name + "!";
    }
    public void ejbActivate() throws EJBException, RemoteException {}
    public void ejbPassivate() throws EJBException, RemoteException {}
    public void ejbRemove() throws EJBException, RemoteException {}
    public void setSessionContext(SessionContext ctx) throws
EJBException, RemoteException {
        this.ejbContext = ctx;
    }
}
---
and TestClient.java:
---
package com.webage.client;
import javax.naming.*;
import com.meins.ejbs.Bean2;
import com.webage.ejbs.SimpleBean;
public class TestClient {
    public void runTest() throws Exception {
        InitialContext ctx = new InitialContext();
        // the ejb3-bean
        Bean2 bean2 = (Bean2) ctx.lookup("ejb/Bean2JNDI");
        String result2 = bean2.sayHello("Billy Bob");
        System.out.println(result2);
        // the ejb2-bean
        SimpleBean bean = (SimpleBean) ctx.lookup("ejb/SimpleBean");
        String result = bean.sayHello("Bully Bib");
        System.out.println(result);
    }
    public static void main(String[] args) {
        try {
            TestClient cli = new TestClient();
            cli.runTest();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
---