确保RMI仅在一个Server实例上运行

时间:2014-02-22 22:06:20

标签: java rmi

我目前正试图通过实现服务器/客户端结构来获取RMI的基础知识,其中客户端可以调用服务器上的远程操作,并且服务器可以调用客户端功能:

public class Client extends GenericRMI implements ClientInterface {
    public ServerInterface server;

    public Client() {
    try {
        String IP = InetAddress.getLocalHost().getHostAddress();

        server = (ServerInterface) Naming.lookup("//192.168.2.124/WServer");

        int uniqueID = (int) Math.round(Math.random() * 1000);

        super.setUpRMI("WClient" + IP + "_" + uniqueID);

        server.registerNewClient(IP, uniqueID);

    } catch (Exception e) {
        e.printStackTrace();
    }
    }

public void setUserID(int id) {
    System.out.println("got my ID from the server: " + id);
}
}

public class Server extends GenericRMI implements ServerInterface {
private List<ClientInterface> clients;

public Server() {
    clients = new ArrayList<ClientInterface>();
    super.setUpRMI("WServer");
}

public void registerNewClient(String IP, int uID) throws RemoteException {
    try {
        ClientInterface c = (ClientInterface) Naming.lookup("//" + IP + "/WClient" + IP + "_"
                + uID);
        int newID = clients.size();
        clients.add(c);
        c.setUserID(newID);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

并且,在主要功能中:

new Server();
Thread.sleep(1000);
new Client();

Thread.sleep(1000);
new Client();

Thread.sleep(1000);
new Client();

接口由

定义
public interface ServerInterface extends Remote...

RMI设置

public class GenericRMI implements Remote, Serializable {
protected Registry registry;

public void setUpRMI(String bindName) {
    if (registry == null) {
        try {
            registry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
        } catch (ExportException e) {
            // client and server on one PC
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    try {
        Naming.rebind(bindName, this);
    } catch (RemoteException | MalformedURLException e) {
        e.printStackTrace();
    }
    System.out.println("Started " + bindName);
}
}

然而,输出就像

Started WServer
Started WClient192.168.2.124_501
got my ID from the server: 0
Started WClient192.168.2.124_655
got my ID from the server: 0
Started WClient192.168.2.124_771
got my ID from the server: 0

即使我调试它,服务器也为每个客户端提供不同的ID。我想我在某个地方犯了一个可怕的错误,因为我曾经认为服务器只运行一个实例。我怎样才能做到这一点?

修改 问题是;如果我调试registerNewClient()函数,每个客户端的相应Server对象都会更改:     服务器@ 7728992     服务器@ 5fbb71ac     ... 即使我使客户列表同步,它也无济于事。但是,使客户端字段瞬态服务器端导致其上出现空指针异常,表明它确实是一个新实例。

1 个答案:

答案 0 :(得分:1)

您只有一个实例正在运行。您只调用了new Server()一次,在任何情况下都无法将三个实例绑定到注册表中的同一名称。更可能的是,在未同步的“客户端”集合上存在并发问题。