Chapter 18: DISTRIBUTED COMPUTING
Remote Method Invocation
A mechanism for creating distributed applications is provided by the Java API known as RMI (Remote Method Invocation). An object can call methods on another object that is running in a different JVM thanks to RMI.
The stub and the skeleton are two items that the RMI makes use of to enable remote communication between applications.
Stub
An object that acts as a client-side gateway is called a stub. Both incoming and outgoing requests are handled by it. It represents the distant item and is situated on the client-side. The following actions are taken when the caller calls a method on the stub object:
It establishes a connection with the remote Virtual Machine (JVM), writes and sends the arguments to the JVM, waits for the outcome, reads the return value or exception, and then sends the value back to the caller.
Skeleton
An object that acts as a portal for the server-side object is called a skeleton. It controls the distribution of all incoming requests. The skeleton responds to requests by doing the following things:
The remote method parameter is read.
1. It calls the method on the actual remote object,
2. then writes and sends (marshals) the result to the caller.
Distributed applications
Any program that carries out these tasks is distributable.
The application needs to find the remote method.
The program must load the class definitions for the objects in order for it to interact with distant objects.
The RMI application is referred to as a distributed application since it has all of these features.
RMI Java Example
>Create the remote user interface.
Get Dilakshi Fernando’s stories in your inbox
Join Medium for free to get updates from this writer.
>Implement a remote interface, please.
>Compile the implementation class and produce the stub and skeleton objects using the rmic tool.
>Start the registry service by using the rmiregistry utility.
>Create the remote program and run it.
>Create and run the client program.
The remote interface and the client application are the only two files needed by the client application. The remote interface in the rmi program is used for communication between the client and the server. As soon as the client program calls methods on the proxy object, RMI forwards the request to the remote JVM. The proxy object receives the return value, which is then sent to the client application.
Step 1 (create the remote interface)
Create the remote interface by extending the Remote interface and declaring the RemoteException with all of the RemoteException’s methods. In this instance, we’re creating an extension of the Remote interface called a remote interface. Add() is the sole method, and it raises a RemoteException.
Step 2 (Provide the remote interface implementation)
To provide an implementation of the Remote interface, we must either extend the UnicastRemoteObject class or use its exportObject() function. If you extend the UnicastRemoteObject class, you must define a constructor that defines RemoteException.
Step 3 (Using the rmic tool, create the stub and skeleton objects)
Then, stub and skeleton objects are created using the rmi compiler. The RMI compiler is started by the rmic tool, which also creates skeleton and stub objects.
Step 4 (The rmiregistry tool should be used to start the registry service)
Start the registry service now by using the rmiregistry utility. It will use the default port number if you don’t provide one. We’re going to utilize port 5000 for this example.
Step 5 (The rmiregistry tool should be used to start the registry service)
RMI functions must now be executed within a server process. Methods for getting and storing the remote object are available in the Naming class. There are 5 methods in the Naming class.
Step 6 (Make and launch the client application)
Using the lookup() method of the Naming class at the client, we retrieve the stub object and call the method on it. We will use localhost in this example because the server and client programs are running on the same computer. If you want to access the remote object from another computer, replace localhost with the hostname (or IP address) of the remote object.
THANK YOU….!







