Vous êtes sur la page 1sur 13

CMIS 445-Fall 2006 Final Project Extension of Java RMI Bank Example By: xxxxx xxxxxx

Overview
The project is modeled on a hypothetical situation. A small town bank has five automated teller machines coupled with a main branch and two smaller branches located within supermarkets. Account information is maintained on a server located in the main branch. Using Java RMI banking tasks will be performed from workstations or automated teller machines

Problem Description
A distributed banking system consists of a server and some automated teller machines (ATM). The server manages all users account information. A customer can invoke the following operations at an ATM.

Make a deposit Make a withdrawal Check account balance

At a particular branch the same operations can be performed with the additional administrative requirements of maintaining and displaying account owner information.

Solution

For a simplistic demonstration of the operations of the various methods a beginning format provided will be expanded upon. Records for this example will be generated for validating performance but the true end state would be storing records in an Oracle database. Database implementation can be easily achieved using the JDBC ODBC driver provided with Java.

In order to support distributed objects in Java, a remote method invocation (RMI) system that is specifically tailored to operate in the Java environment will be used. Other RMI systems exist (such as CORBA) that can be adapted to handle Java objects, but these systems fall short of seamless integration due to their inter-operability requirement with other languages. Java languages RMI system assumes the homogeneous environment of the Java Virtual Machine, and the system can therefore follow the Java object model whenever possible.

In my model, a remote object is one whose methods can be accessed from another address space, potentially on a different machine. This is done by logging into two different sessions on NOVA. An object of this type is described by a remote interface, which is an interface that declares the methods of a remote object. RMI is the action of invoking a method on a remote object. Most importantly, a method invocation on a remote object has the same syntax as a method invocation on a local object. Clients of remote objects program to remote interfaces, not to the implementation classes of those interfaces. Since the failure modes of accessing remote objects are inherently different

than the failure semantics of local objects, clients must deal with an additional exception that can occur during any remote method invocation.

Remote Interfaces In order to implement a remote object, one must first define a remote interface for that object. A remote interface must extend (either directly or indirectly) a distinguished interface called java.rmi.Remote. This interface is completely abstract and has no methods. interface Remote {} For example, the following code fragment defines a remote interface for a bank account that contains methods that deposit to the account, withdraw from the account, and get the account balance:

import java.rmi.Remote; import java.rmi.RemoteException;

