JAVA RMI 获取传递的 ArrayList 元素

JAVA RMI get pass ArrayList element

我有一个服务器,它在“ ServerInfo ”中包含一个 ArrayList,当我尝试从 ClientRMI 获取 ArrayList(在 ServerInfo 中)的一个元素时,例如 adf.getSGM ( 0 ).incrementCount( ) ;

"count"不增加就好像每次我调用它都会实例化一个新的class SGM

简而言之,我想从 ClientRMI 与 ServerInfo 上的 ArrayList 进行交互(英语很抱歉)

听说是 classes :

服务器

public class ServerRMI {

    public static void main(String[] args) {
        Registry registry = null;
        String name = "ServerInfo";

        try {

            System.out.println("Init RMI");

            ServerInfoInterface sir = ServerInfo.getInstance();
            ServerInfoInterface stub = (ServerInfoInterface) UnicastRemoteObject.exportObject(sir, 0);

            registry = LocateRegistry.createRegistry(9000);
            registry.bind(name, stub);

            System.out.println("RMI OK");
            System.out.println("Init SGM...");


            for(int i=0;i<3;i++){
                ServerInfo.getInstance().addSGM(new SGM());
            }

            System.out.println("Init SGM OK");

        } catch (Exception e) {
            System.out.println("RMI Error"+e.toString());
            registry = null;
        }
    }
}
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());
        }

    }

}

您正在服务器上创建一个 SGM,通过序列化将其传递给客户端,在客户端增加它的计数,然后期望该计数在服务器上神奇地增加。

没办法。

您必须使 SGM 成为具有自己的远程接口的远程对象,或者在原始远程接口中提供远程方法来增加由索引指定的 GSM 的计数。