eclipse 类在 ejb 中的窄 jndi reffrence 中强制转换异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6816996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:42:05  来源:igfitidea点击:

class cast exception in narrow a jndi reffrence in ejb

javaeclipseejbjndinarrowing

提问by sara

I am trying to write a simple stateless sesssion bean but I have problem with narrow reference I give in lookup time. I got

我正在尝试编写一个简单的无状态会话 bean,但是我在查找时间给出的窄引用方面存在问题。我有

class cast exeption

班级演员例外

I use

我用

eclipse IDE

日食IDE

my bean class

我的豆类

package codes;
import java.rmi.RemoteException;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;


public class SinaBean implements SessionBean {


    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public String getHello()
    {
        return "hello";
    }
    public void ejbCreate(){

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

    }

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

    }

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

    }

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

    }

}

my home interface

我的家庭界面

package codes;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface SinaHome extends EJBHome {

    public SinaObject create() throws RemoteException,CreateException;
}

my component

我的组件

package codes;

import java.rmi.RemoteException;

import javax.ejb.EJBObject;

public interface SinaObject extends EJBObject {

    String getHello() throws RemoteException;
}

my client

我的客户

package codes;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

public class Client {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Context con=null;
        try {   
            Properties p=new Properties();
            p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
            "org.jnp.interfaces.NamingContextFactory");
            p.setProperty(Context.PROVIDER_URL, "localhost:1099");
            p.setProperty(Context.URL_PKG_PREFIXES,
            "org.jboss.namingrg.jnp.interfaces");
            con = new InitialContext(p);
            Object o=con.lookup("SinaBean");
                       System.out.println(o);/***/untill know it works.it sysout : 
                       //org.jnp.interfaces.NamingContext@e83912*** 

            @SuppressWarnings("unused")
            SinaHome sh=(SinaHome)PortableRemoteObject.narrow(o, SinaHome.class);//***exeption is here!!***
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

my xml file

我的 xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
  <display-name>Ejb </display-name> 
  <enterprise-beans>

  <session>

  <display-name>SinaBean</display-name>
  <ejb-name>SinaBean</ejb-name>
  <home>codes.SinaHome</home>
  <remote>codes.SinaObject</remote>
  <ejb-class>codes.SinaBean</ejb-class> 
  <session-type>Stateless</session-type>
  <transaction-type>Bean</transaction-type> 

  <security-identity>
  <description></description>
  <use-caller-identity></use-caller-identity>
  </security-identity> 

  </session>

  </enterprise-beans>

 </ejb-jar>

the exception I receive

我收到的例外

java.lang.ClassCastException
    at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229)
    at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
    at codes.Client.main(Client.java:27)
Caused by: java.lang.ClassCastException: org.jnp.interfaces.NamingContext cannot be cast to org.omg.CORBA.Object
    at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:212)
    ... 2 more

I will be extremely grateful for your advices.

我将非常感谢您的建议。

采纳答案by Arjan Tijms

First of all, your xml file indicates you are using EJB3 (which is actually the only version even worth considering), but your bean is an EJB2 bean.

首先,您的 xml 文件表明您使用的是 EJB3(这实际上是唯一值得考虑的版本),但您的 bean 是 EJB2 bean。

If your server indeed runs EJB3, remove the XML file and remove the needless EJB home and component implementations. Add the @Statelessannotation and a remote interface. Make sure it looks something like this:

如果您的服务器确实运行 EJB3,请删除 XML 文件并删除不必要的 EJB 主目录和组件实现。添加@Stateless注释和远程接口。确保它看起来像这样:

@Remote
public interface SinaRemote {
    String getHello();
}

@Stateless
public class SinaBean implements SinaRemote {

    public String getHello() {
        return "hello";
    }
}

Then correct your JNDI properties. The following is wrong:

然后更正您的 JNDI 属性。以下是错误的:

p.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.namingrg.jnp.interfaces");

p.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.namingrg.jnp.interfaces");

it should be:

它应该是:

p.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

p.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

(the variants people use here are almost endless and it basically always works anyway, but just to be sure use the correct variant)

(人们在这里使用的变体几乎是无穷无尽的,无论如何它基本上总是有效,但只是为了确保使用正确的变体)

