Remote Method Invocation (RMI) provides a programmer, an ability to invoke methods remotely through objects when they are running on different virtual machines. Which means that two different machines connected to the Internet can communicate with each other in a distributed environment.
How RMI Works:
It is like a two-way handshake, the client sends some information to the remote object, and remote machine acting as the server responds to the request and returns the object and its related desired method details.
In RMI, there are two objects stub and skeleton. The stub acts as a client-side object, whereas skeleton act as server side object.
A stub is a gateway at the client side It sends outgoing requests to the server.
To understand stud, think like stub is junior, he first extends his hand to shake by initiating the connection with the remote server and sends the parameters to the server. It then waits for the server to reply to its handshake. When the boss responds to its hand and says something in reply, it interprets the reply and tells the client.
The skeleton is a gateway at the server side. All the incoming requests are routed through the skeleton.
To understand skeleton, think the skeleton is like a boss. When it sees the stub’s hand and listen to the stub’s message for the remote method, invokes the actual remote method and complete the handshake by replying in the form of result.