Distributed Computing
Methods calls for objects in the same heap. Also, can invoke method on object in different machine. Machines get information from another machines using sockets and I/O. OutputStram send data t another machine through a socket connection. We can call another method on another computer that running on different JVM. But it cannot do directly. Remote Method Invocation is a mechanism that allow to invoke another machined method.
RMI design have four things as server, client, server helper, and client helper. Create client and server apps and the server app is remote service that has object with the invoking method, the client server helpers are responsible for communication. It helps client to act on local object of calling method. As real-world client helper is a proxy. But client helper is not a remote service and doesn’t contain actual method logic. It connects the server and send details of the method call. The server helper gets the request from client helper through a socket connection, and it get the details about method call. Server helper is the one who invoke the real method on real service object. Service helper return the value to the client helper and it unpack details and gives to client object.
RMI build client and service helper object and it look like a real service. The runtime environment provides by RMI including lookup service and allow client to find the client helper. RMI doesn’t have to write I/O or network configurations. The difference between RMI and normal method call is client looks like finding local method, but client helper sends the call through the network.
When using RMI have to choose protocols like JRMP, IIOP.JRMP from RMI native protocols. Java vs Java RMI use IIOP protocol, and it is use for CORBA.
The client helper called as stub and server helper called as skeleton in RMI. To create them first wants to create Remote Interface. It is use as stub and actual service. Then another class for remote implementation. Then generate the stub and skeleton can do by using rmic.
Get Hansika Samaraweera’s stories in your inbox
Join Medium for free to get updates from this writer.
rmic MyRemoteImpl
the RMI registry should start, and it is similar to a phonebook. User can get stub and client helper from this registry. Then the remote service can start.
The remote interface should extend the java.rmi.Remote interface. Then the throwing a RemoteException is important because when client invoking the remote method some exceptions can occur. The return values and arguments should be primitive or serializable because those are travel through the network.
The remote implementation should implement the Remote interface. Also, should extend the UnicastRemoteObject for to give some functionalities to the object relating remote. There should be a default constructor with no arguments and declare the RemoteException. Also, it should register with the RMI registry.
Generating stubs and skeletons should do by run rmic on remote implementation class. RMI automatically creates the stub and skeleton. Then start the RMI registry and start other services. Client gets the stub object when does a lookup and RMI registry gives the stub.
Reference
[1] Bates, Bert, and Kathy Sierra. “Head First Java Second Edition.” (2021).







