Class exception in narrow jndi reffrence in ejb

I am trying to write a simple, stagnant sesssion bean, but I have a problem with the narrow link that I give during the search. I got

casting class

I use

Eclipse 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 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 that I get

 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 would be very grateful for your advice.

+7
source share
2 answers

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

If your server really runs EJB3, delete the XML file and remove the unnecessary home and component EJB implementations. Add the @Stateless and remote interface. Make sure it looks something like this:

 @Remote public interface SinaRemote { String getHello(); } @Stateless public class SinaBean implements SinaRemote { public String getHello() { return "hello"; } } 

Then fix your JNDI properties. The following is not true:

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

(the options used here are almost endless, and in principle they always work, but just to use the correct option)

In addition, and most likely the main culprit, you are using ejb-name as - if this is the global JNDI name, but it is not. ejb-name is a somewhat confusing logical name that is used as an additional kind of qualifier when referring to a specific bean when injecting or creating ejb-ref.

In your case, you install it in the same way as the unqualified name bean, which is already the default value. Since there is SinaBean , I assume that you are not deploying through the EAR, but are deploying an autonomous EJB, right?

If you use JBoss AS 6 (EJB3.1), you can use the global JNDI portable names, otherwise the old JBDI JBoss name can be used: [ear name]/[simple bean name]/remote . Note that [simple bean name] is not an ejb name, but again in your case they turned out to be the same. Since [ear name]/ is deleted when you deploy only the EJB, this explains the exception you get: SinaBean is a directory (context in JNDI terms) that can contain the actual local , remote and no-interface entries. What you are doing is trying to pass this intermediate node directory to the proxy server on your bean, which of course does not work.

In the end, remove the use of PortableRemoteObject , this is no longer required . Client code will look like this:

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

Here If you use EJB 2.0 with Jboss 5.0 and try to inject an Object for your home object from the client, as shown below, it may throw java.lang.ClassCastException com.sun.proxy. $ Proxy104 cannot be thrown at org.omg.CORBA.Object .

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

Solution: Here you need to add additional banks to the classpath of the Client application, which are available in the higher version of JBOSS. So it’s better to download the latest version ** jboss-7.1.1.Final server and deploy the application, and it will work successfully. :) ** download the JBOSS server (Download EAP 6.4.0) from the link below: http: // jbossas. jboss.org/downloads/

0
source

All Articles