简体   繁体   中英

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.

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.

As Pierre Henry's Answer suggest you can implement a call back pattern that uses observer design pattern in RMI.


On granular level you can implement socket programming and use Observer design pattern . Each client will first register on to the server. Server will store them in a data structure and when some event notification is to be sent server will iterate over these registered clients and invoke methods or push notifications to them.

Consider using some implementation of JMS (Java messaging service) like Active MQ . Your sender will asynchronously listen to a queue. Sender will send the message and a receiver will receive the message. If you want all client to receive message use topics instead of queues and there will be publishers and subscribers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM