RMI 异常 "error marshalling arguments"

RMI exception "error marshalling arguments"

我正在开发一个 RMI 项目,该项目有两个独立的客户端和服务器项目。我使用了启动rmiregistry。当我尝试 运行 我的服务器应用程序时,出现异常。

public class Runner extends UnicastRemoteObject {

    public Runner() throws RemoteException {
        try {
            ServerOperations so = new ServerSide();
            Naming.rebind("rmi://localhost:2000/MiveCoffeeService", so);
            System.out.println("Server is online.");

        } catch (RemoteException | MalformedURLException | FileNotFoundException ex) {
            Logger.getLogger(Runner.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) throws RemoteException {
        new Runner();
    }

}

例外情况是:

Sep 11, 2015 9:05:51 PM ir.nscogroup.coffeemive.Runner <init>
SEVERE: null
java.rmi.MarshalException: error marshalling arguments; nested exception is: 
    java.io.NotSerializableException: dataaccess.ServerSide
    at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
    at java.rmi.Naming.rebind(Naming.java:177)
    at ir.nscogroup.coffeemive.Runner.<init>(Runner.java:29)
    at ir.nscogroup.coffeemive.Runner.main(Runner.java:38)
Caused by: java.io.NotSerializableException: dataaccess.ServerSide
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
    ... 4 more

怎么了?

异常堆栈跟踪确切地告诉你哪里出了问题:

java.io.NotSerializableException: dataaccess.ServerSide

您正在尝试序列化不可序列化的 class、dataaccess.ServerSide。解决方案:让您的 class 和所有必要的组成部分 class 实现 Serializable 接口。

谢谢你们。这个示例解决了我的问题。

package com.javacodegeeks.core.rmi.rminterface;

public class Configuration {

    public static final int REMOTE_PORT = 8888;
    public static final String REMOTE_ID = "RMI_EXAMPLE";
    public static final String REMOTE_HOST = "localhost";

}


package com.javacodegeeks.core.rmi.remoteserver;

import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import com.javacodegeeks.core.rmi.rminterface.Configuration;

public class RemoteServer {

    public static void main(String[] args) throws RemoteException, AlreadyBoundException {

        RMIImplementation rmiImplementation = new RMIImplementation();
        Registry registry = LocateRegistry.createRegistry(Configuration.REMOTE_PORT);
        registry.bind(Configuration.REMOTE_ID, rmiImplementation);
    }
}

这里的问题是 ServerSide 既不是导出的远程对象也不是 Serializable. 几乎可以肯定你想要前者,所以你必须:

  • 使其扩展 UnicastRemoteObject,或
  • 通过UnicastRemoteObject.exportObject().
  • 在绑定之前自己手动导出

注意不是两者。