我有一个RMI的远程接口:

public interface JMXManager extends Remote {

    public MFSMBeanServerConnection getMBeanServerConnection(String className)
            throws RemoteException;
    }
}


我为了序列化MBeanServerConnection创建的MFSMBeanServerConnectionMFSMBeanServerConnectionImpl

public interface MFSMBeanServerConnection extends Serializable {
     public MBeanServerConnection getMBeanServerConnection();
}

public class MFSMBeanServerConnectionImpl implements MFSMBeanServerConnection {

    private static final long serialVersionUID = 1006978249744538366L;
    /**
     * @serial
     */
    private MBeanServerConnection mBeanServerConnection;

    public MFSMBeanServerConnectionImpl() {}

    public MFSMBeanServerConnectionImpl(MBeanServerConnection mBeanServerConnection) {
        this.mBeanServerConnection = mBeanServerConnection;
    }

    public MBeanServerConnection getMBeanServerConnection() {
        return mBeanServerConnection;
    }

    private void readObject(ObjectInputStream aInputStream) throws ClassNotFoundException,
            IOException {
        aInputStream.defaultReadObject();
        mBeanServerConnection = (MBeanServerConnection) aInputStream.readObject();
    }

    private void writeObject(ObjectOutputStream aOutputStream) throws IOException {
        aOutputStream.defaultWriteObject();
        aOutputStream.writeObject(mBeanServerConnection);
    }

    private void readObjectNoData() throws ObjectStreamException {

    }
}


在客户端,我有

JMXManager jmxm= (JMXManager) registry.lookup("JMXManager");
MFSMBeanServerConnection mfsMbsc = jmxm.getMBeanServerConnection(className);


在第二行,我得到一个异常:

java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:173)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:178)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:132)
at $Proxy0.getMBeanServerConnection(Unknown Source)


我的目标是创建一个RMI服务器:


由一个或多个存储其MBeanServerConnection的JMX服务器使用
客户端采用一个MBeanServerConnection并使用(操纵)其MBean


我做错了什么?
如何序列化javax.management.MBeanServerConnection以便可以将其与远程接口一起使用?

最佳答案

我认为序列化MBeanServerConnection是个坏主意,因为它应该存储很多运行时信息/某些信息,在反序列化时这些信息将不可用或无效。

认为这是所有已知的SubInterfaces(MBeanServer,MBeanServerForwarder)也未实现Serializable的原因。

10-02 00:38