RMI NotSerializableException虽然它是一个远程对象

时间:2012-06-15 11:26:19

标签: java rmi

我正在编写一个基于RMI的小型聊天应用程序。

这个想法是:客户端在服务器上注册自己,每次服务器从客户端收到消息时,他都会将此消息推送给所有其他客户端。

但是我收到NotSerializableException,但是,我作为方法参数传递的对象实现了Remote接口。

这是一些代码: (有问题的部分是this中的this.chatServ.registriereClient(this);参数(ClientChat Implementation))

(ClientChat)界面:

public interface ChatClient extends Remote
{

}

(ClientChat)执行:

public class ChatClientImpl implements ChatClient
{

    ChatServer chatServ;
    String clientName;

    public ChatClientImpl(String clientName, ChatServer chatServ) {
        this.chatServ = chatServ;
        this.clientName = clientName;
        try {
            this.chatServ.registriereClient(this);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}

(ServerChat)接口

public interface ChatServer extends Remote
{
        void registriereClient(ChatClient client) throws RemoteException;

}

(ServerChat)实施

public class LobbyChatServerImpl implements ChatServer
{

    ArrayList<ChatClient> clientListe = null;

    @Override
    public void registriereClient(ChatClient client) {
        System.out.println("Client registriert");
        this.clientListe.add(client);
    }
}

客户端:

  public static void main(String[] args) {
        ChatServer lobbyChatServer = null;
        try {
            Registry registry = LocateRegistry.getRegistry(Server.RMI_PORT);
            lobbyChatServer = (ChatServer) registry.lookup("LobbyChatServer");

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NotBoundException e) {
            e.printStackTrace();
        }

        ChatClient lobbyChat = new ChatClientImpl(name, lobbyChatServer);
  }

服务器:

    public static void main(String[] args) {
        try {
            if (System.getSecurityManager() == null) {
                System.setSecurityManager(new RMISecurityManager());
            }

            Registry registry = LocateRegistry.getRegistry(RMI_PORT);

            ChatServer lobbyChatStub = (ChatServer)UnicastRemoteObject.exportObject(new LobbyChatServerImpl(), 0);
            registry.bind("LobbyChatServer", lobbyChatStub);

        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (AlreadyBoundException e) {
            e.printStackTrace();
        }
    }

例外:

java.rmi.MarshalException: error marshalling arguments; nested exception is: 
    java.io.NotSerializableException: de.XX.Chat.ChatClientImpl
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:156)
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
    at $Proxy0.registriereClient(Unknown Source)
    at de.XX.Chat.ChatClientImpl.<init>(ChatClientImpl.java:19)
    at de.XX.Client.main(Client.java:49)
Caused by: java.io.NotSerializableException: de.XX.Chat.ChatClientImpl
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
    at sun.rmi.server.UnicastRef.marshalValue(UnicastRef.java:292)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:151)
    ... 5 more

正如已经说过的,我想知道为什么我会得到这种异常,虽然ChatClientImpl已经是Remote。

希望你能帮助我:)。

3 个答案:

答案 0 :(得分:14)

作为参数传递的对象或远程方法的结果必须是:

  1. 可序列化(或可外部化)或

  2. 导出远程对象。

  3. 你的不是。但是,因为它明确地实现了远程接口(2)。 扩展UnicastRemoteObject的对象在构造时自动导出。必须通过UnicastRemoteObject.exportObject()显式导出不需要的对象。

答案 1 :(得分:1)

您可以做的是设置一个回调对象。这是一个扩展UnicastRemoteObject的方法,当你通过它时它就变成了一个回调。

http://www.cs.swan.ac.uk/~csneal/InternetComputing/RMIChat.html


Remote不是可序列化的。您不能以这种方式传递代理对象。即使您将其设置为Serializable,它也会发送服务器上存在的对象的副本。不会调用客户端上对象的副本。

要让您的“服务器”向“客户”发送消息,您必须在“客户端”上创建服务,使其成为服务器。

您可能会发现使用JMS等消息传递解决方案更适合这种情况。 JMS服务器具有可以发布和订阅的主题。要使用的简单JMS服务器是Active MQ。

答案 2 :(得分:1)

看起来您可能已经忘记了扩展UnicastRemoteObject&#39;在接口实现上:

public class ChatClientImpl extends UnicastRemoteObject implements ChatClient{
} 

public class LobbyChatServerImpl extends UnicastRemoteObject  implements ChatServer{
}