JAVA RMI获取传递ArrayList元素
我有一个服务器,它在“ServerInfo”中包含一个ArrayList,当我试图从ClientRMI中获取ArrayList(在ServerInfo中)的一个元素时,例如adf.getSGM(0).incrementCount() “count”并没有增加,就好像每次我调用它时,它都会实例化一个新类SGM 简单地说,我想从ClientRMI与ServerInfo上的ArrayList进行交互(对不起,用英语) 课程包括: 服务器 客户JAVA RMI获取传递ArrayList元素,java,arraylist,get,element,rmi,Java,Arraylist,Get,Element,Rmi,我有一个服务器,它在“ServerInfo”中包含一个ArrayList,当我试图从ClientRMI中获取ArrayList(在ServerInfo中)的一个元素时,例如adf.getSGM(0).incrementCount() “count”并没有增加,就好像每次我调用它时,它都会实例化一个新类SGM 简单地说,我想从ClientRMI与ServerInfo上的ArrayList进行交互(对不起,用英语) 课程包括: 服务器 客户 public class ClientRMI {
public class ClientRMI {
private ServerInfoInterface sgmInterface;
public void startServer() {
String name = "ServerInfo";
Registry registry;
try {
registry = LocateRegistry.getRegistry(9000);
try {
sgmInterface = (ServerInfoInterface) registry.lookup(name);
sgmInterface.getSGM(0).incrementCount();
System.out.println(sgmInterface.getSGM(0).getCount()); // always 0
} catch (AccessException e) {
System.out.println("RIM AccessException"+ e.toString());
} catch (RemoteException e) {
System.out.println("RIM RemoteException"+ e.toString());
} catch (NotBoundException e) {
System.out.println("RIM NotBoundException"+ e.toString());
}
} catch (RemoteException e) {
System.out.println("RIM RemoteException registry"+ e.toString());
}
}
}
您正在服务器上创建一个SGM,通过序列化将其传递给客户端,在客户端增加其计数,然后期望该计数在服务器上神奇地增加 它不能工作 您必须使SGM成为一个远程对象,具有自己的远程接口,或者在原始远程接口中提供一个远程方法,以增加由索引指定的GSM计数
public class ServerInfo implements ServerInfoInterface{
private ArrayList<SGM> sgmHandler = new ArrayList<SGM>();
// Singleton pattern
private static ServerInfo instance;
// Singleton pattern
public static ServerInfo getInstance() {
if (instance == null){
System.out.println("ServerInfo new instance");
instance = new ServerInfo();
}
return instance;
}
@Override
public synchronized void addSGM(SGM sgm) throws RemoteException {
sgmHandler.add(sgm);
}
@Override
public synchronized SGM getSGM(int i) throws RemoteException {
return sgmHandler.get(i);
}
}
public interface ServerInfoInterface extends Remote{
public void addSGM(SGM sgm) throws RemoteException;
public SGM getSGM(int i) throws RemoteException;
}
public class SGM implements Serializable{
/**
*
*/
private static final long serialVersionUID = -4756606091542270097L;
private int count=0;
public void incrementCount(){
count++;
}
public void decrementCount(){
count--;
}
public int getCount(){
return count;
}
}
public class ClientRMI {
private ServerInfoInterface sgmInterface;
public void startServer() {
String name = "ServerInfo";
Registry registry;
try {
registry = LocateRegistry.getRegistry(9000);
try {
sgmInterface = (ServerInfoInterface) registry.lookup(name);
sgmInterface.getSGM(0).incrementCount();
System.out.println(sgmInterface.getSGM(0).getCount()); // always 0
} catch (AccessException e) {
System.out.println("RIM AccessException"+ e.toString());
} catch (RemoteException e) {
System.out.println("RIM RemoteException"+ e.toString());
} catch (NotBoundException e) {
System.out.println("RIM NotBoundException"+ e.toString());
}
} catch (RemoteException e) {
System.out.println("RIM RemoteException registry"+ e.toString());
}
}
}