Java Reference
In-Depth Information
// Instantiate the remote object implementation class.
RMIExampleImpl impl = new RMIExampleImpl ();
// Define the " well-known " name to use in the
// registry.
String server - name =
"//localhost:3001/ rmi-example-server";
// Bind the implementation object under that name.
Naming.rebind (server - name, impl);
} // try
catch (Exception e) {System.err.println (e);}
} // main
} // class RMIExampleServer
Note that we first instantiate and obtain a reference to the RMIExampleImpl
object that provides the remote methods. Then we bind it into the registry under
the name //localhost:3001/rmi-example-server . The format of
the name is a URL formatted string with the URL protocol omitted but with the
two forward slashes ( // ) retained. (As discussed in Chapter 13, the URL format
is an industry standard and always uses forward slashes regardless of what the
underlying operating system may use as a file separator.) We use localhost
as the host since we bind into the RMI registry running on the same host as
RMIExampleServer .Infact, for security reasons, an application can bind or
unbind only to a registry running on the same host. This requirement prevents a
malicious program from removing or overwriting any of the entries in a server's
remote registry. A lookup, however, can be done from any host. The hostname is
followed by an optional colon and port number. If unspecified, the default port
number is the RMI standard port, 1099. The host and optional port are followed
byaslash and a completely arbitrary name for the server. The only requirement
is that the client must know and use the same name. There are two exceptions
that can occur - java.net.MalformedURLException if the name is in an
illegal format and RemoteException if the registry cannot be contacted for
some reason. For simplicity, we just catch the Exception superclass in the code
snippet above.
To start the server running, we simply execute RMIExampleServer . There
are Java security issues that we've glossed over so far that must be addressed
before we have an example that actually works. We discuss those issues after
discussing the client code.
18.4 The RMI client
The client's job is pretty simple. It must first look up the desired server name
in the registry and then make method calls to the remote methods. As explained
above, the only difference in a local call and a remote call on the client side is
 
Search WWH ::




Custom Search