jQuery in Action, 2nd edition*
The moose likes EJB and other Java EE Technologies and the fly likes InitialContext.lookup and narrow malfunctioning Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "InitialContext.lookup and narrow malfunctioning" Watch "InitialContext.lookup and narrow malfunctioning" New topic
Author

InitialContext.lookup and narrow malfunctioning

Murad Iqbal
Ranch Hand

Joined: Dec 09, 2003
Posts: 90
Hi all,

I have successfully deployed a simple CMP entity bean on JBoss 3.2.3. I am attempting to access the ejb using the following client code. both the client and the jboss server that has the ejb container are on the same machine. However, I experienced a weird behavior from the lookup method. When i did Object obj = ctx.lookup("local/PersonnelBean"), it should return the LocalHome interface of the PersonnelBean which is PersonnelLocalHome (same name in ejb-jar.xml), but it returned me local/PersonnelBeanHome which doesnt exist at all. I got this by printint out the obj object. Thereafter, I changed my PersonnelLocalHome interface to PersonnelBeanHome just to let it go to the next step, but it is giving me the same NullPointer Exception. Kindly help. 'local/PersonnelBean' is the jndi name of the bean as told by JBoss. Following is the client code.


public static void main( String [] args )
{

Properties prop = new Properties();
prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
prop.setProperty(Context.PROVIDER_URL, "localhost:1099");
prop.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming rg.jnp.interfaces");

try
{
Context ctx = new InitialContext(prop);
System.out.println("TestEJB: 1 ");

Object obj = ctx.lookup( "local/PersonnelBean" );
System.out.println("Obj is: " + obj);
System.out.println("TestEJB: 2");

com.btm.ejb.personnel.PersonnelBeanHome home =
(com.btm.ejb.personnel.PersonnelBeanHome)javax.rmi.PortableRemoteObject.narrow(
obj, com.btm.ejb.personnel.PersonnelBeanHome.class );
System.out.println("TestEJB: home object created");
PersonnelLocal personnel = home.create("M001", "Murad", "Iqbal",
"4343", 1, "SE", "PK", "19121979");
System.out.println("TestEJB: object created");
//System.out.println( personnelLocal.hello());
//personnel.remove();
}
catch ( Exception e )
{
e.printStackTrace();
System.out.println( "Exception: " + e );
}






TestEJB: 1
Obj is: local/PersonnelBeanHome
TestEJB: 2
java.lang.NullPointerException
TestEJB: home object created
Exception: java.lang.NullPointerException
at org.jboss.ejb.plugins.local.LocalHomeProxy.invoke(LocalHomeProxy.java:110)
at $Proxy0.create(Unknown Source)
at com.test.TestEJB.main(TestEJB.java:45)
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1121

Murad,
It seems to me that you have misunderstood the purpose of the "local" EJB interfaces (as opposed to the "remote" interfaces). The "local" interface is _not_ for clients that are on the same machine as the EJB container. The local interface is for clients that are in the same JVM as the EJB container. Since I assume your client is not in the same JVM as the EJB container, it is a remote client, and must therefore use the EJB "remote" interface.

The local interface was introduced so that one type of EJB (for example a session bean) could lookup a different type of EJB (for example an entity bean) in the same EJB container.

I think you also need to define remote interfaces for your EJB's -- since it appears you have currently only defined local interfaces.

For your information, TheServerSide Web site has a freely available book on EJB -- which can be downloaded as a single, PDF file.

Good Luck,
Avi.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: InitialContext.lookup and narrow malfunctioning
 
Similar Threads
Kyle, Tony- -ServiceUnavailableException while looking context from Remote host
Local Interfaces in EJB
New to EJB, advice please
EJB access issue
JNDI Call