Java Reference
In-Depth Information
% javac RemoteBankServer.java
% rmic -d ../../../../ com.davidflanagan.examples.rmi.RemoteBankServer
% rmiregistry &
% java com.davidflanagan.examples.rmi.RemoteBankServer
FirstRemote is open and ready for customers.
Note that Example 16-2 contains a fatal flaw: if the bank server crashes, all bank
account data is lost, which is likely to result in angry customers! Chapter 17,
Database Access with SQL , includes another implementation of the RemoteBank
interface. This implementation uses a database to store account data in a more
persistent way.
Example 16−2: RemoteBankServer.java
package com.davidflanagan.examples.rmi;
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
import Bank.*;
/**
* This class implements the remote methods defined by the RemoteBank
* interface. It has a serious shortcoming, though: all account data is
* lost when the server goes down.
**/
public class RemoteBankServer extends UnicastRemoteObject implements RemoteBank
{
/**
* This nested class stores data for a single account with the bank
**/
class Account {
String password; // account password
int balance; // account balance
List transactions = new ArrayList(); // account transaction history
Account(String password) {
this.password = password;
transactions.add("Account opened at " + new Date());
}
}
/**
* This hashtable stores all open accounts and maps from account name
* to Account object
**/
Map accounts = new HashMap();
/**
* This constructor doesn't do anything, but because the superclass
* constructor throws an exception, the exception must be declared here
**/
public RemoteBankServer() throws RemoteException { super(); }
/**
* Open a bank account with the specified name and password
* This method is synchronized to make it thread safe, since it
* manipulates the accounts hashtable.
**/
public synchronized void openAccount(String name, String password)
throws RemoteException, BankingException
{
Search WWH ::




Custom Search