gpt4 book ai didi

java - 为什么在 Wildfly 8.2.1 中访问 EJB 2.1 时出现 ClassCastException?

转载 作者:行者123 更新时间:2023-11-30 07:44:10 35 4
gpt4 key购买 nike

我正在 Eclipse Luna 中使用 Java 1.7、XDoclet 1.2.3、W​​ildFly 8.2.1.Final、Dynamic Web Module 2.5、EJB 2.1。

  • 我有一个名为 P001_EAR 的企业应用程序项目。
  • 我有一个名为 P001_WAR 的动态 Web 项目。
  • 我有一个名为 P001_EJB 的 EJB 项目。
  • 我有一个名为 P001_EJBClient 的 EJB 客户端项目。

P001_EJB中,我创建了一个XDoclet Stateless Session Bean (EJB 2.1)。

这是它的远程接口(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;
}

这是它的主界面:

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;
}

P001_WAR中,我创建了一个名为P001Listener的Listener类;在其 contextInitialized 方法中,我尝试调用 Test1SLB EJB 的 foo 方法。这是它的代码:

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");
}

}

我在 WildFly 上部署了 P001_EAR。这是部署的样子:

P001_EAR.ear

P001_EAR.ear里面我有:

  • 元信息
  • P001_EJB.jar
  • P001_WAR.war
  • P001_EJBClient.jar

META-INF里面我有:

  • application.xml

P001_EJB.jar里面我有:

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

P001_WAR.war里面我有:

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

P001_EJBClient.jar里面我有:

  • 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

我运行了 WildFly。在 server.log 文件中,我看到 EJB 已成功部署:

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

但是我在这一行得到java.lang.ClassCastException:

ejbHomeObject = PortableRemoteObject.narrow(namedObject, test1SLBHomeClass);

这是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

我做错了什么才会收到此错误消息?

谢谢

更新

我已经找到解决办法了。当我更改此代码时:

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

这段代码:

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

然后就成功了。所以基本上我现在正在查找 JNDI Home 名称,然后将其转换为 Home Class。

在旧的 JBoss 4.2.X 中,我查找 JNDI 名称 Test1SLB ,然后将其转换为 Home Class 并且它起作用了。那么在旧的 JBoss 4.2.X 中是否有 1 个 JNDI 名称 Test1SLB 用于远程和主目录?

最佳答案

我已经找到解决办法了。当我更改此代码时:

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

这段代码:

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

然后就成功了。所以基本上我现在正在查找 Home,然后将其转换到 Home Class。

在旧的 JBoss 4.2.X 中,我查找 JNDI 名称 Test1SLB ,然后将其转换为 Home Class 并且它起作用了。因此,在旧的 JBoss 4.2.X 中,有 1 个 JNDI 名称 Test1SLB 用于远程和主目录。

关于java - 为什么在 Wildfly 8.2.1 中访问 EJB 2.1 时出现 ClassCastException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34162487/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com