Java Reference
In-Depth Information
also act as its own registry server by using the LocateRegistry class and Reg-
istry interface of the java.rmi.registry package.
After you compile the server program with javac , use rmic to generate a stub
and a skeleton for the remote object. With RMI, the client and server don't
communicate directly. On the client side, the client's reference to a remote
object is implemented as an instance of a stub class. When the client invokes
a remote method, it is a method of this stub object that is actually called. The
stub does the necessary networking to pass that invocation to a skeleton class
on the server. This skeleton translates the networked request into a method
invocation on the server object, and passes the return value back to the stub,
which passes it back to the client. This can be a complicated system, but for-
tunately, application programmers never have to think about stubs and skele-
tons; they are generated automatically by the rmic tool. Invoke rmic with the
name of the remote object class (not the interface) on the command line. It
creates and compiles two new classes with the suffixes _Stub and _Skel .
If the server uses the default registry service provided by the Naming class,
you must run the registry server, if it is not already running. You can run the
registry server by invoking the rmir egistry program.
Now you can write a client program to use the remote object exported by the
server. The client must first obtain a reference to the remote object by using
the Naming class to look up the object by name; the name is typically an rmi:
URL. The remote reference that is returned is an instance of the Remote inter-
face for the object (or more specifically, a stub object for the remote object).
Once the client has this remote object, it can invoke methods on it exactly as
it would invoke the methods of a local object. The only thing that it must be
aware of is that all remote methods can throw RemoteException objects, and
that in the presence of network errors, this can happen at unexpected times.
Finally, start up the server program, and run the client!
The following sections of this chapter provides two complete RMI examples that
follow the steps outlined here. The first example is a fairly simple remote banking
program, while the second example is a complex and lengthy multiuser domain
(MUD) system.
Remote Banking
Example 16-1 shows a class, Bank , that contains inner classes and interfaces for a
remote bank client/server example. In this example, the RemoteBank interface
defines remote methods to open and close accounts, deposit and withdraw
money, check the account balance, and obtain the transaction history for an
account. The Bank class contains all of the classes and interfaces required for the
example except for the server class; the class that actually implements the Remote-
Bank interface. This server class is shown in Example 16-2.
Example 16-1 defines the following inner classes and interfaces:
Search WWH ::




Custom Search