我有
class cast exeption
我用
eclipse IDE
我的豆类
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
我将非常感谢您的建议.
解决方法
如果您的服务器确实运行EJB3,请删除XML文件并删除不必要的EJB主目录和组件实现.添加@Stateless注释和远程接口.确保它看起来像这样:
@Remote public interface SinaRemote { String getHello(); } @Stateless public class SinaBean implements SinaRemote { public String getHello() { return "hello"; } }
p.setProperty(Context.URL_PKG_PREFIXES,“org.jboss.namingrg.jnp.interfaces”);
它应该是:
p.setProperty(Context.URL_PKG_PREFIXES,“org.jboss.naming:org.jnp.interfaces”);
(人们在这里使用的变体几乎是无穷无尽的,无论如何它基本上总是有效,但只是为了确保使用正确的变体)
此外,并且很可能是主要的罪魁祸首,你使用的是ejb-name,如果它是一个全局JNDI名称,但实际情况并非如此. ejb-name是一个有点令人困惑的逻辑名,在注入或创建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-name,但在您的情况下,它们恰好相同.由于[ear name] /在您仅部署EJB jar时被删除,这解释了您获得的异常:SinaBean是一个目录(JNDI术语中的上下文),它可以包含本地,远程和没有的实际条目接口.你正在做的是尝试将这个中间目录节点转换为你的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");