简体   繁体   中英

Java RMI - how to send an object for callback from client

I am using RMI, I have Server Client program.

Client accesses some methods from servers.

However in one of the method I want to pass a client local object, using whose reference server will execute callback (which will inturn be executed on client), at least this is what I want, but may be I am doing it wrong.

at client

---------------------------------------------------------------------------------
   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
    }
}

Basically it's trying to tell you it has no access to the remote code. It's trying to deserialize a class for which is does not have a definition. The classes are loaded from the "code base" by the client. They are not loaded by the client over the RMI connection, so they have no way to serialize this class which it knows nothing about.

The server should specify the system property "java.rmi.server.codebase" when you start the JVM on the command line, or set it in code before trying to run your RMI client. The value must be a URL that is accessible to the client and it should point at the necessary classes to be loaded. Usually I specify a .jar file using the "file://" protocol handler.

The opposite is also true: If the client returns classes the server must deserialize, the server must set the code base property to a URL that is accessible to the server containing the returned classes. It's usually best to have a .jar of your DAO objects and their interfaces which is shared by both client and server to avoid any confusion about which classes need to go where.

You haven't exported the callback object, so it is being serialized instead of passed as a remote reference, and the peer doesn't have the implementation class so it gets this error. Make it extend UnicastRemoteObject.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM