gpt4 book ai didi

java - RMI NotSerializableException 尽管它是一个远程对象

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:11:16 24 4
gpt4 key购买 nike

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

思路是:Client在Server上注册自己,Server每次收到一个Client的消息,就把这条消息推送给所有其他Client。

但是我收到了一个 NotSerializableException,尽管我作为方法参数传递的对象实现了 Remote 接口(interface)。

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

(ClientChat)接口(interface):

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

(服务器聊天)接口(interface)

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

}

(服务器聊天)实现

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

异常(exception):

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,但我想知道为什么会出现这种异常。

希望你能帮助我:)

最佳答案

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

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

  2. 导出远程对象。

你的两者都不是。但是,因为它实现了一个 Remote 接口(interface),这显然是您想要的 (2)。扩展 UnicastRemoteObject 的对象在构造时自动导出。不需要的对象必须通过 UnicastRemoteObject.exportObject() 显式导出。

关于java - RMI NotSerializableException 尽管它是一个远程对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11049810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com