Java Reference
In-Depth Information
Example 16−1: Bank.java (continued)
bank.getTransactionHistory(args[1], args[2]);
for(int i = 0; i < transactions.size(); i++)
System.out.println(transactions.get(i));
}
else System.out.println("Unknown command");
}
// Catch and display RMI exceptions
catch (RemoteException e) { System.err.println(e); }
// Catch and display Banking related exceptions
catch (BankingException e) { System.err.println(e.getMessage()); }
// Other exceptions are probably user syntax errors, so show usage.
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java [-Dbank=<url>] Bank$Client " +
"<cmd> <name> <password> [<amount>]");
System.err.println("where cmd is: open, close, deposit, " +
"withdraw, balance, history");
}
}
}
}
A Bank Server
Example 16-1 defined a RemoteBank interface and a bank client program. Example
16-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 Remote-
BankServer 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 regis-
ter 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 implemen-
tations 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 invo-
cations because multiple clients can be using the object at the same time. Remote-
BankServer uses synchronized methods and synchronized statements to prevent
two clients from opening, closing, or modifying the same account at the same
time.
Before you can run this RemoteBankServer program, you must compile it, generate
stub and skeleton classes, and start the rmir egistry service (if it is not already run-
ning). You might do all this with commands like the following (on a Unix system).
Note the -d argument to rmic : it tells the RMI compiler where to put the stub and
skeleton classes. Assuming the RemoteBankServer.class file is in the current direc-
tory, the usage shown here puts the generated classes in the same directory.
Search WWH ::




Custom Search