discordia28 discordia28 - 7 days ago 5
Java Question

How to send a message from Server to Client using Java RMI?

The application is a Project Management application for a LAN, and it has objects such as Project, Task, etc. So RMI seemed like the way to go.

But it also has live notifications sent to certain clients as events are triggered by other clients. I read that Servers cannot keep track of the clients that has been connected to it in RMI. So as an option I think the server could connect to the client like the client connected to the server beforehand. Is this how it is done?

If not, should I resort to socket programming in this situation?

Apologies in advance if this is a stupid question.

Answer

You are right in you assumption.

For active push-style notification from server to clients, the clients have to be "servers" also.

So in the client app you also need to have a remote interface. The first time the client connects to the server you pass to it a reference to an instance of the remote interface.

This object needs to be exported as a remote RMI object, but it doesn't need to be registered in a registry, since you will directly pass a reference to it to the server who needs to call methods on it.

The server keeps a register of all the clients so it can call back when needed. Typically a Map with the key being a meaningful identifier of the clients and the value being the remote reference to the client.

When the client app is shut down, the client needs to unregister.

And the the server will probably want to have a periodic check of all the clients so it doesn't keep references to dead clients.

Your server interface would look something like that :

public interface Server extends Remote {

    void register(Client client) throws RemoteException;

    void unregister(Client client) throws RemoteException;

    void doSomethingUseful(...) throws RemoteException;

    ...

}

And your client interface:

public interface ClientCallbackInterface extends Remote {

    void ping() throws RemoteException;

    void notifyChanges(...) throws RemoteException;

}

And somewhere in your client app startup code :

ClientCallbackInterface client = new ClientImpl();

UnicastRemoteObject.exportObject(client);

Registry registry = LocateRegistry.getRegistry(serverIp, serverRegistryPort); 

Server server = (Server) registry.lookup(serviceName);

server.register(client);

It is totally possible to implement it. But not trivial. There are many things you have to take care of :

  • You must take care of which can be a problem if there are firewalls involved.
  • Could be problems with local OS firewall too, your client app actually must open local incoming ports
  • If you try to start several clients on the same machine you will have port conflict, must take care of that too
  • Totally not going to work outside of LAN

I did implement a system like this and it works fine. But all that said if I had to do it again I would definitely use something else, probably REST services and WebSockets for the callbacks. It would be much less constraints on the network part, just HTTP needed.

Comments