JAVA RMI basic model: 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. They are two-step procedures. The first step is running the clients and servers in Java. This will allow them an inherent object-oriented appearance and the objects will interact with all the Java features. So it means that we can access / execute a server object from any Java Virtual Machine (JVM), therefore we can achieve platform independently.
Second, the fundamental Java RMI model always has a client program, and this client program is used to approach remote objects from any Java virtual machine. For the connection between a client and a remote object, a reference to the object is needed, it is hosted by a server program. The server can locate the remote server object in two different ways. Both of these procedures have their own methods for remotely referencing the client. These are the procedures,
• 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 method of its own, and it is used to label remote objects, allowing them to be identified 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 exception.
Example I have used in this paragraph for the remote interface, “bite.example.SampleServer”. Declare the four methods, “Addition,” “Subtraction,” “Multiplication,” and “Square.”
Below is the source code for “SampleServer.java”.
Sample server
bite.example package;
import java.rmi.Remote;
import java.rmi.RemoteException;
SampleServer public interface extends Remote
{
public int also (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;
}
Sever 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. Then combine that instance with a name in a Java RMI record. The class that contains this “main” method is possibly the implementation class itself or another class entirely.
In the “Server” class we declare the “main” method for the server, and it also performs the SampleServer remote interface. Our server’s “main” method follows these two steps. A new remote object is produced and exported in the first step, the second step deals with registering an object with a Java RMI registry.
The source code for the “SampleServerImpl.java” class is as follows.
Server Example Impl.
bite.example package;
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 int also (int x, int y) throws RemoteException {
value = x + y;
lastOp = “ADD”;
return value;
}
public int subtract (int x, int y) throws RemoteException {
value = x – y;
lastOp = “SUBTRACT”;
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 (int value) {
this.value = value;
}
public String getLastOp () throws RemoteException {
return lastOp;
}
public void setLastOp (String lastOp) {
this.lastOp = lastOp;
}
public static void main (String args[]) {
treat {
// Create and export a remote object
SampleServerImpl obj = new SampleServerImpl ();
SampleServer stub = (SampleServer) UnicastRemoteObject.exportObject (obj, 0);
// Register the remote object with a Java RMI registry
// and bind the remote object stub in the registry
Registry registry = LocateRegistry.getRegistry ();
record.bind (“SampleServer”, stub);
System.err.println (“server ready”);
} catch (Exception e) {
System.err.println (“Server exception:” + e.toString ());
e.printStackTrace ();
}
}
}
Client implementation:
The client class acquires a “stub” for the registry on the server host, looks up the remote object’s stub in the registry by their names, and then invokes the “Add”, “Subtract”, “Multiply” and “methods. Square”. on the remote object using stub.
Below is the client source code.
Sample client
bite.example package;
import java.io. *;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;
Public class SampleClient {
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 have the output “Server ready”
Start the client: The last step is to start the client. When the server was ready, I open another window from the command prompt line and then run the client as follows “C: rmi> java bite.example.SampleClient”