jboss 5 ejb2.1 从 struts 操作类查找导致 ClassCastException

发布于 2024-12-01 06:32:54 字数 2025 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕花眠 2024-12-08 06:32:54

我认为 jndi 调用将返回 DemoEJB.DemoEJBHome 类,而不是 DemoEJB 类,因此会出现类转换异常。

尝试:

DemoEJBHome home =  (DemoEJBHome)ctx.lookup("ejb/DemoEJB");
DemoEJB demoEjb = home.create();

I think the jndi call would return the DemoEJB.DemoEJBHome class, not the DemoEJB one, hence the classcast exception.

Try:

DemoEJBHome home =  (DemoEJBHome)ctx.lookup("ejb/DemoEJB");
DemoEJB demoEjb = home.create();
小红帽 2024-12-08 06:32:54

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文