This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Naming.lookup arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Naming.lookup arguments" Watch "Naming.lookup arguments" New topic
Author

Naming.lookup arguments

Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
According to Java API:
The Naming class's methods take, as one of their arguments, a name that is a URL formatted java.lang.String of the form:
//host ort/name
where host is the host (remote or local) where the registry is located, port is the port number on which the registry accepts calls, and where name is a simple string uninterpreted by the registry. Both host and port are optional. If host is omitted, the host defaults to the local host. If port is omitted, then the port defaults to 1099, the "well-known" port that RMI's registry, rmiregistry, uses.
Binding a name for a remote object is associating or registering a name for a remote object that can be used at a later time to look up that remote object. A remote object can be associated with a name using the Naming class's bind or rebind methods.

I have a question here:
Considering the default port 1099:
1. If the lookup is in localhost, then the url would be:

where name is the server name binded to registry.
2. If the lookup is for the remote server, then the url would be:

where name is the server name binded to registry.
But can someone explain what should be in the url for remotehost is it the ip address?
thanks,
sri
p.s: This post is in continuation with this post:
http://www.coderanch.com/t/182606/java-developer-SCJD/certification/Rmi-lookup-Class-Cast-Exception
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

But can someone explain what should be in the url for remotehost is it the ip address?

That would be an IP address or simply the name of the server, such as "fatBrain", "deepBlue", etc. To see what your computer name is on Windows, right click My Computer|Properties|Network Identification (this may be different on Windows 95/98/2000/NT/XP)
Eugene.
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Originally posted by Eugene Kononov:

That would be an IP address or simply the name of the server, such as "fatBrain", "deepBlue", etc. Eugene.

Thanks Eugene!
If host is just the name of the server, which is bound to registry, then can you please explain what is Name here:

Naming.lookup("rmi://"+host+"Name");

So, does this mean that the following code is correct:

FactoryInterface fi =(FactoryInterface)Naming.lookup("rmi://serverName");

where serverName is an instance of ServerFactoryImplementation class,
bound to registry like this:

Naming.rebind(serverName, new FactoryImpl());

thanks,
sri
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

If host is just the name of the server, which is bound to registry, then can you please explain what is Name here:

No, the host is the name of the physical server, i.e, the name of the computer. As I recommended, look up the name of your computer so that you are not confused. What you bind to the registry is the remote object. For some reson, many people are confused with the concept of registry and binding to it. It couldn't be simpler, -- loosely speaking, the registry is just a block of memory where you make your objects available for everyone to see. The registry is organized as a map, where your key is the name of you object (any String, such as "my object"), and and the value is the actual object. Thus, to bind an object to registry, all you need to do is:
registry.bind("my object", myObject);
Notice that you don't need to specify the machine name or port, because the object is bound on this machine. Now, to retreve the object from another machine, you must specify the machine name (such as "fatBrain", or 170.3.133.169), the port (such as 1099), and the name to which the object is mapped ("my object"):
String service = "//" + "fatBrain" + ":" + 1099 + "/" + "my object";
myObject = (MyObjectInterface) Naming.lookup(service);
That's all there is to it, -- one line of code for bind(), two lines for lookup(). The client needs to know the server name to connect, of course.
Eugene.
[ April 01, 2003: Message edited by: Eugene Kononov ]
Sri Addanki
Ranch Hand

Joined: Apr 27, 2001
Posts: 195
Thanks Eugene, for that wonderful explanation!
Now, if i have just
Naming.lookup("rmi://"+remoteObject);
1. if i don't mention any hostname/ipaddress
2. and if its only the default port,
3. and i have the remote object bind to registry, then Naming.lookup will search in rmi://localhost, right?
If this is right, then in my code, i get classcast exception, why?
Can you please see my other post too, even Mark is trying to help me out.
thanks,
sri
 
GeeCON Prague 2014
 
subject: Naming.lookup arguments