0

我正在尝试构建一个简单的 Hello world 类型的 EJB 2.1 应用程序。这个应用程序的预期运行时应该是 Jboss 5.1.0。这是我写的代码。

EJB 配置文件:

ejb/TestEJBInterfaceBean com.TestEJB.TestEJBInterfaceHome com.TestEJB.TestEJBInterfaceRemote com.TestEJB.TestEJBInterfaceBean 无状态容器

主页界面:

import javax.ejb.EJBHome;

public interface TestEJBInterfaceHome extends EJBHome {
    public TestEJBInterfaceRemote create() throws java.rmi.RemoteException,
            javax.ejb.CreateException;
}

Remote Interface:

import java.rmi.RemoteException;
import javax.ejb.EJBObject;

public interface TestEJBInterfaceRemote extends EJBObject { 

    public String ping(String version) throws RemoteException;
}

豆类:

import java.rmi.RemoteException;
import java.util.Date;

import org.jboss.logging.Logger;

public class TestEJBIInterfaceBean extends BaseSessionBean implements TestEJBIInterfaceRemote{

    private static final long serialVersionUID = 1L;

    final Logger log = Logger.getLogger(TestEJBIInterfaceBean.class);   


    public String ping(String arg0) throws RemoteException {

        String response = this.getClass().getSimpleName() + " pinged @ " + new Date().getTime();

        log.info(response);
        return response;
    }

    public void ejbActivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub

    }

    public void ejbPassivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub

    }

    public void ejbRemove() throws EJBException, RemoteException {
        // TODO Auto-generated method stub

    }

    public void setSessionContext(SessionContext arg0) throws EJBException,
            RemoteException {
        // TODO Auto-generated method stub

    }

    public EJBHome getEJBHome() throws RemoteException {
        // TODO Auto-generated method stub
        return null;
    }

    public Handle getHandle() throws RemoteException {
        // TODO Auto-generated method stub
        return null;
    }

    public Object getPrimaryKey() throws RemoteException {
        // TODO Auto-generated method stub
        return null;
    }

    public boolean isIdentical(EJBObject arg0) throws RemoteException {
        // TODO Auto-generated method stub
        return false;
    }

    public void remove() throws RemoteException, RemoveException {
        // TODO Auto-generated method stub

    }

}

测试客户端:

public class TestEJBInterfaceClientTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            Context ctx = new InitialContext();

            TestEJBInterfaceRemote obj = (TestEJBInterfaceRemote)ctx.lookup("ejb/TestEJBInterfaceBean");

            System.out.println(obj.ping("12345"));

        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }
}

当我运行客户端时,我收到以下错误:

Exception in thread "main" java.lang.ClassCastException: $Proxy0 cannot be cast to com.TestEJB.TestEJBInterfaceRemote
    at TestEJBInterfaceClientTest.main(TestEJBInterfaceClientTest.java:21)

该错误似乎表明演员表是错误的,但我怀疑演员表与错误无关。(我试图转换为 TestEJBInterfaceHome 但我得到了同样的错误)。我的怀疑实际上是在应用程序的版本中。

问题

  • Jboss 是否有可能将其视为 EJB3 应用程序?查看配置文件,我没有指定这是一个 EJB2.1,所以这可能会导致问题吗?
  • 有没有办法找出ctx.lookup调用返回的类型?我试过了getClass().getNamegetClass().getCanonicalName()我得到的只是名字之类的$proxy0, $proxy20
  • 我错过了什么明显的东西吗?
4

1 回答 1

2

EJB-3.0 之前的查找结果是主界面,因此请尝试转换为TestEJBInterfaceHome。请注意,为了可移植性,您需要在转换为目标接口之前对 ctx.lookup 的返回值使用PortableRemoteObject.narrow

实际类型是代理类,因此getClass().getName()返回正确的东西。要确定类实现了哪些接口,请使用getClass().getInterfaces().

于 2012-12-10T20:26:43.470 回答