Backend Development 10 min read

Exploring JDK RMI: Architecture, Code Samples, and Service Invocation Process

This article introduces JDK RMI, explains its role in distributed Java applications, provides complete Maven project examples for interface, server, and client implementations, and details the underlying registry, stub, skeleton, and export mechanisms that enable remote method calls.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Exploring JDK RMI: Architecture, Code Samples, and Service Invocation Process

RMI (Remote Method Invocation) is a built‑in Java framework for creating distributed applications; many RPC frameworks such as Dubbo, Spring RMI, and Hessian support the RMI protocol. The article walks through a complete example consisting of three Maven modules: rmiclient (client), rmiserver (server), and rmidependency (shared interface).

HelloWorldService interface :

public interface HelloWorldService extends Remote {
    public String sayHello(String from, String to) throws RemoteException;
}

Server implementation ( HelloWorldServiceImpl ) extends UnicastRemoteObject and overrides sayHello to return a concatenated string.

public class HelloWorldServiceImpl extends UnicastRemoteObject implements HelloWorldService {
    public HelloWorldServiceImpl() throws RemoteException { super(); }
    @Override
    public String sayHello(String from, String to) throws RemoteException {
        return from + " say hello to " + to;
    }
}

RMIServer creates the service instance, starts a registry on port 1099, and binds the service:

public class RMIServer {
    public static void main(String[] args) {
        try {
            HelloWorldService helloWorldService = new HelloWorldServiceImpl();
            Registry registry = LocateRegistry.createRegistry(1099);
            registry.bind("rmi://localhost:1099/helloWorldService", helloWorldService);
        } catch (RemoteException | MalformedURLException | AlreadyBoundException e) {
            e.printStackTrace();
        }
    }
}

RMIClient looks up the remote object and invokes sayHello :

public class RMIClient {
    public static void main(String[] args) {
        try {
            HelloWorldService helloWorldService = (HelloWorldService) Naming.lookup("rmi://localhost:1099/helloWorldService");
            if (helloWorldService != null) {
                String result = helloWorldService.sayHello("java", "菜鸟");
                System.out.println(result);
            } else {
                System.out.println("rmi 查找服务错误");
            }
        } catch (NotBoundException | MalformedURLException | RemoteException e) {
            e.printStackTrace();
        }
    }
}

The three essential roles in RMI are:

Registry (center) : registers services.

Server : provides the concrete implementation of the remote interface.

Client : obtains a proxy and invokes remote methods.

Internally, both HelloWorldServiceImpl and the registry extend RemoteObject . The RemoteObject holds a RemoteRef (implemented by UnicastRef ), which contains a LiveRef that wraps network details such as Endpoint and Channel .

When a service is exported, UnicastServerRef.exportObject() creates a stub (client proxy) and a skeleton (server dispatcher). The exportObject method of Transport opens a ServerSocket , starts a listener thread, and registers the Target object in the ObjectTable .

On the client side, LocateRegistry.getRegistry() builds a RemoteRef (either UnicastRef or UnicastRef2 ) and creates a dynamic proxy (the stub). The stub’s lookup method sends the service name to the registry, receives the remote reference, and finally invokes the remote method via RemoteRef.invoke() , which performs the socket communication.

Summary : The article demonstrates the complete RMI workflow—from defining the remote interface, implementing the server, publishing the service, to looking up and invoking it from a client—while also exposing the underlying mechanisms such as registry binding, stub/skeleton generation, and transport export.

distributed systemsJavaBackend DevelopmentRMIJava ExampleRemote Method Invocation
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.