Export inner object

旧街凉风 提交于 2019-12-02 19:26:25

问题


Lets say I have a server and a client. The client requests a service object from the server. That object is transmitted by copy but also provides a callback method (bar). If the client then asks the server to update that object (updateValue) the server will call bar() using RMI. I tried something like this, but i get an error when requsting the object from the server: "cannot assign instance of com.sun.proxy.$Proxy1 to field ServiceObjImpl.barWrapper of type ServiceObjImpl$BarWrapper in instance of ServiceObjImpl"

Service object interface:

public interface ServiceObject{
    public int foo();
    public void bar(int var) throws RemoteException;
}

Server interface

public interface Service extends Remote {
    public ServiceObject get() throws RemoteException;
    public void updateValue(int var) throws RemoteException;
}

Server implementation

public class Server extends UnicastRemoteObject implements Service {
    private static final long serialVersionUID = 247610230875286173L;
    private ServiceObject serviceObj = null;

    public Server() throws RemoteException {
       super();
       serviceObj = new ServiceObjImpl();
   }

    public void updateValue(int var){
        try {
            serviceObj.bar(var);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }


    public ServiceObject get() throws RemoteException {
        return serviceObj;
    }
 }

Service object implementation

class ServiceObjImpl implements ServiceObject, Serializable{
    private static final long serialVersionUID = 1576043218876737380L;
    private int data = 0;
    private BarWrapper barWrapper = new BarWrapper();

    private class BarWrapper extends UnicastRemoteObject implements Remote{
        private static final long serialVersionUID = -5451253032897838145L;

        public BarWrapper() throws RemoteException {
             super();
        }

        public void bar(int value){
            data = value;
        }
    }

    public ServiceObjImpl() throws RemoteException {
        super();
    }

    public int foo() {
        return data;
    }

    public void bar(int value) throws RemoteException {
        barWrapper.bar(value);
    }
}

Client implementation

public class Client{
    private Service server = null;
    private ServiceObject obj = null;

    public void get(){
        try {
            obj = server.get();
        } catch (RemoteException e) {
        }
    }

    public void updateValue(int value){
        try {
            server.updateValue(value);
        } catch (RemoteException e) {
        }
    }

     public void foo(){
         System.out.println(obj.foo());
    }
}

回答1:


BarWrapper doesn't implement any remote interfaces other than remote. But it is an exported remote object, so it is passed around as a stub, not as its own type. So the type at the client must be a type implemented by the stub, not the concrete type BarWrapper this is why you get the exception

Solution:

  • Define a new remote interface for it
  • make it implement that, and
  • change the declaration of the field barWrapper from type BarWrapper to the new remote interface type.


来源:https://stackoverflow.com/questions/28177802/export-inner-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!