通过RMI引用枚举

时间:2014-06-29 06:57:31

标签: java enums rmi

我将从一些代码开始,稍后解释我的问题。 在服务器端:

public enum LobbyImpl implements Lobby, Serializable {
    INSTANCE();
    private List<Account> accounts;

    private LobbyImpl(){
        this.accounts = new LinkedList<>();
    }

    public synchronized void join(Account userAcc) {
        accounts.add(userAcc);
    }

    public List<Account> getAccounts() {
        return accounts;
    }
}

public class AccountImpl implements Account, Serializable {
    public Lobby getLobby() {
        return LobbyImpl.INSTANCE;
    }
}

public enum AccountManagerImpl implements AccountManager {
    INSTANCE;
    public Account login() {
        Account acc = new AccountImpl();
        LobbyImpl.INSTANCE.join(acc);
        return acc;
    }
}

public class Server extends UnicastRemoteObject {
    public static void main(String[] args) {
        Registry rmiregistry = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
        AccountManager accMan = (AccountManager) UnicastRemoteObject.exportObject(AccountManagerImpl.INSTANCE, 0);
        rmiregistry.rebind("accMan", accMan);
    }
}

在客户端:

Registry registry = LocateRegistry.getRegistry();
AccountManager accMan = (AccountManager) registry.lookup("accMan");
Account myAccount = accMan.login();
Lobby lobby = myAccount.getLobby();
List<Account> accs = lobby.getAccounts();

这将始终返回一个空的帐户列表。我怎样才能解决这个问题?不太确定问题出在哪里。一切都编译并运行没有错误。我甚至不确定LobbyImpl是否应该实现Serializable。我希望有人可以帮助我。

提前致谢

0 个答案:

没有答案