java - EJB2 - $Proxy0 无法转换为 com.TestEJB.TestEJBInterfaceRemote 错误

标签 java jakarta-ee ejb ejb-3.0 ejb-2.x

我正在尝试构建一个简单的 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;
}

Bean 类:

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 调用返回的 Type 是什么?我尝试了 getClass().getNamegetClass().getCanonicalName() ,我得到的只是诸如 $proxy0、$proxy20 等名称.
  • 我是否错过了一些明显的事情?

最佳答案

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

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

关于java - EJB2 - $Proxy0 无法转换为 com.TestEJB.TestEJBInterfaceRemote 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13804962/

相关文章:

java - 二级缓存 : Spring 3. 2.2 + Hibernate 4.2.0 Infinispan

java - 在 XPath 中获取标签后的文本

java - 如何解决jsp到servlet的空间问题?

java - URL 太长时被截断

mysql - hibernate 连接超时

java - EJB异常,可能是什么原因造成的?注解?服务器 :GlassFish

jakarta-ee - 将@SessionScoped CDI Bean 注入(inject)@Stateless EJB

java - setConnectTimeout 不影响网关超时吗?

java - 如何将操作类的数据检索到其生成的 jsp 页面

Java EE 架构 - CDI Beans 属于哪里?