Java RMI - UnicastRemoteObject:UnicastRemoteObject.exportObject()和扩展UnicastRemoteObject有什么区别? [英] Java RMI - UnicastRemoteObject: what is the difference between UnicastRemoteObject.exportObject() and extends UnicastRemoteObject?
问题描述
我正在准备考试,我有一个问题,我希望有人可以回答我。
i'm preparing for an exam and I'm having a question that I hope someone here could answer me.
这是关于RMI和远程对象。我想知道为什么这两个实现之间存在很大差异。一个是扩展UnicastRemoteObject而另一个是将对象导出为UnicastRemoteObject。
It's about RMI and remote objects. I wonder why there is so much difference between these two implementations. one is extending the UnicastRemoteObject whereas the other is exporting the object as an UnicastRemoteObject.
我没有真正得到差异
接口:
public interface EchoI extends Remote {
public String echo() throws RemoteException
}
这是服务器代码(版本1):
This is the server code (version 1):
public class EchoImpl extends UnicastRemoteObject implements EchoI {
public EchoImpl {
super();
}
public static void main (String[] args) {
try {
LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
StoreHouse storehouseImpl = new StorehouseImpl();
Naming.rebind("//localhost/StoreHouse.SERVICE_NAME", storehouseImpl);
System.out.println("Server ready");
} catch (RemoteException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public String echo() {
return "echo";
}
}
这将是版本2:
public class EchoImpl implements EchoI {
public static void main (String[] args) {
EchoI echoService = new EchoImpl();
EchoI stub = (EchoI) UnicastRemoteObject.exportObject(echoService, 0);
Registry registry = LocateRegistry.getRegistry();
registry.bind("echoService", stub);
...
}
}
我的问题是:这两者有什么区别?
My question is: what is the difference between these two?
在第一个版本中,显式创建了注册表,而且远程对象是在重新绑定中创建的吗?
In thefirst version the registry is explicitly created, furthermore the remote object is created within a rebind?
我真的很好奇,为什么在第一个我需要自己创建注册表但不需要显式导出对象而只需使用 Naming重新绑定它
。该对象之前是否已绑定到注册表,或者我可以使用bind吗?如果对象之前没有绑定并且重新绑定已经完成,会发生什么?
I'm really curious, why in the first I need to create the registry myself but do not need to export the object explicitly and just rebind it using Naming
. Is that object already bound to the registry before, or could I use bind instead? And what happens, if the object was not previously bound and a rebind is excecuted?
在第二个版本中,注册表似乎已经创建了。
为什么绑定命名与绑定到注册表的命名相同?
In the second version, the registry seems to be already created. Why is binding to naming the same as binding to an registry directly?
这就是我的想法:
- 第一个类direclty实现了UnicastRemoteObject接口,这意味着在运行时创建了注册表,并且对象自动导出到RMI注册表。
- 由于对象已绑定到注册表,因此必须重新绑定而不是正常绑定。
- 后者明确地完成所有这些。
推荐答案
java.rmi.server.UnicastRemoteObject
用于导出远程使用Java远程方法协议(JRMP)获取对象并获取与远程对象通信的存根。
java.rmi.server.UnicastRemoteObject
is used for exporting a remote object with Java Remote Method Protocol (JRMP) and obtaining a stub that communicates to the remote object.
对于下面的构造函数和静态 exportObject
方法,获取正在导出的远程对象的存根...
For the constructors and static exportObject
methods below, the stub for a remote object being exported is obtained ...
你应该遵循 Javadoc
这篇关于Java RMI - UnicastRemoteObject:UnicastRemoteObject.exportObject()和扩展UnicastRemoteObject有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!