public interface IAccount extends Remote {

public void deposit(int amount) throws RemoteException;

public void withdraw(int amount) throws RemoteException;

public int getBalance() throws RemoteException;

As shown above, each method declared in an interface for a remote object must include java.rmi.RemoteException in its throws clause. If RemoteException is thrown during a remote call, then some communication failure happened during the call. Remote objects have very different failure semantics than local objects.

Remote Method Invocation For a client to invoke a method on a remote object, that client must first obtain a reference to the object. A reference to a remote object is obtained in the usual manner: as a return value in a method call or as a parameter passed to a method. The RMI system provides a simple bootstrap name server from which to obtain remote objects on given hosts. Since remote methods include RemoteException in their signature, the caller must be prepared to handle those exceptions in addition to other application specific exceptions. So, for each of the calls (deposit, withdraw, and balance), the code needs to catch RemoteException.

Locating Remote Objects A simple bootstrap name server is provided for storing named references to remote objects. A remote object reference can be stored using the URL-based interface java.rmi.Naming. For a client to invoke a method on a remote object, that client must first obtain a reference to the object. A reference to a remote object is usually obtained as a return value in a method call. The RMI system provides a simple bootstrap name server from which to obtain remote objects on given hosts. The Naming interface provides Uniform Resource Locator (URL) based methods to lookup, bind, rebind, unbind and list

the name and object pairings maintained on a particular host and port. Here's an example of how to bind and lookup remote objects:
public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException { String url = "rmi://localhost/Account"; } IAccount account = (IAccount)Naming.lookup(url);

public class AccountServer { public static void main(String[] args) throws RemoteException, MalformedURLException { System.out.println("starting the server"); Account m = new Account(); Naming.rebind("Account", m); } }

Output of Program nova> dir accountrmi cmis445 nova> ant Buildfile: build.xml does not exist! Build failed nova> cd accountrmi nova> ant Buildfile: build.xml property.setup: clean.dirs: [delete] Deleting: /class/cm445a/07/accountrmi/accountrmi.jar build.java: build.all: [jar] Building jar: /class/cm445a/07/accountrmi/accountrmi.jar BUILD SUCCESSFUL Total time: 3 seconds nova> rmiregistry & [1] 15245 nova> ant startaccountserver

Buildfile: build.xml property.setup: startaccountserver: [java] starting the server [java] account was made [java] after name rebind. nova> dir accountrmi cmis445 nova> cd accountrmi nova> ant startaccountclient Buildfile: build.xml property.setup: startaccountclient: [java] Working directory ignored when same JVM is used. [java] Owner Name: Tom Jones [java] Social Security Num: 123-45-6789 [java] Address: 15 South St. Aiea, HI 96789 [java] Phone Number: 808-345-6789 [java] Email: cfsmith@earthlink.net [java] check cur balance: 0 [java] check cur balance: 100 [java] cur balance: 50 [java] saving cur balance: 0 [java] saving cur balance: 1000 [java] saving cur balance: 850 [java] All accounts: Current checking account balance is 50 and savings account balance is 850 [java] Information: Account owner information: [java] Tom Jones [java] 15 South St. Aiea, HI 96789 [java] 808-345-6789 [java] cfsmith@earthlink.net [java] 123-45-6789

BUILD SUCCESSFUL Total time: 4 seconds nova>

Appendix
Source code for AccountClient.java, Account.java, IAAccount.java, and AccountSever.java.
/** * File: Account.java * * Description: This file contains the implementation of the account object. * It extends the interface defined in IAccount. * * rmic is run against it to create the stubs. * * ie. rmic cmis445.account Account * * Author: Mike Tarquinio extended by xxxxx xxxxx * */ package cmis445.account; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; import cmis445.account.IAccount; public class Account extends UnicastRemoteObject implements IAccount { private private private private private private private private private int accountBalance = 0; int accountBalance1 = 0; String socialSecurityNumber = "123-45-6789"; String OwnerName = "Tom Jones"; String address = "15 South St. Aiea, HI 96789"; String phoneNumber = "808-345-6789"; String emailAddress = "cfsmith@earthlink.net"; String allbalance = ""; String ownerinfo = "";

protected Account() throws RemoteException { super(); } public void deposit(int amount) throws RemoteException { accountBalance += amount; } public void deposit1(int amount) throws RemoteException { accountBalance1 += amount; } public int getBalance() throws RemoteException { return accountBalance; } public int getBalance1() throws RemoteException {

return accountBalance1;

public String getSocialSecurityNumber() throws RemoteException { return socialSecurityNumber; } public String getName() throws RemoteException { return OwnerName; } public String getownerinfo() throws RemoteException { ownerinfo += "Account owner information: " + "\n" + getName() + "\n" + getAddress() + "\n" + getPhoneNumber() + "\n" + getEmailAddress() + "\n" + getSocialSecurityNumber() + "\n\n"; return ownerinfo; } public String getAddress() throws RemoteException { return address; } public String getPhoneNumber() throws RemoteException { return phoneNumber; } public String getEmailAddress() throws RemoteException { return emailAddress; } public void withdraw(int amount) throws RemoteException { accountBalance -= amount; } public void withdraw1(int amount) throws RemoteException { accountBalance1 -= amount; } public String getAllBalance() throws RemoteException { allbalance += "Current checking account balance is " + getBalance() + " and savings account balance is " + getBalance1() + "\n"; return allbalance; } }

/** * File: AccountClient.java * * Description: This file contain the account client. It goes to the registry * to get a reference to the account object * * Author: Mike Tarquinio extended by xxxxx xxxxx * */ package cmis445.account; import java.net.MalformedURLException; import java.rmi.Naming;

import java.rmi.NotBoundException; import java.rmi.RemoteException; public class AccountClient { public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException { String url = "rmi://localhost/Account"; IAccount account = (IAccount)Naming.lookup(url); System.out.println("Owner Name: " + account.getName()); System.out.println("Social Security Num: " + account.getSocialSecurityNumber()); System.out.println("Address: " + account.getAddress()); System.out.println("Phone Number: " + account.getPhoneNumber()); System.out.println("Email: " + account.getEmailAddress()); System.out.println("check cur balance: " + account.getBalance()); account.deposit(100); System.out.println("check cur balance: " + account.getBalance()); account.withdraw(50); System.out.println("cur balance: " + account.getBalance()); System.out.println("saving cur balance: " + account.getBalance1()); account.deposit1(1000); System.out.println("saving cur balance: " + account.getBalance1()); account.withdraw1(150); System.out.println("saving cur balance: " + account.getBalance1()); System.out.println("All accounts: " + account.getAllBalance()); System.out.println("Information: " + account.getownerinfo()); } }

/** * File: IAccount.java * * Description: This file defines the remote interface to the object that is being published * * Author: Mike Tarquinio extended by xxxxx xxxxx * */ package cmis445.account; import java.rmi.Remote; import java.rmi.RemoteException; public interface IAccount extends Remote { public String getSocialSecurityNumber() throws RemoteException; public String getName() throws RemoteException; public void deposit(int amount) throws RemoteException; public void withdraw(int amount) throws RemoteException; public int getBalance() throws RemoteException; public void deposit1(int amount) throws RemoteException; public void withdraw1(int amount) throws RemoteException; public int getBalance1() throws RemoteException; public String getAddress() throws RemoteException; public String getPhoneNumber() throws RemoteException; public String getEmailAddress() throws RemoteException; public String getAllBalance() throws RemoteException; public String getownerinfo() throws RemoteException; }

/** * File: AccountServer.java * * Description: This file contain the account server. It creates the account * object and binds it to the rmiregistry

* * Author: Mike Tarquinio * */ package cmis445.account; import java.net.MalformedURLException; import java.rmi.Naming; import java.rmi.RemoteException; import cmis445.account.Account; public class AccountServer { public static void main(String[] args) throws RemoteException, MalformedURLException { System.out.println("starting the server"); Account m = new Account(); System.out.println("account was made"); Naming.rebind("Account", m); System.out.println("after name rebind."); } }

Vous aimerez peut-être aussi