我在理解我正在制作的程序的内部工作方面遇到了问题。 该程序应该是一个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
答案 0 :(得分:2)
您绑定的GameServer
实例与您在main方法中创建的实例不同。
它是接收RMI调用的绑定实例,而它是线程将使用的主要方法实例 - 因此您有两个不同的客户端列表。
在GameServer
中替换以下行:
UnicastRemoteObject.exportObject(engine, 0);
使用:
UnicastRemoteObject.exportObject(this, 0);