2014-03-04 68 views
0

我正在使用RMI,我有Server Client程序。Java RMI - 如何從客戶端發送回調對象

客戶端從服務器訪問某些方法。

但是,在我想傳遞一個客戶端本地對象的方法之一中,使用其參考服務器將執行回調(這將在客戶端執行),至少這是我想要的,但可能是我在做它錯了。

在客戶

--------------------------------------------------------------------------------- 
    server.setLock("/hello.txt", 99, 125,Locker.Lock_type.WRITE,"Shubham Verma",true, new CallBackProcess()); 
--------------------------------------------------------------------------------- 

CallBackProcess implements an interface which is presented on both the ends. 

But I get this error  

Exception in thread "main" java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: scality.CallBackProcess (no security manager: RMI class loader disabled) 
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:353) 
    at sun.rmi.transport.Transport$1.run(Transport.java:177) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173) 
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:701) 
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273) 
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251) 
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160) 
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194) 
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148) 
    at com.sun.proxy.$Proxy1.setLock(Unknown Source) 
    at scality.Client.main(Client.java:31) 
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: scality.CallBackProcess (no security manager: RMI class loader disabled) 
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:313) 
    at sun.rmi.transport.Transport$1.run(Transport.java:177) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173) 
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:701) 
Caused by: java.lang.ClassNotFoundException: scality.CallBackProcess (no security manager: RMI class loader disabled) 
    at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:393) 
    at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:183) 
    at java.rmi.server.RMIClassLoader$2.loadClass(RMIClassLoader.java:637) 
    at java.rmi.server.RMIClassLoader.loadClass(RMIClassLoader.java:264) 
    at sun.rmi.server.MarshalInputStream.resolveClass(MarshalInputStream.java:220) 
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1611) 
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1516) 
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1770) 
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1349) 
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369) 
    at sun.rmi.server.UnicastRef.unmarshalValue(UnicastRef.java:324) 
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:307) 
    ... 9 more 

public interface Callback extends Remote{ 
    void execute() throws RemoteException; 
} 


public class CallBackProcess implements Callback, Serializable{ 
    public static int test = 0; 
    @Override 
    public void execute() { 
     // need to do something here 
    } 
} 

回答

0

基本上它試圖告訴你它具有遠程代碼的訪問權限。它試圖反序列化一個沒有定義的類。這些類是由客戶端從「代碼庫」加載的。它們沒有被客戶端通過RMI連接加載,所以他們無法序列化它不知道的類。

當您啓動命令行JVM服務器應指定系統屬性「java.rmi.server.codebase」,或者試圖運行RMI客戶端之前設置它的代碼。該值必須是可供客戶端訪問的URL,並且它應該指向要加載的必要類。通常我使用「file://」協議處理程序指定一個.jar文件。

相反也是如此:如果客戶端返回的類服務器必須反序列化,服務器必須的代碼庫屬性設置爲是包含返回類的服務器訪問的URL。通常最好有一個.jar DAO對象和它們的接口,它們被客戶端和服務器共享,以避免對哪些類需要去哪裏產生混淆。

+0

這是得到異常的服務器。你有這個回到前面和顛倒,你已經錯過了根本原因。 – EJP

0

您還沒有出口的回調對象,因此它被序列化,而不是作爲遠程引用傳遞,和同行沒有實現類,因此得到這個錯誤。使其擴展UnicastRemoteObject。

相關問題