更新时从服务器到客户端的RMI通知

Mad*_*sen 3 java distributed client-server rmi

我正在写一个假的运送应用程序.客户端发送产品,服务器保留所有发送的产品.

现在服务器 - 因为它只是虚拟的 - 每分钟更新产品的状态(SEND - > ACCEPTED - > SHIPPED - > RECEIVED),现在我希望服务器在更新状态时更新相应的客户端.

我讨论的大多数RMI信息只谈到客户端 - >服务器..但我需要我的服务器为我的客户端调用这个...

希望你们能帮忙!

Rus*_*ard 7

服务器到客户端的通信在所有远程技术(包括RMI)中都是一个雷区.这可能是您在努力寻找有关该主题的大量文档的原因.对于受控环境中的虚拟程序,以下方法将起作用并且是最简单的方法.请注意,已省略所有错误处理.

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

interface ClientRemote extends Remote {
    public void doSomething() throws RemoteException;
}

interface ServerRemote extends Remote {
    public void registerClient(ClientRemote client) throws RemoteException;
}

class Client implements ClientRemote {
    public Client() throws RemoteException {
        UnicastRemoteObject.exportObject(this, 0);
    }

    @Override
    public void doSomething() throws RemoteException {
        System.out.println("Server invoked doSomething()");
    }
}

class Server implements ServerRemote {
    private volatile ClientRemote client;

    public Server() throws RemoteException {
        UnicastRemoteObject.exportObject(this, 0);
    }

    @Override
    public void registerClient(ClientRemote client) throws RemoteException {
        this.client = client;
    }

    public void doSomethingOnClient() throws RemoteException {
        client.doSomething();
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:在服务器上创建一个Server对象,将其添加到RMI注册表并在客户端上查找.

还有其他技术使客户端通知更容易,Java消息服务(JMS)通常用于此.