File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes jndi context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "jndi context" Watch "jndi context" New topic

jndi context

Sujit Nene

Joined: Apr 02, 2001
Posts: 6
While refering another bean, we have to lookup its remote interface in jndi context. Normally we use following construct
Context jndiContext = new InitialContext()
Object objRef = jndiContext.lookup("<bean-jndi-name>")
Here it is assumed that the referenced bean is deployed on the same server. But if we want to refer to a bean which is on a remote server, we have to provide the location of the that server while getting the jndi context. For this we have to hardcode the address of the other server in our bean. But this is a problematic situation. Can anybody suggest a better way to do this ? (for example using properties file)
-Sujit Nene
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
You would use a bean reference in the deployment descriptor. At deployment time you give the real JNDI location of the referenced bean. This gives you an additional layer of indirection: the other bean's location is specified at deployment time, you should not have to hardcode it in your EJB.
- Peter

[This message has been edited by Peter den Haan (edited April 30, 2001).]
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
Hi Peter,
Please explain how u will do this in detail.
Thanks in advance
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Cherry Mathew:
Please explain how u will do this in detail.

I can only explain in part. Setting up a bean reference in the EJB descriptor is easy.

(See section of the EJB spec v1.1). The link to the actual bean is made at deployment time, and that's where I cannot help you, as deployment varies from application server to application server. You are at the mercy of the server here - if two servers refuse to link up their JNDI trees at this stage then you may well be stuck.
- Peter

[This message has been edited by Peter den Haan (edited May 02, 2001).]
Vivek Viswanathan
Ranch Hand

Joined: Mar 03, 2001
Posts: 350
Since you are accessing the EJB deployed in another J2EE machine ( not the same machine as your client), you will have to specify this information when looking you the initial context.
To do that you have 2 choices
1) Use java.util.Properties object to set the properties of the initial Host for the JNDI ( the machine which has the EJB)
here 'buzz' is the name of the machine that has the EJB.
So your code will be something like this
Properties prop = new Properties();
// u will set the properties over here
Context initial = new InitialContext(prop);

2) Specify this property as a system property when you start your client application
you will do it something like this
java -Dorg.omg.CORBA.ORBInitialHost=buzz ClientApplicaton
Here again 'buzz' is the name of the machine that contains the EJB Object that your client is trying to access.
When you are using this method make sure that you use the default InitialContext constructor in your source code i.e Context initial = new InitialContext();

I have tried the 2nd method on a J2EE server and it work perfectly fine, and I have tried both the methods on a Weblogic5.1 Server and it works fine.
Assumptions made for the above 2 choices
1) The JNDI naming service is running on the same machine as the Machine that contains the
deployed EJB ( i.e buzz ) : In a real world you may have JNDI running on Machine A , EJB
deployed in Machine B , and Client in Machine C.In this case you will look up the initial
context on Machine A , which contains the information about where the EJB are deplyed, in
effect you as the client do not need to know on which machine the EJB is deployed -- this is
on the same lines as location Transperency in distributed applications.
2) The J2EE server uses the default port no: The J2EE server that contains your deplyed EJB
uses the default port number, if it were listening on a different port number then you can
sepciffy that in the Properties, in either of the above mentioned methods.
You can more information about this stuff in the J2EE docs
'Developer's Guide' Chapter 8 - Clients

Vivek Viswanathan
P.S I still have to learn to spell.

Vivek Viswanathan SCJP 1.2, SCJP 1.6,SCJD,SCEA,SCWCD,IBM-484,IBM-486,IBM-141,Ms.NET C# 70-316,SCMAD, LPIC-I
Gavin Wilkinson

Joined: Mar 06, 2001
Posts: 13
This solution does work a treat with refrence j2ee version 1.2, and i had been using it for a while.
However, try with 1.3 and it doesnt work.
Vivek Viswanathan
Ranch Hand

Joined: Mar 03, 2001
Posts: 350
Thanx , I too tried it with 1.2, I should b trying it with 1.3 as soon as possible
Vivek Viswanathan
Ranch Hand

Joined: Mar 03, 2001
Posts: 350
I have verified the program using J2ee 1.3 and it works perfectly fine.
I have used JDK 1.3 for the client , I faced a problem when using JDK 1.2
I can send you the code, along with the client , if u want??
I agree. Here's the link:
subject: jndi context
Similar Threads
what implements the Home and Remote interfaces?
Error:CalculatorBeanRemote not bound
calling EJB from servlet in visualage for java
Session bean accessing entity
HttpConnection VS web service