A Bank Server
Example 21-1
defined a RemoteBank
interface and
a bank client program. Example
21-2 is a RemoteBankServer
class that implements the RemoteBank
interface and acts as a server
for the Bank.Client
program. This
class includes a main( )
method so
it can be run as a standalone program. This method creates a RemoteBankServer
object and registers it
with Naming.rebind( )
, so that
clients can look it up. It reads the system property bankname
to determine what name to use to
register the bank, but uses the name FirstRemote
by default. (This is the same
name that the Bank.Client
uses by
default as well.)
RemoteBankServer
implements the RemoteBank
interface, so it provides implementations for all remote methods
defined by that interface. It also defines some utility methods that
are not remote methods, but that are used by the remote methods. Note
that RemoteBankServer
includes an
inner Account
class that stores all
the information about a single bank account. It maintains a hashtable
that maps from account names to Account
objects. The various remote methods
look up the named account, verify the password, and operate on the
account in some way. Any RMI remote object must be able to handle
multiple, concurrent method invocations because multiple clients can
be using the object at the same time. RemoteBankServer
uses synchronized
methods and synchronized
statements to prevent two
clients from opening, closing, or modifying the same account at the
same time.
Before ...
Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.