我有一个RMI的远程接口:
public interface JMXManager extends Remote {
public MFSMBeanServerConnection getMBeanServerConnection(String className)
throws RemoteException;
}
}
我为了序列化MBeanServerConnection而创建的 MFSMBeanServerConnection
和MFSMBeanServerConnectionImpl
:
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服务器:
我做错了什么? 如何序列化javax.management.MBeanServerConnection以便我可以将它与远程接口一起使用?
答案 0 :(得分:1)
我认为序列化MBeanServerConnection是一个坏主意,因为它应该存储大量的运行时信息/一些在反序列化时无法使用或无效的信息。
它认为这是其所有已知的SubInterfaces(MBeanServer,MBeanServerForwarder)也不实现Serializable的原因。