R4R
Right Place For Right Person TM
 
R4R Java RMI

R4R
provide basic RMI Tutorials concept with RMI Examples . Through R4R you can develop RMI programming concept. R4R provide RMI Interview Questions with answers.R4R provide RMI Languages study materials in easy way.

RMI Tutorials

1.1    RMI Basic Tutorials

RMI Interview Questions with Answers

2.1    RMI Interview Questions And Answers
2.2    RMI Objective Questions And Answers
2.3    RMI Interview Questions And Answers
2.4    RMI FAQS

REMOTE METHOD INVOCATION

RMI provides for remote communication between programs written in the Java programming language. Java Remote method invocation (RMI)application programming interface (API) enables client and server communications over the internet. Typically client program request to a server program and the server program responds to those requests. A common example  is sharing a word processing program over a network.The Java Remote Method Invocation (RMI) system allows an object running in one Java virtual machine to invoke methods on an object running in another Java virtual machine. It's like placing a class on Machine A and calling methods of that class from Machine B as though they were from the same machine. RMI provides the Way by  which the server and the client communicate with each other and pass information back and forth. This  type of  application is sometimes referred to as a distributed object application.

ADVANTAGES of RMI
1.Simple and clean to implement which  to more robust, maintainable and flexible applications
 2.Distributed systems creations are required  during  decoupling the client and server objects simultaneously
 3.It is possible to create zero-install client for the users.
 4.No client installation is needed except java capable browsers
 5.At the time of changing the database, the server objects are needed to be recompiled  but server interface and the client remain the same.     

ARCHITECTURE OF RMI 
RMI is based on client-server architecture model. The stub plays the role of a proxy server for the remote objects. The skeleton lives in the same JVM as the remote object and communication will be handled with the stub. The remote references are managed by the registry. The binding of server with reference to itself will be on the registry. The clients communicate with a registry which in turn obtains a remote reference to the server. This could be a remote host. The remote reference could be obtained by the client from the registry in order to invoke the methods from the remote object.

 HOW RMI WORKS
The RMI implementation is essentially built from three abstraction layers:
1.The Stub/Skeleton Layer:-Toget location transparency, RMI has  introduces two  kinds of objects known as stubs and skeletons
that serve as an interface between an application and rest of the RMI system. Stubs are present at  the client side, whereas skeletons are found on the server side .The purpose of this layer is to transfer the data to the Remote Reference layer. through marshaling and unmarshaling .marshaling is the process of converting data and object into byte stream while unmarshaling is reverse of it .umarshaling convert the stream byte data to the data and object. readable by the client.
2. The Remote Reference Layer:-The Remote Reference Layer The remote reference layer defines and supports the invocation semantics of the RMI connection. This layer maintains the session during the method call.
3.The Transport Layer:- Transport layer is used to make  the stream-based network connections over TCP/IP protocol  between the JVMs, and is responsible for setting and managing the connections. Even if two JVMs are running on the same physical computer, they connect through their host computers TCP/IP network protocol stack. RMI uses a protocol called JRMP (Java Remote Method Protocol) on top of TCP/IP (an analogy is HTTP over TCP/IP).
 

Sequence of events performed by the stub:
• Initially set  a connection with the remote JVM which is containing the remote object.
• Marshals the parameters to the remote JVM.i.e (writes and transmits)
• Waits for the result of the method invocation.
• Unmarshals the return value or exception returned.
• Return the value to the caller.

Sequence of events performed by the skeleton:
• Unmarshals  the parameters for the remote method the same  parameters those  were marshaled by the stub
on the client side
• Invokes those  method on the real remote object implementation.
• Marshals (writes and transmits) the result (return value or exception) to the caller (which is then
unmarshalled by the stub)

STEPS TO CREATE A RMI SYSTEM
1) Create an interface.
2) Create a class that implements the interface. .
3) Create a server that creates an instance of this class
4) Create a client that connects to the server object using Naming.lookup()
5) Compile these classes.
6) Run the RMI interface compiler on the .class file of the implementation
class ..
7) Start the RMI registry as "start rmiregistry".
8) Start the server class as"start java myRMIServer".
9) Run the client program as"java myRMIClient".
 

Create an  interface


