160 likes | 380 Views
Java RMI. Distributed Systems IT332. Outline. Introduction to RMI RMI Architecture RMI Program Example. method call. AnotherClass. SomeClass. returned object. computer 1. computer 2. RMI. Consider the following program organization: RMI is one technology that makes this possible
E N D
Java RMI Distributed Systems IT332
Outline • Introduction to RMI • RMI Architecture • RMI Program Example
method call AnotherClass SomeClass returned object computer 1 computer 2 RMI • Consider the following program organization: • RMI is one technology that makes this possible • Allows distributed java objects across the network
RMI and other technologies • CORBA (Common Object Request Broker Architecture) • CORBA supports object transmission between virtually any languages • Objects have to be described in IDL (Interface Definition Language) • CORBA is complex and flaky • RMI is purely Java-specific • Java to Java communications only • As a result, RMI is much simpler than CORBA
RMI Components • The RMI application contains the THREE components: • RMI Server: contains objects whose methods are to be called remotely. • RMI Client: gets the reference of one or more remote objects from Registry with the help of object • RMI Registry: is a naming service. • Servers use registry to bind the remote java object with their names. • Clients executing on local or remote machines retrieve the remote objects by their name registered.
RMI Architecture lookup rmi registry bind Client Program Server Program RMI Machine Boundary
Steps for Developing an RMI System 1. Define the interface 2. Develop the server program 3. Develop the client program
Step 1: Defining the Interface • To create an RMI application, first define a remote interface between the client and server objects. • A remote interface must satisfy the following requirements: • Must extend the interface java.rmi.Remote. • Each method declaration in a remote interface must include the exception java.rmi.RemoteException (or one of its superclasses such as java.io.IOException or java.lang.Exception) in its throws clause. public interface Remote a remote interface is an interface that declares a set of methods that may be invoked from a remote Java virtual machine. Only those methods specified in a "remote interface” are available remotely.
Interface example import java.rmi.Remote; import java.rmi.RemoteException; public interface BankAccount extends Remote { public void deposit(float amount) throws RemoteException; public void withdraw(float amount) throws RemoteException; public float getBalance() throws RemoteException; }
Step 2: Develop the Server program • The server program defines the abstract methods in the remote interface and must satisfy the following requirements: • The class should implements the remote interface • The class can implement any number of remote interfaces. • The class usually extendsUnicastRemoteObject, • The class can extend another remote implementation class. • The class can define methods that do not appear in the remote interface, but those methods can only be used locally and are not available remotely. • The class should locate the rmiregistryand specify the port number. • The class should bind the remote java object with their names to the rmiregistry
Server program example import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class BankAccountServerextendsUnicastRemoteObject implementsBankAccount { public BankAccountServer (float initialBalance) throws RemoteException { balance = initialBalance; } public void deposit(float amount) throws RemoteException { ... } …….. }
Server program example public static void main(String args []) { try { Registry reg= LocateRegistry.createRegistry(1234); reg.rebind("server", new BankAccountServer ()); System.out.println("Server started"); }catch (Exception e) { System.out.print(e.toString()); } }
Step 3: Develop the Client program • The Client program invokes the methods implemented in the server and must satisfy the following requirements: • Locate the rmiregistry using the server IP address and same port number. • Look up the name of the server in the registry using the name registered. • Start calling the methods.
Client example public static void main(String args[]) { try{ Registry reg= LocateRegistry.getRegistry("127.0.0.1", 1234); RMI rmi= (RMI) reg.lookup("server"); System.out.println("Connected to RMI server"); rmi.deposit(1000); } catch(Exception e) { System.out.printf(e.toString()); } }