At a high-level prospective, Java RMI consists of several well-split layers:

  • Application Layer

    Provides remote interface.

  • Stub/Proxy Layer

    This layer automatically handles method invocation marshalling and unmarshalling, network transmission.

  • Remote Reference Layer

  • Transport Layer

    Provides TCP-based communication for RPC.

Example

Remote Interface

Defines methods callable remotely.

Remote Interface
1
2
3
4
5
6
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface HelloService extends Remote {
String sayHello(String name) throws RemoteException;
}

Remote Object Implementation

Implement the interface defined and extend the remote object class.

Remote Object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;

public class HelloServiceImpl extends UnicastRemoteObject
implements HelloService {

protected HelloServiceImpl() throws RemoteException {
super();
}

@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}