JAVA RMI get pass ArrayList element

981 views Asked by At

I have a server that contains an ArrayList in " ServerInfo " and when I try to take from ClientRMI an element of the ArrayList(in ServerInfo) for example adf.getSGM ( 0 ).incrementCount( ) ;

"count" does not increase it's as if every time I call it instantiates a new class SGM

in a few words I want to interact from ClientRMI with ArrayList that is on ServerInfo (SORRY FOR ENGLISH)

Hear are the classes :

SERVER

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

}

CLIENT

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

    }

}
1

There are 1 answers

0
user207421 On BEST ANSWER

You're creating an SGM at the server, passing it via Serialization to the client, incrementing its count at the client, and then expecting that count to be magically increased at the server.

It can't work.

You will have to make SGM a remote object, with its own remote interface, or else provide a remote method in the original remote interface to increment the count of a GSM, specified by index.