Java RMI

Basic model of JAVA RMI: The execution of distributed objects in Java is possible with the help of Java RMI. A client can access a hosted Java object on a remote virtual machine from any JVM. It is a two-step procedure. The first step is about running the clients and servers in Java. This will allow them an inherently object-oriented look and feel and objects to interact with all the features of Java. So it means we can access/execute a server object from any Java Virtual Machine (JVM), hence we can achieve platform independent.

Second, the fundamental Java RMI model always has a client program, and this client program is used to address remote objects from any Java virtual machine. For the connection between a client and a remote object a reference to the object is needed, this is hosted by a server program. The remote server can locate the server object in two different ways. These two procedures have their own methods to remote reference the client. These are the procedures are,

• Explicitly.
• Implicitly.

Both are used to “get a remote reference”.

Java RMI architecture:

To create a math service using Java RMI, I have followed these steps.

1. Define a remote interface
2. Server Implementation
3. Client Implementation
4. Compile the source code
5. Start the Java RMI registry, the server, and then the client.

Create the remote interface:

In Java RMI an interface is used to extend the “java.rmi.Remote” interface. The remote interface does not have any methods of its own, and is used to tag remote objects, making it possible to identify them as they are. (Harold ER, 2000). Set of remote methods also declared in the interface. Each remote method must declare “java.rmi.RemoteException” or a superclass of “RemoteException” in its throws section, in addition to any application-specific exceptions.

Example that I have used in this paragraph for the remote interface, “bite.example.SampleServer”. Declare all four methods, “Add”, “Subtract”, “Multiply” and “Square”.

The source code for “SampleServer.java” is shown below.

sample server
bite package.example;
import java.rmi.Remote;
import java.rmi.RemoteException;
the public interface SampleServer extends Remote
{
public add int (int x, int y) throws RemoteException;
public int subtract(int x, int y) throws RemoteException;
public int multiply (int x, int y) throws RemoteException;
public int square(int x) throws RemoteException;
public int getValue() throws RemoteException;
public String getLastOp() throws RemoteException;
}

Server implementation:

In this context, our “server” class exports the remote object and this server class contains a “main” method, this produces an instance of the remote object implementation. It then matches that instance with a name in a Java RMI record. The class that contains this “main” method is possibly the implementing class itself, or another class entirely.

In the “Server” class we declare the “main” method for the server, and it also performs the remote interface SampleServer. Our server’s “main” method follows these two steps. In the first step, a new remote object is produced and exported; the second step deals with registering an object with a Java RMI registry.
The source code of the “SampleServerImpl.java” class is as follows.

Sample server implementation.
bite package.example;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class SampleServerImpl implements SampleServer {
private int value = 0;
private string lastOp;
SampleServerImpl(){
}
public add int (int x, int y) throws RemoteException {
value = x + y;
lastOp = “ADD”;
return value;
}
public int subtraction (int x, int y) throws RemoteException{
value = xy;
lastOp = “REST”;
return value;
}
public int multiply (int x, int y) throws RemoteException{
value = x*y;
lastOp = “MULTIPLY”;
return value;
}
public int square(int x) throws RemoteException{
value = x*x;
lastOp = “SQUARE”;
return value;
}
/* Resource properties */
public int getValue() throws RemoteException {
return value;
}

public void setValue(intvalue) {
this.value = value;
}
public String getLastOp() throws RemoteException {
return lastOp;
}
public void setLastOp(String lastOp) {
this.lastOp = lastOp;
}
public static void main(String args[]) {
try {
//Create and export a remote object
ImplServerInstance obj = newImplServerInstance();
SampleServer stub = (SampleServer)UnicastRemoteObject.exportObject(obj, 0);
//Register the remote object with a Java RMI registry
// and bind the stub of the remote object in the registry
Registry registry = LocateRegistry.getRegistry();
log.bind(“SampleServer”, stub);

System.err.println(“Server Ready”);
} catch (Exception e) {
System.err.println(“Exception from server: ” + e.toString());
e.printStackTrace();
}
}
}

Client Implementation:

The client class acquires a “stub” for the registry on the server host, and looks up the remote object’s stub in the registry by its names, and then calls the “Add”, “Subtract”, “Multiply” methods and “Square”. on the remote object using stub.
The source code for the Client is as follows.

sample customer

bite packet.example;
import java.io.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;
public class sample client {
public static void main(String[] arguments) {
String host = (args.length start java -Djava.rmi.server.codebase=file:C:/rmi/ -Djava.rmi.server.name=192.168.0.03 bite.example.SampleServerImpl”
And then I got the output “Server Ready”

Start the client – ​​The final step is to start the client. When the server was ready, I opened another command line window and then ran the client as follows “C:rmi>java bite.example.SampleClient”

Leave a Reply