jboss 5 ejb2.1 从 struts 操作类查找导致 ClassCastException
我在 JBOSS 5 服务器中部署了 ejb 2.1 。 jndi 名称是 ejb/DemoEJB
,它与 jboss 中的 jmx-console 树
匹配。当我尝试按如下方式在 struts 操作类中查找时,它会抛出 ClassCastException
。如果有任何不同的查找方式,请告诉我。 ejb jar是单独部署的,并且不在ear文件中。
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, url);
InitialContext ctx = new InitialContext(env);
DemoEJB demoEjb = (DemoEJB)ctx.lookup("ejb/DemoEJB");
ejb-jar.xml
<?xml version = '1.0' encoding = 'windows-1252'?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<enterprise-beans>
<session>
<description>Session Bean ( Stateless )</description>
<display-name>DemoEJB</display-name>
<ejb-name>DemoEJB</ejb-name>
<home>DemoEJB.DemoEJBHome</home>
<remote>DemoEJB.DemoEJB</remote>
<ejb-class>DemoEJB.impl.DemoEJBBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
jboss.xml
<?xml version = '1.0' encoding = 'UTF-8'?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 5.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_5_0.dtd">
<jboss>
<session>
<ejb-name >DemoEJB</ejb-name>
<jndi-name>ejb/DemoEJB</jndi-name>
<local-jndi-name >ejb/DemoEJB</local-jndi-name>
<ejb-ref>DemoEJB.DemoEJB </ejb-ref>
<ejb-local-ref>DemoEJB.DemoEJBHome</ejb-local-ref>
</session>
</jboss>
堆栈跟踪
I have ejb 2.1 deployed in JBOSS 5 server . The jndi name is ejb/DemoEJB
and it matches the jmx-console tree
in jboss . While i try to lookup in struts action class as follows , it throws the ClassCastException
. kindly let me know, if there is any different way to lookup . The ejb jar is separately deployed and it is not in the ear file.
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, url);
InitialContext ctx = new InitialContext(env);
DemoEJB demoEjb = (DemoEJB)ctx.lookup("ejb/DemoEJB");
ejb-jar.xml
<?xml version = '1.0' encoding = 'windows-1252'?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
<enterprise-beans>
<session>
<description>Session Bean ( Stateless )</description>
<display-name>DemoEJB</display-name>
<ejb-name>DemoEJB</ejb-name>
<home>DemoEJB.DemoEJBHome</home>
<remote>DemoEJB.DemoEJB</remote>
<ejb-class>DemoEJB.impl.DemoEJBBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
jboss.xml
<?xml version = '1.0' encoding = 'UTF-8'?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 5.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_5_0.dtd">
<jboss>
<session>
<ejb-name >DemoEJB</ejb-name>
<jndi-name>ejb/DemoEJB</jndi-name>
<local-jndi-name >ejb/DemoEJB</local-jndi-name>
<ejb-ref>DemoEJB.DemoEJB </ejb-ref>
<ejb-local-ref>DemoEJB.DemoEJBHome</ejb-local-ref>
</session>
</jboss>
Stack trace
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 jndi 调用将返回 DemoEJB.DemoEJBHome 类,而不是 DemoEJB 类,因此会出现类转换异常。
尝试:
I think the jndi call would return the DemoEJB.DemoEJBHome class, not the DemoEJB one, hence the classcast exception.
Try:
jboss 启动后(我假设是正确的),尝试查看您的 EJB 是否已成功部署,如果是,则它位于 JNDI 树中的确切位置。我会从 jmx 控制台检查 jndi-view (至少在 jboss 4.x 中是这样)。这将使您了解在 jndi 代码中查找什么内容。
当然,这也可能是因为您的客户端代码(部署了 struts 的地方)缺少一些 jar。所以错误发生在查找本身期间。在这种情况下,就像 UnitTest 博士所说的那样,堆栈跟踪将受到高度赞赏:)祝你好运!
after the jboss starts (I assume correctly), try to see whether your EJB is deployed successfully and if yes, where exactly in JNDI tree it resides. I would check a jndi-view from your jmx console (at least it was so in jboss 4.x). This will give you an idea of what to look up in your jndi code.
Of course this can be also a lack of some jar in your client code (where the struts is deployed). So the error occurs during the lookup itself. In this case, like Dr. UnitTest said, the stacktrace will be highly appreciated :) Good luck!