2017-03-23 84 views
-1

我正在開發一個使用以下JAVA RMI設置的分佈式系統。Java RMI ClassCast異常

public interface NodeOperations { 
/* RMI methods */ 
public FingerTable getFingerTable() throws RMIException; 
} 

public class Node implements NodeOperations { 
/* Implements all RMI methods */ 
/* Also holds a object of FingerTable */ 
FingerTable ft; 

} 

public class FingerTable { 
/* Holds a reference to Node class which has object of 'this' FingerTable */ 
Node self; 
} 

所有RMI通信都是通過NodeOperations接口中存在的方法完成的。有一種方法會返回特定節點的對象FingerTable。但是,當我從客戶端調用該方法時,我會遇到異常。我該如何解決這個問題?

java.lang.ClassCastException: cannot assign instance of com.sun.proxy.$Proxy5 to field chord.FingerTable.self of type chord.Node in instance of chord.FingerTable 
    at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:2133) 
    at java.io.ObjectStreamClass.setObjFieldValues(ObjectStreamClass.java:1305) 
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2237) 
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2155) 
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2013) 
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1535) 
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2231) 
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2155) 
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2013) 
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1535) 
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422) 
    at sun.rmi.server.UnicastRef.unmarshalValue(UnicastRef.java:326) 
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:175) 
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:227) 
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:179) 
    at com.sun.proxy.$Proxy5.getNodeInfo(Unknown Source) 
    at chord.NodeTest.testTopology(NodeTest.java:108) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
+0

即使我這麼認爲,但我已經使所有這些類可序列化。 – Nullpointer

+0

此代碼不會產生此異常。至少缺少兩件事。發佈真實的代碼。 – EJP

回答

1
  1. 如果Node旨在是遠程對象:

    • NodeOperations應該擴展Remote
    • Node應延伸UnicastRemoteObject或通過UnicastRemoteObject.exportObject()出口。
    • NodeOperations.getFingerTable()應宣佈投擲RemoteException
    • 您不應該有一個Node類型的字段:它應該是NodeOperations類型。

    根據該例外,Node已經是導出的遠程對象。所以這不是真正的代碼。

  2. 否則,即如果你不想Node是遠程對象,Node應該實現Serializable,聲明serialVersionUID構件等

  3. 你的類定義看起來比較圓。審查他們。