RMI 服务器:rmiregistry 或 LocateRegistry.createRegistry
对于服务器端的RMI,我们需要启动rmiregistry
程序,还是只调用LocateRegistry.createRegistry
? 如果两者都可以的话,各有什么优点和缺点?
For RMI on server-side, do we need to start rmiregistry
program, or just call LocateRegistry.createRegistry
?
If both are possible, what are the advantages and disadvantages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您正在编写一个独立的 java 应用程序,您可能希望启动自己的 rmiregistry,但如果您正在编写一个显然在 J2EE 容器内运行的 J2EE 应用程序,那么您希望“LocateRegistry”,因为应用程序服务器上已经有一个正在运行!
If you are writing a standalone java application you would want to start your own rmiregistry but if you are writing a J2EE app that obviously runs inside a J2EE container then you want to "LocateRegistry" as there is already one running on the app server!
如果您使用 Spring 导出 RMI 服务,它会自动启动注册表(如果注册表尚未运行)。 请参阅 RmiServiceExporter
If you use Spring to export your RMI services, it automatically starts a registry if one is not already running. See RmiServiceExporter
如果我们首先启动 rmiregistry,RmiServiceExporter 会将自身注册到正在运行的 rmiregistry。 在这种情况下,我们必须将系统属性“java.rmi.server.codebase”设置为可以找到“org.springframework.remoting.rmi.RmiInitationWrapper_Stub”类的位置。 否则,RmiServiceExporter 将不会启动并出现异常“
ClassNotFoundException 未找到类:org.springframework.remoting.rmi.RmiInitationWrapper_Stub; 嵌套异常是:...”
如果您的 rmi 服务器、rmi 客户端和 rmiregistry 可以访问同一文件系统,您可能希望系统属性自动配置为在共享文件系统上可以找到 spring.jar 的位置。以下实用程序类和 spring 配置显示了如何实现这一点。
上面的示例显示了仅当 rmi 服务器、rmi 客户端和 rmi 注册表可以访问同一文件系统时如何自动设置系统属性,或者 spring 代码库通过其他方法共享。 (例如HTTP),您可以修改CodeBaseResolver以满足您的需要。
If we start rmiregistry first, RmiServiceExporter would register itself to the running rmiregistry. In this case, we have to set the system property 'java.rmi.server.codebase' to where the 'org.springframework.remoting.rmi.RmiInvocationWrapper_Stub' class can be found. Otherwise, the RmiServiceExporter would not be started and got the exception "
ClassNotFoundException class not found: org.springframework.remoting.rmi.RmiInvocationWrapper_Stub; nested exception is: ..."
If your rmi server, rmi client and rmiregistry can access the same filesystem, you may want the system property to be automatically configured to where the spring.jar can be found on the shared filesystem. The following utility classes and spring configuration show how this can be achieved.
The above example shows how system property be set automatically only when rmi server, rmi client and rmi registry can access the same filesystem. If that is not true or spring codebase is shared via other method (e.g. HTTP), you may modify the CodeBaseResolver to fit your need.
它们是同一件事...
rmiregistry
是一个单独的程序,您可以从命令行或脚本运行它,而LocateRegistry.createRegistry
以编程方式执行相同的操作。根据我的经验,对于“真实”服务器,您将需要使用
rmiregistry
,这样您就知道无论客户端应用程序是否启动,它都始终在运行。createRegistry
对于测试非常有用,因为您可以根据需要从测试中启动和停止注册表。They're the same thing...
rmiregistry
is a separate program, which you can run from a command line or a script, whileLocateRegistry.createRegistry
does the same thing programatically.In my experience, for "real" servers you will want to use
rmiregistry
so that you know it's always running regardless of whether or not the client application is started.createRegistry
is very useful for testing, as you can start and stop the registry from your test as necessary.