Java RMI - A Complete Example

Author Avatar
Nathaniel May 04, 2016

In this example, City can be replaced by any similar model class, such as Car or Person.

Package Directory

.
├── client
│   ├── CityAddedCallbackImpl.java
│   └── ClientRunnable.java
├── interfaces
│   ├── City.java
│   ├── CityAddedCallback.java
│   └── CityPool.java
└── server
    ├── CityImpl.java
    ├── CityPoolImpl.java
    └── ServerRunnable.java

Interfaces Shared by Client & Server

Model

public interface City extends Remote{
    String getName() throws RemoteException;
    int getPopulation() throws RemoteException;
    int getTemperature() throws RemoteException;
}

Callback

public interface CityAddedCallback extends Remote {
    void cityAdded(City city) throws RemoteException;
}

Model Pool

public interface CityPool extends Remote{
    void create(String name, int population, int temperature) throws RemoteException;
    City lookup(String name) throws RemoteException;

    void register(CityAddedCallback cityAddedCallback) throws RemoteException;
    void deregister(CityAddedCallback cityAddedCallback) throws RemoteException;
}

Client

Callback Implementation

public class CityAddedCallbackImpl extends UnicastRemoteObject implements CityAddedCallback {

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

    public void cityAdded(City city) {
        System.out.println(city);
    }
}

Runnable

public class ClientRunnable implements Runnable{
    public void run() {
        try{
            CityPool cityPool = (CityPool) Naming.lookup("//host/CityPool");
            CityAddedCallback cityAddedCallback = new CityAddedCallbackImpl();

            cityPool.register(cityAddedCallback);
            cityPool.create("city1", 10, 30);

            City city = cityPool.lookup("city1");

            System.out.println(city.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Server

Model Implementation

public class CityImpl extends UnicastRemoteObject implements City {
    private String name;
    private int population;
    private int temperature;

    public CityImpl(String name, int population, int temperature) throws RemoteException {
        super();
        this.name = name;
        this.population = population;
        this.temperature = temperature;
    }

    public String getName() throws RemoteException {
        return name;
    }

    public int getPopulation() throws RemoteException {
        return population;
    }

    public int getTemperature() throws RemoteException {
        return temperature;
    }
}

Model Pool Implementation

public class CityPoolImpl extends UnicastRemoteObject implements CityPool {
    private List<City> cities;
    private List<CityAddedCallback> cityAddedCallbacks;

    public CityPoolImpl() throws RemoteException {
        super();
        this.cities = new ArrayList<>();
    }

    public void create(String name, int population, int temperature) throws RemoteException {

    }

    public City lookup(String name) throws RemoteException {
        return null;
    }

    public void register(CityAddedCallback cityAddedCallback) throws RemoteException {

    }

    public void deregister(CityAddedCallback cityAddedCallback) throws RemoteException {

    }
}

Runnable

public class ServerRunnable implements Runnable{

    public void run() {
        try{
            CityPool cityPool = new CityPoolImpl();
            Naming.rebind("CityPool", cityPool);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}