Logo F2FInterview

RMI Interview Questions

Q   |   QA

RMI handles the remote objects that are being provided between the server and client computers. The difference between the objects are handled using RMI as well. RMI uses the standard method invocation to communicate between one object or another object. It loads the class byte-codes for objects that are passed using the defined parameters and return the values to the server in case of failure or success of it. RMI also allows the transmission to be done when passing the data as well. The string that is used by the RMI registry is “rmi://hostname:port/remoteObjectName", this allows the RMI registry to know the correct object and create a connection between different objects through this. If the hostname is not defined then local host can be used instead of it and this will also establish the connection.

RMI distributed applications uses certain steps that defines the path mentioned in the registry. It has the following steps:

  • It uses the registry that stores the location and important information about the remote object. It uses the registry to obtain the references to a remote object that keeps all the information and allow further communication to be done.
  • The server calls the registry to associate the name with the remote object and then the client sends the request for the object.
  • The client searches the remote object by its name in the registry on the server and invokes a method on it.
  • RMI uses a web server to load the bytecodes of classes to use the communication for the client and server. RMI can load the class using the Internet protocols like HTTP, FTP, file, and etc. 

RMI uses a remote interface declaring the methods that are invoked from a virtual machine and further forwarded to the server to take any request coming from the client. Java.rmi.Remote provides an interface declaring a set of methods that invokes the virtual machine to fulfill the requirements:

  • This allows the extension of the interface directly or indirectly.
  • Each method declaration in remote interface must have the exception policy such as java.rmi.RemoteException or java.io.IOException or java.lang.Exception clause that can be made application specific and it reduces the effort to extend java.rmi.RemoteException.
  • Remote method declares the object as a parameter or the return value and that has to be declared using the remote interface. 

Remote interface allows the extension of the interface of java.rmi.Remtoe. This interface extends it non-remotely using the condition that this extend the interface when all the methods used are also extended in the interface that satisfy all the requirements of a remote method declaration. The example code shows the remote interface that is used in general:

public interface Bank extends java.rmi.Remote {
public void deposit(float amount)
throws java.rmi.RemoteException;
public void withdraw(float amount)
throws OverdrawnException, java.rmi.RemoteException;
public float getBalance()
throws java.rmi.RemoteException;
}

The class of this extends the java.rmi.Remote to inherit the behavior and properties used by this specified class. This also uses several other class objects for example:

java.rmi.server.RemoteObject and
java.rmi.server.RemoteServer.

To implement the distributed object model a class is required that implements a remote interface extending the functionality of java.rmi.Remote and other classes such as java.rmi.server.UnicastRemoteObject. The implementation then take the responsibility for exporting the object having the correct use of remote semantics provided using hashCode, equals, and toString methods that are inherited from the java.lang.Object class. The code is given below as:

public void withdraw(float amount) throws OverdrawnException,
RemoteException
{ // write your own statements }
public float getBalance() throws RemoteException
{// write your own statements } 

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: