The RMI (Remote Method Invocation) is an API that provides a mechanism to create distributed application in java. The RMI allows an object to invoke methods on an object running in another JVM.
The RMI provides remote communication between the applications using two objects stub and skeleton.
RMI uses stub and skeleton object for communication with the remote object.
A remote object is an object whose method can be invoked from another JVM. Let's understand the stub and skeleton objects:
The stub is an object, acts as a gateway for the client side. All the outgoing requests are routed through it. It resides at the client side and represents the remote object. When the caller invokes method on the stub object, it does the following tasks:
The skeleton is an object, acts as a gateway for the server side object. All the incoming requests are routed through it. When the skeleton receives the incoming request, it does the following tasks:
If any application performs these tasks, it can be distributed application.
The RMI application have all these features, so it is called the distributed application.
The is given the 6 steps to write the RMI program.
In this example, we have followed all the 6 steps to create and run the rmi application. The client application need only two files, remote interface and client application. In the rmi application, both client and server interacts with the remote interface. The client application invokes methods on the proxy object, RMI sends the request to the remote JVM. The return value is sent back to the proxy object and then to the client application.
For creating the remote interface, extend the Remote interface and declare the RemoteException with all the methods of the remote interface. Here, we are creating a remote interface that extends the Remote interface. There is only one method named add() and it declares RemoteException.
import java.rmi.*; public interface Adder extends Remote{ public int add(int x,int y)throws RemoteException; }
Now provide the implementation of the remote interface. For providing the implementation of the Remote interface, we need to
import java.rmi.*; import java.rmi.server.*; public class AdderRemote extends UnicastRemoteObject implements Adder{ AdderRemote()throws RemoteException{ super(); } public int add(int x,int y){return x+y;} }
Next step is to create stub and skeleton objects using the rmi compiler. The rmic tool invokes the RMI compiler and creates stub and skeleton objects.
rmic AdderRemote
Now start the registry service by using the rmiregistry tool. If you don't specify the port number, it uses a default port number. In this example, we are using the port number 5000.
rmiregistry 5000
Now rmi services need to be hosted in a server process. The Naming class provides methods to get and store the remote object. The Naming class provides 5 methods.
public static java.rmi.Remote lookup(java.lang.String) throws java.rmi.NotBoundException, java.net.MalformedURLException, java.rmi.RemoteException; | It returns the reference of the remote object. |
public static void bind(java.lang.String, java.rmi.Remote) throws java.rmi.AlreadyBoundException, java.net.MalformedURLException, java.rmi.RemoteException; | It binds the remote object with the given name. |
public static void unbind(java.lang.String) throws java.rmi.RemoteException, java.rmi.NotBoundException, java.net.MalformedURLException; | It destroys the remote object which is bound with the given name. |
public static void rebind(java.lang.String, java.rmi.Remote) throws java.rmi.RemoteException, java.net.MalformedURLException; | It binds the remote object to the new name. |
public static java.lang.String[] list(java.lang.String) throws java.rmi.RemoteException, java.net.MalformedURLException; | It returns an array of the names of the remote objects bound in the registry. |
In this example, we are binding the remote object by the name sonoo.
import java.rmi.*; import java.rmi.registry.*; public class MyServer{ public static void main(String args[]){ try{ Adder stub=new AdderRemote(); Naming.rebind("rmi://localhost:5000/sonoo",stub); }catch(Exception e){System.out.println(e);} } }
At the client we are getting the stub object by the lookup() method of the Naming class and invoking the method on this object. In this example, we are running the server and client applications, in the same machine so we are using localhost. If you want to access the remote object from another machine, change the localhost to the host name (or IP address) where the remote object is located.
import java.rmi.*; public class MyClient{ public static void main(String args[]){ try{ Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo"); System.out.println(stub.add(34,4)); }catch(Exception e){} } }
For running this rmi example, 1) compile all the java files javac *.java 2)create stub and skeleton object by rmic tool rmic AdderRemote 3)start rmi registry in one command prompt rmiregistry 5000 4)start the server in another command prompt java MyServer 5)start the client application in another command prompt java MyClient
Consider a scenario, there are two applications running in different machines. Let's say MachineA and MachineB, machineA is located in United States and MachineB in India. MachineB want to get list of all the customers of MachineA application.
Let's develop the RMI application by following the steps.
First of all, we need to create the table in the database. Here, we are using Oracle10 database.
package com.rookienerd; public class Customer implements java.io.Serializable{ private int acc_no; private String firstname,lastname,email; private float amount; //getters and setters }
package com.rookienerd; import java.rmi.*; import java.util.*; interface Bank extends Remote{ public List<Customer> getCustomers()throws RemoteException; }
package com.rookienerd; import java.rmi.*; import java.rmi.server.*; import java.sql.*; import java.util.*; class BankImpl extends UnicastRemoteObject implements Bank{ BankImpl()throws RemoteException{} public List<Customer> getCustomers(){ List<Customer> list=new ArrayList(); try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle"); PreparedStatement ps=con.prepareStatement("select * from customer400"); ResultSet rs=ps.executeQuery(); while(rs.next()){ Customer c=new Customer(); c.setAcc_no(rs.getInt(1)); c.setFirstname(rs.getString(2)); c.setLastname(rs.getString(3)); c.setEmail(rs.getString(4)); c.setAmount(rs.getFloat(5)); list.add(c); } con.close(); }catch(Exception e){System.out.println(e);} return list; }//end of getCustomers() }
package com.rookienerd; import java.rmi.*; public class MyServer{ public static void main(String args[])throws Exception{ Remote r=new BankImpl(); Naming.rebind("rmi://localhost:6666/rookienerd",r); }}
package com.rookienerd; import java.util.*; import java.rmi.*; public class MyClient{ public static void main(String args[])throws Exception{ Bank b=(Bank)Naming.lookup("rmi://localhost:6666/rookienerd"); List<Customer> list=b.getCustomers(); for(Customer c:list){ System.out.println(c.getAcc_no()+" "+c.getFirstname()+" "+c.getLastname() +" "+c.getEmail()+" "+c.getAmount()); } }}