RMI调用未添加回调监听器



我在理解我正在制作的程序的内部工作时遇到了问题。该程序应该是一个RMI客户端,它与服务器联系并注册自己。然后,服务器应该在循环中调用客户机上的方法。但是,没有添加侦听器。

注意输出。在服务器上添加侦听器后,该方法打印出正确的大小,但运行服务器的线程不会打印出来,因为列表仍然为空。为什么,为什么会这样?

客户

public class GameClient extends Thread implements Remote, Client, ModelChangeListener<Client>{
private static final long serialVersionUID = -394039736555035873L;
protected Queue<GameModelEvent> queue = new ConcurrentLinkedQueue<GameModelEvent>(); 

public GameClient(){
}
public static void main(String[] args){
    GameClient client = new GameClient();
    client.start();
}

protected void bind(){
    System.setProperty("java.rmi.server.codebase","file:bin/");
    try {
        Registry registry = LocateRegistry.getRegistry();
        Client c = (Client)UnicastRemoteObject.exportObject(this, 10999);
        Server stub = (Server) registry.lookup("Server");
        stub.registerClient(c);
    } catch (RemoteException | NotBoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
public void run() {
    super.run();
    bind();
    while(!Thread.interrupted()){
        System.out.print(".");
        GameModelEvent event = queue.poll();
        while(event != null){
            System.out.println(event);

            event = queue.poll();
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            return;
        }
    }
}
   .....
}
服务器

public class GameServer extends Thread implements ModelChangeListener<GameServer>, Server{
protected Queue<GameModelEvent> queue = new ConcurrentLinkedQueue<GameModelEvent>(); 
List<Client> clients = Collections.synchronizedList(new ArrayList<Client>());
public static void main(String[] args){
    GameServer server = new GameServer();
    server.start();
}
protected void bind(){
    System.setProperty("java.security.policy","file:policy.policy");
    System.setProperty("java.rmi.server.codebase","file:bin/");
    if (System.getSecurityManager() == null) {
        System.setSecurityManager(new SecurityManager());
    }
    try {
        String name = "Server";
        Server engine = new GameServer();
        Server stub =
            (Server) UnicastRemoteObject.exportObject(engine, 0);
        Registry registry = LocateRegistry.getRegistry();
        registry.rebind(name, stub);
        System.out.println("ComputeEngine bound");
    } catch (Exception e) {
        System.err.println("GameServer exception:");
        e.printStackTrace();
        System.exit(1);
    }
}
public void run() {
    super.run();
    bind();
    while(!Thread.interrupted()){
        System.out.print(clients.size()+ " ");
            try {
                for(Client c : clients)
                c.modifyConnection(null);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        GameModelEvent event = queue.poll();
        while(event != null){
            System.out.println(event);

            event = queue.poll();
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            return;
        }
    }
}
@Override
public void registerClient(Client client) {
    System.out.println("nAdded client "+client);
    clients.add(client);
    System.out.println("clients size "+clients.size());     
}
 ...
 }

输出
ComputeEngine bound
0 0 0 0 
Added client Proxy[Client,RemoteObjectInvocationHandler[UnicastRef [liveRef: [endpoint:[10.117.2.88:10999](remote),objID:[5c30c56:13c5dfe5faf:-7fff, 1084850783049542281]]]]]
clients size 1
0 0 0 0 0 

您绑定的GameServer实例与您在主方法中创建的实例不同。

它是将接收RMI调用的绑定实例,而它是将被线程使用的主方法实例-因此您有两个不同的客户端列表。

GameServer中替换行:

UnicastRemoteObject.exportObject(engine, 0);

:

UnicastRemoteObject.exportObject(this, 0);

最新更新