Search code examples
javarmiremote-access

Two calls access a static variable with different results


I'm getting this issue while trying to access a shared static variable between two different methods...

Suppose that the environment should be like this:

Method 1 with RMI interface CommonUtils:

CommonUtils service = registry.lookup("chat"); //i'm sure that it works fine
service.register(String username);...

Implementation of CommonUtils:

public static ArrayList<ChatInterface> connectedChat=new ArrayList<ChatInterface>();
public static void register(String username){
     connectedChat.add(username);
}
public static String getChatByUsername(String username){
    for(ConnetctedChat temp:connectedChat)
       if (temp.getUsername().equals(username))
           return temp;
} ...

Method 2 calls the utils directly from the same library:

String username;
ChatInterface tmp=CommonUtils.getChatByUsername(username); <---- This is "the problem"

Now, when I try to inspect what is inside the "ArrayList connectedChat", I see two different results: the right one is when I take info from the first method. Otherwise, when I try to take something out from the "Method 2", it says that the ArrayList is empty, so I can't operate anymore (but from the other method it seems that the list isn't empty!).

The thing that I'm trying to resolve is a kind of Chat service... it works from client sender - server - client receiver, but it seems that it doesn't work in the simply server sender - client receiver communication.


Solution

  • You seem to be expecting static variables to be shared between JVMs by RMI. They aren't. That's why remote methods exist.