Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

RMI - Remote Method Invocation in Java

In this article, we will understand about Remote Method Invocation in Java in which we will learn how we can call other methods or use them placed at different machines and JVM.

What is RMI?

Java RMI is a mechanism to allow the invocation of methods that reside on different Java Virtual Machines (JVMs). The JVMs may be on different machines or they could be on the similar machine. In either case, the method can run in a different address bar than the calling process. Java RMI is an object-oriented remote procedure call mechanism.

Distributed Object Application

An RMI application is often composed of two different programs, a server and a client. The server creates remotes objects and makes references to those objects available. Then it holds for clients to invoke methods on the objects.

The client results remote references to remote objects in the server and invokes methods on those remote objects.

The RMI model provides a distributed object application to the programmer .It is a mechanism that the server and the client use to communicate and pass information among one another. A distributed object application has to manage the following properties:

  • Locate remote objects: The system has to obtain references to remote objects. This can be done in two ways. Either by using RMI’s naming facility, the RMI registry, or by passing and returning remote objects.
  • Communicate with remote objects: The programmer doesn’t have to handle communication between the remote objects since this is handled by the RMI system. The remote communication shows like an ordinary method invocation for the programmer.
  • Load class bytecodes for objects that are transferred as arguments or results values.

All mechanisms for loading an object’s code and transmitting data is provided by the RMI system.40

Interfaces and Classes

Since Java RMI is an individual-language system, the programming of distributed application in RMI is rather simple. All interfaces and classes for the RMI system are defined in the java.RMI package the relationship between some of the interfaces and classes. The Remote Object class implements the Remote interface while the other classes extend RemoteObject.

The Remote Interface

A remote interface is defined by extending the Remote interface which is in the java.RMI package. The interface which declares methods that clients can invoke from a remote virtual machine is known as Remote Interface. The remote interface must satisfy the following conditions:

  • It must extend the interface Remote.
  • Every remote method declaration in the remote interface must include the exception RemoteException (or one of its superclasses) in its thrown clause.

The RemoteObject Class

RMI server functions are provided by the class RemoteObject and its subclasses Remote Server, Activatable and UnicastRemoteObject. Here is a short description of what the different classes handle:

  • RemoteObject provides implementations of the methods toString, equals and hash Code and in the java.lang.Object class.
  • The classes UnicastRemoteObject and Activatable create remote objects and export them, i.e. these classes make the remote objects used by remote clients.

The RemoteException Class

The class RemoteException is a super class of the exceptions that the RMI system throws during a remote method invocation. Every remote method that is declared in a remote interface must specify RemoteException (or one of its superclasses) in its throws clause to ensure the robustness of applications in the RMI system.

When a remote method invocation fails, the exception RemoteException is scatter. Communication failure, protocol errors and failure during marshalling or unmarshalling of parameters or return values are some reasons for RMI failure. Remote Exception is an exception that must be handled by the caller of the remote method; i.e.it is a kind of checked exception. The compiler confirms that the programmer have handled these exceptions.

Implementation of a simple RMI system

This is a simple RMI system with a client and a server. The server contains single method (Hello) that returns a string to the client.

