是否可以通过RMI中的引用传递? [英] is it possible to pass by reference in RMI?

查看:102
本文介绍了是否可以通过RMI中的引用传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读过有关使用RMI传递变量的各种文章。

I have read various articles about passing variables around using RMI.

他们中的一些人说不可能通过RMI中的引用传递变量。例如:这一个 this this

Some of them say that it is impossible to pass variables by references in RMI. e.g.: this one and this one

虽然其他人说有可能的。例如:
这一个这一个这个

While others says that it is possible. e.g.: this one, this one and this one

任何人都可以清楚这一点吗? :)

can anyone clear this up please? :)

推荐答案

重要说明:如果通过引用传递意味着修改方法内部的参数值和调用者中的原始变量,你不能。如果您要做的是将对引用的副本传递给对象,则允许该方法与您的某个对象进行交互...是的,您可以。答案探讨了第二种选择。

Important note: if pass by reference means modifying argument value inside the method and change original variable in caller, you can't. If what you want to do is passing a copy of a reference to an object, to allow the method interact with some object of yours... yes you can. The answer explores that second option.

是的。但它必须是一个RMI对象。在这种情况下,RMI存根将通过复制传递。

Yes. But it has to be an RMI object. In that case a RMI stub will be passed by copy.

RMI传递参数并以两种方式返回值: / p>

RMI passes arguments and return values two ways:


  1. 复制整个对象

  2. 发送远程引用(它有如果它是一个参考远程!)。

  1. copy the full object
  2. send a remote reference (it has to be remote if its a reference!).

样本

猜猜我们有服务。它是一个RMI对象,通过RMI注册表发布,因此可以访问客户端。客户端可以在其上调用一个方法(创建一些东西),服务想要返回对新创建的对象的引用。不是序列化副本,而是对服务器内存空间中创建的对象的引用。

Guess we have a Service. It's a RMI object, published through the RMI registry, so it's accesible to clients. Client can call a method on it (to create something) and the service wants to return a reference to that newly created object. Not a serialized copy but a reference to the created object in server memory space.

import java.rmi.Remote;
import java.rmi.RemoteException;

// normally published object
public interface MyService extends Remote
{
    // creates something and return a "handle" to it
    public MyHandle createX(SomeSerializableObj param1) throws RemoteException;
}

// interface for object that the service will return a reference to...
public interface MyHandle extends Remote
{
    void doOne();
    void doTwo();
}

在这个例子中你可以:

创建MyService的实现并发布它

Registry registry = LocateRegistry.createRegistry(port);
MyService stub = (MyService) UnicastRemoteObject.exportObject(server, 0);
registry.bind("myService", stub);`

然后,某些RMI客户端可以获得对它的引用

Registry registry = LocateRegistry.getRegistry(server, port);
MyService serv1 = (MyService) registry.lookup("myService");

并且对服务对象的引用可以获得对其他RMI对象的引用。 / em>

and with a reference to the service object could obtain a reference to other RMI object.

MyHandle handle1 = serv1.createX(...);
handle1.doOne()

在这个例子中,方法参数是序列化的(它应该是一个Serializable类)
而返回的对象是对服务器中创建的对象的RMI引用。

In this example the method argument is serialized (it should be a Serializable class) while the returned object is a RMI reference to an object created in the server.

createX(...)的实现可能是:

The implementation of createX(...) could be:

public MyHandle createX(...) {
   MyHandle handle = new MyHandleImpl(); // create an implementation
   createdHandlers.add(handle); // add it to some structure (just a sample)
   return handle; // return the implementation. RMI will send to client a RMI reference to this very instance
}

这篇关于是否可以通过RMI中的引用传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