【问题标题】:Java RMI - Returning a reference to another remote object on server from a remote method on server, to clientJava RMI - 从服务器上的远程方法返回对服务器上另一个远程对象的引用到客户端
【发布时间】:2014-11-21 05:40:52
【问题描述】:

问题在于使用 RMI 进行客户端-服务器通信 - 我不知道如何将运行在服务器空间上的对象(或对对象的引用)之类的东西返回给我可以使用其方法的客户端然后从该客户端调用。我需要这个,因为基本的 RMI 示例只是将字符串或 int 等数据从服务器传递到客户端,而不是在服务器上运行的完全序列化的对象。

我发现的唯一类似问题是is it possible to pass by reference in RMI?

我想要做的是:

服务器:创建和导出远程对象(实现远程接口)。客户端调用此远程对象的方法。该方法反过来创建另一个类的新对象并返回对它的引用。

客户端:从 Java RMI 注册表获取存根。根据远程对象规范调用存根并获取另一个对象的句柄,然后使用此句柄调用在服务器空间中运行的该对象的方法。

这甚至可能吗?我也可能在这里遗漏一两个概念。

代码:

//Hello.java
import java.rmi.Remote;
import java.rmi.RemoteException;


public interface Hello extends Remote {
    public target sayHello() throws RemoteException;
}






//Server.java   
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Server implements Hello {
public target t;
public Server() {}

public target sayHello() {
    target t = new target(1,"target");
    return t;
}

public static void main(String args[]) {
Hello stub = null;
Registry registry = null;

try {
    Server obj = new Server();
    stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);
    registry = LocateRegistry.getRegistry();
    registry.bind("Hello", stub);

    System.err.println("Server ready");
} catch (Exception e) {
    try{
    registry.unbind("Hello");
    registry.bind("Hello",stub);
        System.err.println("Server ready");
    }catch(Exception ee){
    System.err.println("Server exception: " + ee.toString());
        ee.printStackTrace();
    }
}
}
}




//Client.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Client {

private Client() {}

public static void main(String[] args) {


String host = (args.length < 1) ? null : args[0];
try {

    Registry registry = LocateRegistry.getRegistry(host);
    Hello stub = (Hello) registry.lookup("Hello");
    target response = stub.sayHello(); //**Error string cannot be converted to type** target while compiling with javac
    System.out.println("response: " + response.getdesc());
} catch (Exception e) {
    System.err.println("Client exception: " + e.toString());
    e.printStackTrace();
}
}
}




//target.java
import java.io.*;

class target implements Serializable {
int data;
String desc;

target(int data, String desc) {
this.data = data;
this.desc = desc;
}

public int getdata() { return data; }
public String getdesc() { return desc; }

public void setdata(int data) { this.data = data; }
public void setdesc(String desc) { this.desc = desc; }
}

【问题讨论】:

    标签: java serialization rmi rpc


    【解决方案1】:

    您只需要确保您返回的对象是一个导出的远程对象,并且它在返回它的远程接口中引用它自己的远程接口名称,而不是它自己的名称。

    【讨论】:

      【解决方案2】:

      抱歉,我不明白您的整个问题,但 RMI 有 2 个选项。

      如果您对特定的类方法感兴趣,可以使用接口对在服务器的方法“sayHello”上创建的对象进行代理

      类似这样的:

      public interface TargetInterface extends Remote {
          public void doSomething() throws RemoteException;
      }
      
      public Target extends UnicastRemoteObject implements TargetInterface {
          private static final long serialVersionUID = 7204253532056038242L; 
          public Target() throws RemoteException {
             super();
          }
      
          @Override
          public void doSomething() throws RemoteException {
              //TODO
          }
      }
      

      在你的方法中你应该返回 TargetInterface。

      否则,如果你想知道所有的对象结构,你的对象应该是可序列化的:

        public class Target implements Serializable
      

      您正在寻找类似的东西吗?

      【讨论】:

        猜你喜欢
        • 2014-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-24
        • 1970-01-01
        • 2018-10-13
        • 1970-01-01
        • 2018-03-09
        相关资源
        最近更新 更多