jjzjj

java - classcastexception in narrow a jndi reference in ejb

coder 2024-03-19 原文

我正在尝试编写一个简单的无状态 session bean,但我在查找时间中给出的窄引用有问题。 我得到了

class cast exeption

我用

eclipse IDE

我的 bean 类

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

    }

}

我的主界面

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

我的组件

package codes;

import java.rmi.RemoteException;

import javax.ejb.EJBObject;

public interface SinaObject extends EJBObject {

    String getHello() throws RemoteException;
}

我的客户

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


    }

}

我的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>

我收到的异常

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

我将非常感谢您的建议。

最佳答案

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

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

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

@Stateless
public class SinaBean implements SinaRemote {

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

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

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

应该是:

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

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

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

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

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

无论如何,最后,删除 PortableRemoteObject 的用法,这是 no longer needed .客户端代码将如下所示:

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

关于java - classcastexception in narrow a jndi reference in ejb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6816996/

有关java - classcastexception in narrow a jndi reference in ejb的更多相关文章

随机推荐