To build the RMI system all files has to be compiled. Then the outline and stub, which are standard mechanisms communication with remote objects, are created with the RMIc compiler. [This RMI system contains the following files (the files are shown below):

  • Hello.java: The remote interface.
  • HelloClient.java: The client application in the RMI system.
  • HelloServer.java: The server application in the RMI system.

When all files are compiled, performing the following command will create the stud and the skeleton:

RMIc HelloServer

Then the two classes will be created, HelloServer_Stub.class and HelloServer_Skel.class, where the first class represents the client side of the RMI system and the second file represents the server side of the RMI system.

Listing 1: Hello.java

import java.RMI.Remote;
import java.RMI.RemoteException;
/*
Classname: Hello
Comment: The remote interface.
*/
public interface Hello extends Remote {
String Hello() throws RemoteException;
}

Listing 2: HelloClient.java

import java.RMI.Naming;
import java.RMI.RemoteException;
/*
Classname: HelloClient
Comment: The RMI client.
*/
public class HelloClient {
static String message = "blank";
// The Hello object "obj" is the identifier that is

// the Hello interface.
static Hello obj = null;
public static void main(String args[])
{
try {
obj = (Hello)Naming.lookup("//"
+ "kvist.cs.umu.se"
+ "/Hello");
message = obj.Hello();
System.out.println("Message from the RMI-server was: \""
+ message + "\"");
}
catch (Exception e) {
System.out.println("HelloClient exception: "
+ e.getMessage());
e.printStackTrace();
}
}
}

Listing 3: HelloServer.java

import java.RMI.Naming;
import java.RMI.RemoteException;
import java.RMI.RMISecurityManager;
import java.RMI.server.UnicastRemoteObject;
/*
Classname: HelloServerDemo
Purpose: The RMI server.
*/
public class HelloServerDemo extends UnicastRemoteObject
implements Hello {
public HelloServerDemo() throws RemoteException {
super();
}
public String Hello() {
System.out.println("Invocation to Hello was succesful!");
return "Hello World from RMI server!";
}
public static void main(String args[]) {
try {
// Creates an object of the HelloServer class.
HelloServer obj = new HelloServer();
// Bind this object instance to the name "HelloServer".
Naming.rebind("Hello", obj);
System.out.println("Hello bound in registry");
}
catch (Exception ex) {
System.out.println("error: " + ex.getMessage());
e.printStackTrace();
}
}

Output:

Message from the RMI-server was: Joe
Hello bound in registry

Listing 4: Program to implement Calculator using RMI - Calci.java

import java.RMI.Remote;
import java.RMI.RemoteException;
 
public interface Calci extends Remote
{
    public long add(long a, long b) throws RemoteException;
}

Listing 5: CalculatorImple.java

import java.RMI.RemoteException;
import java.RMI.server.UnicastRemoteObject;
 
public class CalculatorImple extends UnicastRemoteObject implements Calculator
{
    protected CalculatorImple() throws RemoteException 
    {
        super();
    }
    public long add(long a, long b) throws RemoteException 
    {
        return a+b;
    }
}

Listing 6: CalculatorServer.java

import java.RMI.Naming;
 
public class CalculatorServer 
{
    CalculatorServer()
    {
        try
        {
            Calci c = new CalculatorImple();
            Naming.rebind("RMI://127.0.0.1:1020/CalculatorService", c);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) 
    {
        new CalculatorServer();
    }
}	

Listing 7: CalculatorClient.java

import java.RMI.Naming;
public class CalculatorClient 
{
    public static void main(String[] args) 
    {
        try
        {
            Calculator c = (Calculator) Naming.lookup("//127.0.0.1:1020/CalculatorService");
            System.out.println("addition : "+c.add(20, 15));
        } 
        catch (Exception e) 
        {
            System.out.println(e);
        }
    }
}

Steps to run these programs:

  • First of all, put all these four programs inside bin folder of JDK.
  • Let this example suppose our JDK folder is inside java folder in drive E:
  • Now open command prompt and do the following:
  • cd\
  • e:
  • cd Java\jdk1.6.0_23\bin
  • javac Calculator.java
  • javac CalculatorImple.java
  • javac CalculatorServer.java
  • javac CalculatorClient.java
  • rmic CalculatorImple
  • start rmi registry
  • java CalculatorServer
  • open another cmd and again go to same path e:\Java1\jdk1.6.0_23\bin
  • java CalculatorClient

Output:

Addition:35

Conclusion:

In this article we studied how we can use different methods on different JVM and combine them to use as a single Application. RMI is helpful in an application where server wants to communicate with different JVM machines methods.RMI uses object serialization to marshal and unmarshal parameters and does not trim types, reinforcing true object-oriented polymorphism.



Have experience in Oracle, Java and have done certified courses in Android

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login