The whole purpose of RMI is to simplify the use of remote objects as much as possible. The only additional thing we need to do in the client program is to find and retrieve the remote interface from the server. Since then, the rest of the thing is normal Java programming: sending messages to objects. Here are the programs that use Perfecttime:
: Displayperfecttime.java
//Uses remote Object Perfecttime
package c15.ptime;
Import java.rmi.*;
Import java.rmi.registry.*;
public class Displayperfecttime {public
static void Main (string[] args) {
System.setsecuritymanager (
new Rmisecuritymanager ());
try {
Perfecttimei t =
(Perfecttimei) naming.lookup (
"//colossus:2005/perfecttime");
for (int i = 0; i < i++)
System.out.println ("Perfect time =" +
t.getperfecttime ());
} catch (Exception e) {
e.printstacktrace ();}}}
///:~
The ID string is the same as the string that registers the object with the naming, and the first part indicates the URL and the port number. Because we are ready to use a URL, we can also specify a machine on the Internet.
The returned from Naming.lookup () must be styled to the remote interface, not to the class. If you swap a class, you get an offending prompt.
In the following method calls:
T.getperfecttime ()
We can see that once the handle of the remote object is obtained, programming with it is very similar to programming with local objects (there is only one difference: The remote method "throws" a remoteexception violation).