java 怎么给其他进程发送消息,如何使用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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值