Java Reference
In-Depth Information
RemoteBank
The Remote interface implemented by the bank server and used by the bank
client.
FunnyMoney
A trivial class that represents money in this banking example. It is nothing
more than a wrapper around an int , but it serves to demonstrate that Serial-
izable objects can be passed as arguments to remote methods and returned
by remote methods.
BankingException
A simple exception subclass that represents banking-related exceptions, such
as “Insufficient funds.” It demonstrates that remote method implementations
on a server can throw exceptions that are transported across the network and
thrown in the client program.
Client
This class is a standalone program that serves as a simple client to the bank
server. It uses Naming.lookup() to look up the desired RemoteBank object in
the system registry and then invokes various methods of that RemoteBank
object, depending on its command-line arguments. It is really as simple as
that; the use of RMI is almost transparent.
A session using the Bank.Client class might look as follows (note that the com-
mand-line argument “david” is the account name and “javanut” is the password
that protects the account):
% java com.davidflanagan.examples.rmi.Bank\$Client open david javanut
Account opened.
% java com.davidflanagan.examples.rmi.Bank\$Client deposit david javanut 1000
Deposited 1000 wooden nickels.
% java com.davidflanagan.examples.rmi.Bank\$Client withdraw david javanut 100
Withdrew 100 wooden nickels.
% java com.davidflanagan.examples.rmi.Bank\$Client balance david javanut
You have 900 wooden nickels in the bank.
% java com.davidflanagan.examples.rmi.Bank\$Client history david javanut
Account opened at Wed Jul 12 15:30:12 PDT 2000
Deposited 1000 on Wed Jul 12 15:30:31 PDT 2000
Withdrew 100 on Wed Jul 12 15:30:39 PDT 2000
% java com.davidflanagan.examples.rmi.Bank\$Client close david javanut
900 wooden nickels returned to you.
Thanks for banking with us.
In this example session, the bank client is running on the same host as the server.
This need not be the case; the Client class looks for a system property named
bank to determine which bank server to connect to. So you could invoke the client
program like this (one long command line that has been broken into two lines):
% java -Dbank=rmi://bank.trustme.com/TrustyBank \
com.davidflanagan.examples.rmi.Bank\$Client open david javanut
Example 16−1: Bank.java
package com.davidflanagan.examples.rmi;
import java.rmi.*;
import java.util.List;
Search WWH ::




Custom Search