Remote Method Invocation (RMI) is an essential concept for Java developers to master. As a popular framework for implementing distributed applications, RMI is commonly tested during Java job interviews.
To help prepare for RMI questions, I’ve compiled this list of the top 10 RMI interview questions with example answers below. With insight into what hiring managers look for you can ace the RMI portion of your next Java interview.
1. What is RMI and how does it work?
RMI enables remote communication between Java objects residing on separate JVMs. It allows a client to invoke methods on a remote server object as if it were co-located on the same JVM.
Here’s a quick rundown of how RMI works
-
The server creates remote objects that implement remote interfaces extending
java.rmi.Remote. These interfaces declare the public methods available remotely. -
The server registers the remote objects with the RMI registry, essentially a phone book storing references.
-
The client looks up the remote reference via the RMI registry and invokes methods on it.
-
Under the hood, RMI handles marshalling/unmarshalling parameters and return values between client and server.
2. Explain the role of stub and skeleton in RMI
In RMI, stubs and skeletons act as mediators on the client and server sides respectively:
-
Stub: The stub acts as a client’s local representative or proxy for the remote object. When the client invokes a method on the stub, it initiates a connection to the server, marshals parameters, and relays the request.
-
Skeleton: The skeleton is a server-side proxy that unpacks the request, invokes the actual remote method, and passes return values/exceptions back to the stub.
This stub/skeleton architecture isolates the client and server from lower-level details like network connectivity, data marshalling, and transport protocols.
3. What is the role of the RMI registry in an RMI system?
The RMI registry is a simple distributed lookup service that enables clients to locate remote objects by name. It maps unique names to remote object references.
Key responsibilities include:
- Binding remote object references to names
- Providing a mechanism to lookup and obtain remote references by name
- Acting as a centralized repository of available remote services
The registry is crucial for decoupling clients from server implementations, enabling seamless access to remote objects by abstract names.
4. Explain marshalling and unmarshalling in RMI
Since RMI involves communication between JVMs, data passed as method parameters/return values needs to be packaged (marshalled) for transmission:
-
Marshalling: Converts method parameters/return values into a common wire format suitable for transmission. This serializes the objects into a byte stream.
-
Unmarshalling: Counterpart process that deserializes the byte stream back into actual objects the receiving JVM can work with.
This marshalling/unmarshalling handles serialization on both ends transparently so developers don’t have to worry about the lower-level communication details.
5. How does distributed garbage collection work in RMI?
Since RMI applications involve objects distributed across different JVMs, normal garbage collection cannot track object references spanning multiple JVMs.
RMI includes a Distributed Garbage Collector (DGC) that uses a reference counting algorithm to provide automatic distributed garbage collection.
Here are the key steps:
-
The server/client maintains a count of remote references locally.
-
Clients inform the server when obtaining a remote reference, incrementing the count.
-
Once done with the remote object, clients notify the server to decrement the reference count.
-
The server destroys the object once the count reaches zero.
This automated process frees developers from complex distributed memory management.
6. What is the difference between using bind() and rebind() in RMI registry?
The bind() and rebind() methods associate names with remote object references in the RMI registry. The key difference is:
-
bind(): Binds the name to the remote object. Fails if the name already exists. -
rebind(): Replaces any existing binding for the name with the new remote object.
So rebind() overcomes bind()‘s limitation of failing if the name already exists. It enables seamless replacement of bound remote objects.
7. How can you ensure security for RMI applications?
Some security measures for RMI include:
-
Using the RMI security manager to prevent downloading harmful classes.
-
Restricting access via firewalls, VPNs, etc.
-
Using SSL for encrypted communication.
-
Requiring authentication for accessing remote objects.
-
Not exposing internal network structure through public registries. Using port forwarding instead.
-
Code signing for integrity checks and authenticity of communicating parties.
A combination of network-level and application-level techniques is ideal for robust RMI security.
8. What is the role of Remote interfaces in RMI?
Remote interfaces extend java.rmi.Remote and define all the available remote methods. Key properties include:
-
All methods must declare
java.rmi.RemoteExceptionin theirthrowsclause. -
Methods must be declared public to be accessible by remote clients.
-
Remote interfaces should not extend non-remote interfaces.
-
Remote interfaces can extend other remote interfaces.
These interfaces essentially provide the API contract for clients to access remote functionality.
9. Explain the steps involved in developing an RMI application
Developing an RMI application generally involves the following sequence:
-
Define remote interfaces specifying remote methods.
-
Implement the remote interfaces in classes on the server.
-
Compile remote interfaces and implementations.
-
Generate stubs and skeletons using
rmic. -
Start RMI registry on server-side.
-
Run server and register remote objects with registry.
-
Client looks up remote reference and invokes methods on it.
This covers the key steps from defining interfaces through client access of remote methods.
10. What are some advantages and disadvantages of using RMI?
Some key advantages of RMI include:
-
Enables building distributed applications across JVMs.
-
Supports passing objects transparently between JVMs.
-
Integrates seamlessly with core Java and its type safety.
-
Includes automatic garbage collection framework.
-
Leverages underlying sockets for communication.
Disadvantages:
-
Can have significant overhead for remote calls.
-
adds complexity around managing server lifecycle and resources.
-
Network failures can lead to brittle applications.
-
Security requires special consideration.
-
Generally slower and less scalable than more modern approaches.
RMI interview questions and answers in java
FAQ
What is the basic principle of rmi architecture?
What is the rmi process in Google?
What questions are asked in a risk advisory interview?
What is Java RMI interview questions & answers?
RMI Interview Questions and Answers will guide us now that the Java Remote Method Invocation API, or Java RMI, is a Java application programming interface that performs the object-oriented equivalent of remote procedure calls (RPC).
What is Remote Method Invocation (RMI) in Java?
Remote Method Invocation (RMI) allows java objects that execute on one machine and invoke the method of a Java object to execute on another machine. The steps involved in developing an RMI object are: Define the interfaces. Compile the interfaces and their implementations with the java compiler Question 2. What is RMI architecture? Question 3.
How does RMI work?
RMI uses the standard method invocation to communicate between one object or another object. It loads the class byte-codes for objects that are passed using the defined parameters and return the values to the server in case of failure or success of it. RMI also allows the transmission to be done when passing the data as well.
What are the different RMI protocol implementations?
RMI has at least three protocol implementations: Java Remote Method Protocol (JRMP), Internet Inter ORB Protocol (IIOP), and Jini Extensible Remote Invocation (JERI). These are alternatives, not part of the same thing, All three are indeed layer 6 protocols for those who are still speaking OSI reference model.