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

2

6

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.

Dithyramb answered 26/3, 2015 at 16:48 Comment(4)
Could you keep the connection alive as in #227776Fijian
@Fijian That question is not about how to keep the connection alive, and keeping it alive is not a solution to this problem.Lumbar
@EJP Well my question is about whether the connection is being dropped and recreated or kept alive ? I wanted to know this since the solution I am proposing was in the lines of WebSockets or exactly that instead of even RMI.Fijian
@Fijian What connection? RMI doesn't expose connections, and doesn't rely on them being kept alive in any way.Lumbar
S
12

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.

Substitutive answered 26/3, 2015 at 17:21 Comment(4)
Good to know this. QQ though. How is callback executed. Does Client interface extend ClientCallbackInterface interface?Aigrette
Yes there was a typo in my code. I edited it. Thanks for noticingSubstitutive
@PierreHenry Other than port issues, why "Totally not going to work outside of LAN"? And does LAN include VPLAN?Photofinishing
@Paul: well, nothing other than port issues, but that is a big one. It would work on VPN if you have control over the firewall and ports that can be opened. By default RMI uses random ports but you can change that.Substitutive
A
0

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.

Aigrette answered 26/3, 2015 at 16:56 Comment(4)
As I have written in my answer, you actually can implement the observer pattern with RMI. However I agree that JMS (or something else) would probably be a better option.Substitutive
The remote object is a server, and the process that calls its remote methods is a client. It is possible for an RMI client to also be a server: this is a callback pattern. It is not necessary to use Sockets and Observables.Lumbar
Have modified the answer to remove "cannot be done" by RMI part. Please do add an answer or provide link to the callback pattern you are referring to.Aigrette
@PierreHenry has already provided all the information you need. Implementing the RMI callback pattern is extremely trivial, and the Registry and/or LDAP are not relevant to that one way or the other. Your entire answer remains incorrect.Lumbar

© 2022 - 2024 — McMap. All rights reserved.