RMI Review 1

来源:互联网 发布:js <%%> 编辑:程序博客网 时间:2024/06/13 10:41

1.4和1.5在编译RMI的时候有些不同。1,4在编译的时候要产生一个stub文件,这个文件也要发布到客户端去;而1.5就没有这个文件了。

RMI是一种基于借口的编程,也就是说当客户端要访问服务器的方法时,他并不能知道服务器类所提供的所有方法,它只能看见服务器类的接

口所提供的方法。如果一个服务器类想让客户端使用它的方法,就必须在接口中声明,而这个接口必须继承Remote接口,所有的方法必须抛

出RemoteException。客户端在使用的时候也是用接口来声明变量,而不是具体的实现类。

具体的服务端实现类必须继承RemoteServer,因为它提供了与stub打交道的机制,但是RemoteServer是一个抽象类,所以我们通常继承

的是他的子类: UnicastRemoteObject。有些情况下,因为服务器类已经继承了其他类而不能再继承UnicastRemoteObject,这时可

以用UnicastRemoteObject.exportObject(this, 0);

在1.5之前,必须用rmic来编译服务器类以产生stub文件。

在服务器端使用
Context namingContext = new InitialContext();
namingContext.bind("rmi:toaster", p1);
来绑定服务

在客户端:Product p = (Product) namingContext.lookup("rmi://yourserver.com:port/toaster");
默认端口号:1099

取得服务器所有绑定服务:
NamingEnumeration<NameClassPair> e = namingContext.list("rmi:");

发布RMI是比较麻烦的事,有人提供一个checklist以供检查:
Did you put the interface class files into the download and client directory?

Did you put the stub class files into the server directory (for JDK 1.4 or below)?

Did you include the dependent classes for each class? For example, in the next section, you will see

an interface Warehouse that has a method with a parameter of type Customer. Be sure to include

Customer.class whenever you deploy Warehouse.class.

When you started rmiregistry, was the CLASSPATH unset? Was the current directory free from class

files?

Did you use a policy file when starting the client? Does the policy file contain the correct server

names (or * to connect to any host)?

If you used a file: URL for testing, did you specify the correct file name in the policy file? Does

it end in a /- or //- ? Did you remember to use // for Windows file names?

Does your codebase URL end in a slash?