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

前端之家收集整理的这篇文章主要介绍了java – RMI NotSerializableException虽然它是一个远程对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个基于RMI的小型聊天应用程序.

这个想法是:客户端在服务器上注册自己,并且每当服务器从客户端收到消息时,他将该消息推送给所有其他客户端.

但是我收到一个NotSerializableException虽然,对象,我作为一个方法参数传递实现Remote接口.

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

(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已经是远程.

希望你可以帮我:)

解决方法

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

>可串行化(或Externalizable),或
>导出的远程对象.

你们都不是然而,由于它实现了一个远程接口,你明确地意图(2).扩展UnicastRemoteObject的对象在构建时自动导出.不必通过UnicastRemoteObject.exportObject()显式导出的对象.

原文链接:https://www.f2er.com/java/121779.html

猜你在找的Java相关文章