Why Does Simple RMI Server Need Codebase?

依然范特西╮ 提交于 2019-12-02 03:57:15

You are correct, you don't need to use the codebase feature. However that means that your remote interface, the classes it depends on, and the stub if you're using one must be available to both the client and the Registry on their classpaths. The exception you're getting indicates that the Registry doesn't have these classes on its CLASSPATH.

The reason it works this way, is that the RMI Registry is implemented itself using RMI. So when you call bind in one application, the actual implementation of Registry within the rmiregistry process will receive a stub object implementing all of your Remote interfaces just as with all RMI invocations passing references to remote objects.

Therefore the rmiregistry process needs access to all remote interfaces implemented by the object you bind but as you already have discovered, its actual contents is irrelevant as the registry will never invoke any methods on it. All it does is handing the remote stub to callers of lookup and in this case the stub is serialized and transmitted to the remote caller’s JVM where an equivalent stub is created, again, implementing all remote interfaces the stub (and hence the original object) has implemented.

So all that happens with the interfaces within the rmiregistry process (if you started it as stand-alone process) is that the information about the remote interfaces implemented by a remote object is recorded, by letting a stub implement them accordingly, and passed to all other remote applications performing a lookup.

In principle it would be possible to implement the entire registry differently by not using RMI and record the implemented interface without the need for their class files, however, that would be a huge effort as you would have to implement all the things you get for free when simply using RMI.

But note that, if you have only a single server, you can simplify everything by letting your server itself create the registry within its own JVM. Then you don’t need to start another process nor think about its classpath/codebase.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!