Furthermore, and most likely the main culprit, you're using the ejb-nameas-if it was a global JNDI name but this is not the case. The ejb-nameis a somewhat confusing logical name that's used as an extra kind of qualifier when referring to a specific bean when injecting or creating an ejb-ref.

此外,很可能是罪魁祸首,您使用的ejb-name好像是全局 JNDI 名称,但事实并非如此。这ejb-name是一个有点令人困惑的逻辑名称,在注入或创建 ejb-ref 时引用特定 bean 时,它用作一种额外的限定符。

In your case you're setting it to the same as the unqualified bean name, which is already the default. Since SinaBeanexists, I'm guessing you're not deploying via an EAR, but deploy a standalone EJB jar, right?

在您的情况下,您将其设置为与非限定 bean 名称相同的名称,这已经是默认值。既然SinaBean存在,我猜你不是通过 EAR 部署,而是部署一个独立的 EJB jar,对吧?

If you are using JBoss AS 6 (EJB3.1) you can use the global portable JNDI names, otherwise the old JBoss specific JNDI naming can be used: [ear name]/[simple bean name]/remote. Note that [simple bean name] is not the ejb-name, but again in your case they happened to be the same. Since [ear name]/is removed when you're deploying only an EJB jar, this explains the exception you're getting: SinaBeanis a directory (contextin JNDI terms), which can contain the actual entries local, remoteand no-interface. What you're doing is trying to cast this intermediate directory node to the proxy to your bean, which of course doesn't work.

如果您使用的是 JBoss AS 6 (EJB3.1),您可以使用全局可移植 JNDI 名称,否则可以使用旧的 JBoss 特定 JNDI 名称:[ear name]/[simple bean name]/remote. 请注意, [simple bean name] 不是 ejb 名称,但在您的情况下,它们碰巧是相同的。由于[ear name]/在您仅部署 EJB jar 时被删除,这解释了您得到的异常:SinaBean是一个目录(JNDI 术语中的上下文),它可以包含实际条目local,remoteno-interface. 您正在做的是尝试将此中间目录节点强制转换为您的 bean 的代理,这当然不起作用。

Anyway, finally, remove the usage of PortableRemoteObject, this is no longer needed. The client code will then look like this:

无论如何,最后,删除 的用法PortableRemoteObject不再需要。客户端代码将如下所示:

Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
properties.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
properties.setProperty(Context.PROVIDER_URL, "localhost:1099");
context = new InitialContext(properties);
SinaRemote sina = (SinaRemote) context.lookup("SinaBean/remote");

回答by Amit Thakur

Here If you are using EJB 2.0 along with Jboss 5.0 and trying to type cast Object obj to your Home Object from Client as below then it might throw java.lang.ClassCastException com.sun.proxy.$Proxy104 cannot be cast to org.omg.CORBA.Object.

这里如果您使用 EJB 2.0 和 Jboss 5.0 并尝试将 Object obj 从客户端键入到您的 Home Object 如下,那么它可能会抛出 java.lang.ClassCastException com.sun.proxy.$Proxy104 cannot be cast to org.omg .CORBA.Object

Code to lookup JNDI : java.lang.Object obj = ctx.lookup("java:comp/env/ATSessionBean"); ATHome home = (ATHome) PortableRemoteObject.narrow(obj, ATHome.class);

查找 JNDI 的代码: java.lang.Object obj = ctx.lookup("java:comp/env/ATSessionBean"); ATHome home = (ATHome) PortableRemoteObject.narrow(obj, ATHome.class);

Solution : Here u need to add additional jars in classpath of Client app those are available in Higher version of JBOSS. So better to download latest version **jboss-7.1.1.Finalserver and deploy your application and it will run successfully. :)** download JBOSS server (Download EAP 6.4.0)from below link : http://jbossas.jboss.org/downloads/

解决方案:这里你需要在客户端应用程序的类路径中添加额外的 jars,这些 jars 在更高版本的 JBOSS 中可用。 所以最好下载最新版本 **jboss-7.1.1.Final服务器并部署您的应用程序,它会成功运行。:)**从以下链接下载 JBOSS 服务器(下载 EAP 6.4.0)http: //jbossas.jboss.org/downloads/