Java Reference
In-Depth Information
sequence to run an RMI application is usually:
1. Start the registry, specifying the desired port number.
2. Start the server, which registers itself in the registry.
3. Run the client, which finds the server by querying the registry.
Clients query the registry using a known sever name to obtain a reference to the
desired remote object. Of course, the remote objects must first be entered into
the registry, using that same known server name, before they can be found. This
binding into the registry is typically handled by the server code itself at server
startup time, and the server is expected to be running essentially all the time,
waiting on client connections. Alternatively, RMI supports an activation model
in which servers can be activated on demand instead of running all the time.
We describe only the standard persistent binding rather than activation. For more
information on RMI activation, consult the online Java documentation.
One way to handle binding is to add a main() method to the remote
method implementation class shown above. This main() would instantiate an
RMIExampleImpl object and then bind the object reference into the registry.
We use a slightly more general method and create another class altogether to
do the instantiation and binding. In this way, the implementation class remains
separate from the class that handles server instantiation and binding. We call this
special class RMIExampleServer . Its sole job is to create an instance of the
RMIExampleImpl implementation class and bind it into the RMI registry under
a known name so that the remote methods are accessible to clients. All the actual
remote methods reside in RMIExampleImpl while the action of starting up the
server occurs in the main() method of RMIExampleServer .
Assuming the registry is already running, the registry is accessed program-
matically through the java.rmi.Naming class, which has methods to bind,
unbind, rebind, list, and look up names. The bind() and rebind() methods
are similar except that bind() requires that no previous binding exist with the
specified name - it throws an AlreadyBoundException if the name is already
in use. The method rebind() , because it permits, but does not require, a preex-
isting name, is most commonly used - it replaces the object reference previously
bound to a name with the new object reference, if necessary. Both bind() and
rebind() take two parameters - the name under which to bind the object and a
reference to the java.rmi.Remote object to be bound. An example of the use
of rebind() is shown below:
import java.rmi.*;
public class RMIExampleServer
{
public static void main (String[] args) {
try {
 
Search WWH ::




Custom Search