我正在尝试使用RMI客户端 - 服务器通信 . 我写了以下类/接口:
-
接口RemoteInterface扩展了Remote
-
类HelloStub扩展UnicastRemoteObject实现RemoteInterface
-
类服务器,我绑定了远程obj
-
类客户端如下:
import java.rmi.*;
public class Client
{
public static void main(String[] args)
{
try
{
String globalName = "rmi//127.0.0.1:1099/hello";
RemoteInterface remoteObj = (RemoteInterface)Naming.lookup(globalName);
System.out.println(remoteObj.SayHello());
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
我不明白为什么我必须使用RemoteInterface接口进行查找?我不能使用HelloStub类,这是真正的远程obj吗?
谢谢再见 .
1 回答
如果您知道类名,则可以定义Stub类实例而不是RemoteInterface,并将查找结果转换为存根 .
但是,您将获得定义存根而不是接口的好处是什么?