RMI问题 - 客户端在哪里获取尚未添加到注册表的远程类的定义? [英] RMI Question - Where do clients get a definition for remote classes that have not been added to registry?

查看:144
本文介绍了RMI问题 - 客户端在哪里获取尚未添加到注册表的远程类的定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法创建了一个RMI应用程序,它完成了我需要它做得非常成功的事情,但是我很难解决客户端获取远程对象定义的问题。例如:



我有一个服务器用rmiregistry注册自己(允许客户端调用方法)。



UnicastRemoteObject.exportObject(new Server(),0);



正在运行 reg.list()确认我的服务器确实已添加到注册表中。我有另一个远程对象(rObj)在与服务器相同的JVM上运行。这不会添加到注册表中。



在我的客户端中,我可以通过在rmiregistry中查找 Server 来获取我的Server类的定义: / p>

reg.lookup(服务器)



之后可以自由创建rObj的实例。我的问题的关键是,我的客户端在哪里获得了rObj的定义,即使它从未被添加到注册表中。



我知道它必须来自服务器那样的存储类和接口的位置。与 Server 的连接是否会自动打开管道以接收其他远程类?



如果是这样,客户端如何知道在服务器上查找远程类。是否将服务器视为客户端类路径的扩展(它将在服务器上检查不在其自己的类路径中的类)?

解决方案

首先,要意识到没有必要从服务器设置动态类加载以使用RMI。如果您将接口和实现编译到客户端和服务器jar中,那么一切都会正常工作。这就是我几乎总是实现RMI的方式。



如果你有充分的理由从服务器动态加载类,你需要设置一个HTTP服务器某处有接口和实现类(最好是在jar文件中,尽管类目录也可以工作)。这不会作为RMI的一部分自动发生,您需要构建jar并将它们放在Web服务器上的某个位置。然后使用指示此jar文件的URL的系统属性启动客户端:

  -Djava.rmi.server.codebase = http ://webline/public/mystuff.jar 

这里有详细解释: http://download.oracle.com/javase/1.5.0/ docs / guide / rmi / codebase.html


Ive managed to create an RMI application that does what i need it to do quite succesfully, but im having a bit of trouble getting my head around where client obtains definitions for remote objects. for example:

I have a server that registers itself with the rmiregistry (allowing clients to call methods on it).

UnicastRemoteObject.exportObject(new Server(), 0);

running reg.list() confirms that my server has indeed been added to the registry. I have another remote object (rObj) running on the same JVM as the server. This is not added to the registry.

In my client, i can get the definition of my Server class by looking up Server in the rmiregistry:

reg.lookup("Server")

after this can freely create instances of rObj. The crux of my question is, where does my client get a definition for rObj even though its never been added to registry.

I know it must come from the server as thats where the class and interface are stored. Does the connection to Server automatically open the pipe for other remote classes to received?

If so, how does the client know to look on the server for the remote class. Is the server treated almost as an extension to the clients classpath (it will resort to checking the server for classes that arent in its own classpath)?

解决方案

First of all, realize that it's not necessary to set up dynamic classloading from the server in order to use RMI. If you compile the interface and implementation into both the client and server jars, then everything will work fine. That is how I've almost always implemented RMI.

If you have a good reason for loading the classes dynamically from the server, you'll need to set up an HTTP server somewhere that has the interfaces and implementation classes (preferably in a jar file, although a class directory will work too). This doesn't happen automatically as part of RMI, you need to build the jars and put them somewhere on your web server. Then launch the client with a system property indicating the URL to this jar file:

-Djava.rmi.server.codebase=http://webline/public/mystuff.jar

This is explained in full detail here: http://download.oracle.com/javase/1.5.0/docs/guide/rmi/codebase.html

这篇关于RMI问题 - 客户端在哪里获取尚未添加到注册表的远程类的定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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