public interface myRMIInterface extends java.rmi.Remote
{
        public java.util.Date getDate() throws java.rmi.RemoteException;
}

 
RMI implementation

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class myRMIImpl extends UnicastRemoteObject implements myRMIInterface
{  
                       
                   
                   
               
               
           
           
       
        public myRMIImpl(String name) throws RemoteException
                               
                           
                           
                       
                       
                   
                   
               
                {             
                               
                           
                           
                       
                       
                   
                   
               
                super();
                               
                           
                           
                       
                       
                   
                   
               
                try
                                   
                                   
                                   
                               
                               
                           
                           
                       
                        {
                                    
                                   
                                   
                               
                               
                           
                           
                       
                        Naming.rebind(name, this);
                                    }
                               
                           
                           
                       
                       
                   
                   
               
                catch(Exception e)
                                       
                                   
                                   
                               
                               
                           
                           
                       
                        {
                                       
                                   
                                   
                               
                               
                           
                           
                       
                        System.out.println("Exception occurred: " + e);
                                       
                                   
                                   
                               
                               
                           
                           
                       
                        }
                                
                           
                           
                       
                       
                   
                   
               
                }
                       
                   
                   
               
               
           
           
       
        public java.util.Date getDate()
                               
                           
                           
                       
                       
                   
                   
               
                {
                               
                           
                           
                       
                       
                   
                   
               
                return new java.util.Date();


RMI server side program


import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

public class myRMIServer
{
                       
                   
                   
               
               
           
           
       
        public static void main(String[] argv)
                              {
                              
                           
                           
                       
                       
                   
                   
               
                System.setSecurityManager(new RMISecurityManager());
                               
                           
                           
                       
                       
                   
                   
               
                try
                                       
                                   
                                   
                               
                               
                           
                           
                       
                        {
                                       
                                   
                                   
                               
                               
                           
                           
                       
                        myRMIImpl implementation = new myRMIImpl("myRMIImplInstance");
                                       
                                   
                                   
                               
                               
                           
                           
                       
                        }
                               
                           
                           
                       
                       
                   
                   
               
                catch (Exception e)
                                       
                                   
                                   
                               
                               
                           
                           
                       
                        {
                                       
                                   
                                   
                               
                               
                           
                           
                       
                        System.out.println("Exception occurred: " + e);
                                       
                                   
                                   
                               
                               
                           
                           
                       
                        }
                               
                           
                           
                       
                       
                   
                   
               
                }
}

RMI client side program


import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.util.Date;

public class myRMIClient
{
                  
                   
                   
               
               
           
           
       
        public static void main(String[] argv)
                                {
                            System.setSecurityManager(new RMISecurityManager());
                              
                           
                           
                       
                       
                   
                   
               
                if (argv.length != 1)
                                     {
                                      System.out.println("usage: java myRMIClient <IP address of host running RMI server>");
                                       
                                   
                                   
                               
                               
                           
                           
                       
                        System.exit(0);
                                       }
                                 String serverName = argv[0];
                              try
                                    {
                                         
                                                   
                                                   
                                               
                                               
                                           
                                           
                                       
                                        //bind server object to object in client
                                       
                                   
                                   
                               
                               
                           
                           
                       
                        myRMIInterface myServerObject = (myRMIInterface) Naming.lookup("rmi://"+serverName+"/myRMIImplInstance");
                                       

                                                   

                                                   

                                               

                                               

                                           

                                           

                                       

                                        //invoke method on server object
                                        Date d = myServerObject.getDate();
                                       
                                   
                                   
                               
                               
                           
                           
                       
                        System.out.println("Date on server is " + d);
                                       }
                                 
                           
                           
                       
                       
                   
                   
               
                catch(Exception e)
                                      {
                                      System.out.println("Exception occured: " + e);
                                      System.exit(0);
                                      }
                                System.out.println("RMI connection successful");
                             }
                         }


How compile and run the RMI

1. Start the rmiregistry
To start the registry, Windows users should do the following
start rmiregistry
To start the registry, Unix users should do the following:-
rmiregistry &
2.Compile the server
Compile the server, and use the rmic tool to create stub files.
3.Start the server

From the directory in which the classes are located, type the following:-
java myRMIServer
4.Start the client
We  can run the client on local machine , or from a different machine at difrent location. In either case, we'll need to specify the hostname of the machine wherewe are running the server. If we're running it locally, than we use localhost as the hostname.java myRMIClient localhost
Java RMI is a useful mechanism for invoking methods of remote objects. Java RMI allows one Java Virtual Machine to invoke methods of another, and to share any Java object type, even if client or server has never come across that object type before.
 

New Updates

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R