RMI的动态代理存根实现

发布于 2024-07-16 07:37:17 字数 245 浏览 7 评论 0原文

我想了解动态代理存根实现实际上是如何在幕后完成的。 根据我读到的内容,当导出远程对象时,如果没有找到预先生成的存根类,RMI 运行时将生成一个动态代理来充当存根。 然后,该存根会绑定到 RMI 注册表,稍后可由某个 RMI 客户端访问。

问题是:既然存根实际上是一个动态生成的代理,它的类定义在客户端是不可用的,那么为什么客户端仍然能够从 RMI 注册表检索存根呢? 幕后是否发生某种动态类加载,或者 RMI 是否使用另一种技术来解决这个问题?

I want to understand how the dynamic proxy stub implementation is actually done behind the scene. According to what I read, by the time a remote object is exported if no pre-generated stub class is found, the RMI runtime would generate a dynamic proxy to act as the stub. That stub is then bound to the RMI Registry and later accessible by some RMI client.

The question is: since the stub is actually a dynamically generated proxy, its class definition would not be available on the client side, then how come the client is still able to retrieve the stub from the RMI Registry? Is there some kind of dynamic class-loading happening behind the scene or does RMI use another technique to work-around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人事已非 2024-07-23 07:37:17

Java.lang.reflect.Proxy 是可序列化的,并且在 ObjectOutputStream 和 ObjectInputStream 中具有特殊支持。 基本上,仅序列化实现的接口和调用处理程序,并在反序列化期间从中构造一个新的动态代理。

Java.lang.reflect.Proxy is serializable and it has special support in ObjectOutputStream and ObjectInputStream. Basically just the interfaces implemented and the invocation handler are serialized, and a new dynamic proxy is constructed from that during deserialization.

回眸一笑 2024-07-23 07:37:17

RMI 确实使用动态类加载 - 类路径作为“类路径注释”与调用一起发送,客户端从中加载类。 您可以查看 RMI 实现以获取更多信息 - 它作为 JDK 源代码的一部分提供。 具体来说,是 ObjectOutputStream 和 RMIClassloader 类。

更新:RMI 不会启动 HTTP 服务器 - 事实上,您需要为此定制解决方案。 正如您提到的,其中之一可以是您运行的 HTTP 服务器,使类可以通过服务器使用,并在存根中传递带有 HTTP 服务器地址/端口的代码库,以便您的客户端可以下载它们。

RMI does use dynamic classloading - the classpath is sent alongwith the call as a 'classpath annotation' from which the client loads the class. You can look at the RMI implementation for more info - it's available as part of the JDK source. Specifically, the classes ObjectOutputStream and RMIClassloader.

Update: RMI does not start an HTTP server - in fact, you would need your custom solution for this. One of them as you mention can be an HTTP server that you run, make the classes available through the server, and pass the codebase with the HTTP server's address/port in your stubs, so that your clients can download them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文