java RMI-如何通过远程方法传递远程对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/774828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RMI-How does passing a remote object through a remote method work?
提问by Bill K
As I understand it, once I've set up an RMI communications link between two systems, I can pass an object that implements "Remote" into one of the remote methods that takes an object of that type and the far-end will just get the remote interface for the new object (in other words, it will become a new remote connection as opposed to just serializing the object over.)
据我了解,一旦我在两个系统之间建立了 RMI 通信链接,我就可以将实现“Remote”的对象传递到采用该类型对象的远程方法之一中,而远端只会得到新对象的远程接口(换句话说,它将成为一个新的远程连接,而不是仅仅序列化对象。)
Is this correct?
这个对吗?
If so, I assume this has something to do with the method signatures--but I'd like to know exactly how it determines that it should create a new remote object instead of simply serializing the entire object.
如果是这样,我认为这与方法签名有关——但我想确切地知道它是如何确定它应该创建一个新的远程对象而不是简单地序列化整个对象的。
This is really hard to put into words. Let me try this:
这真的很难用语言表达。让我试试这个:
Let's say I have a client and a server system. On the server system I create and publish an RMI object, on the Client system I retrieve the interface and can interact with the Server system.
假设我有一个客户端和一个服务器系统。在服务器系统上我创建并发布一个 RMI 对象,在客户端系统上我检索接口并可以与服务器系统交互。
Client Server
Object1 RemoteIface ---- Object1 Implementation
So far so good. On Client, Object1.remoteMethod() will execute on Server (after serializing over the parameters).
到现在为止还挺好。在客户端上, Object1.remoteMethod() 将在服务器上执行(在序列化参数之后)。
Now, here's the question, on the client I execute code like this:
现在,问题来了,在客户端我执行这样的代码:
Object2 object2=new object2(); // Also a remote object
object1.send(object2);
As I understand it, at that point, my system will have a new communications mechanism:
据我了解,到那时,我的系统将有一个新的通信机制:
Client Server
Object1 RemoteIface ----- Object1 Implementation
Object2 Implementation ----- Object2 RemoteIface
At this point, if the server calls a method on Object2, that method will actually be executed on the client.
此时,如果服务器调用 Object2 上的方法,该方法实际上会在客户端执行。
I'm wondering at what point the system decides to do this rather than just serializing it (as it would with any non-remote object).
我想知道系统在什么时候决定这样做,而不是仅仅序列化它(就像任何非远程对象一样)。
Or am I completely wrong and it just serializes it over and I need some kind of "getRemoteInterface()" method call to actually create the remote call?
或者我完全错了,它只是将它序列化了,我需要某种“getRemoteInterface()”方法调用来实际创建远程调用?
采纳答案by Charlie Martin
It's what's called the Proxy Pattern. The thing instantiated at the remote end has generated code to transfer values and activate methods.
这就是所谓的代理模式。在远程端实例化的事物已生成用于传输值和激活方法的代码。
java.rmi.Remote itself is just a "tagging interface". You need an implementation to do the actual work. Have a look at this JavaCamp tutorialfor more.
java.rmi.Remote 本身只是一个“标记接口”。您需要一个实现来完成实际工作。查看此 JavaCamp 教程了解更多信息。
Update: okay, going the other direction, yes, you need to serialize the object and pass if over the wire. Probably the best thing is to work through the Java Tutorial on RMIfirst. But, basically, Serializable is another tagging interface that tells Java the object is prepared to be turned into an internal string format, kind of like XML or YAML. That format can be "rehydrated" into a matching object at the receiving end so long as the class files for that object are available.
更新:好的,转到另一个方向,是的,您需要序列化对象并通过网络传递。可能最好的办法是先学习RMI 上的Java 教程。但是,基本上,Serializable 是另一个标记接口,它告诉 Java 对象已准备好转换为内部字符串格式,有点像 XML 或 YAML。只要该对象的类文件可用,该格式就可以在接收端“再水化”为匹配的对象。

