【问题标题】:JAVA RMI get pass ArrayList elementJAVA RMI 获取传递ArrayList 元素
【发布时间】:2015-06-09 16:26:51
【问题描述】:

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

“count”并没有增加,就好像我每次调用它都会实例化一个新的类SGM

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

听课:

服务器

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());
        }

    }

}

【问题讨论】:

    标签: java arraylist get element rmi


    【解决方案1】:

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

    它不能工作。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多