| |
客户机接口 程序员在编写使用远程对象的 applet 或应用程序时,需要注意 java.rmi 包 中可用的 rmi 系统客户机可视接口。 4.1 远程接口 package java.rmi; public interface remote {} java.rmi.remote 接口用来识别所有远程接口;所有远程对象必须直接或间接 实现此接口。 实现类可以实现任意数目的远程接口,并可扩展其它远程实现类。rmi 提供一些 远程对象实现可以扩展的类,有助于远程对象的创建。这些类是 java.rmi.server.unicastremoteobject 和 java.rmi.activation.activatable。 有关如何定义远程接口的详细信息,参见“java.rmi.remote 接口”(2.4.1)一 节。 4.2 remoteexception 类 类 java.rmi.remoteexception 是许多在执行远程方法调用时可能发生的、与 通信有关的异常的通用超类。远程接口中的每种方法(也是一个接口)必须在其 throws 子句中列出 remoteexception(或其超类,如 java.io.ioexception 或 java.lang.exception)。 package java.rmi; public class remoteexception extends java.io.ioexception { public throwable detail; public remoteexception(); public remoteexception(string s); public remoteexception(string s, throwable ex); public string getmessage(); public void printstacktrace(); public void printstacktrace(java.io.printstream ps); public void printstacktrace(java.io.printwriter pw); } remoteexception 可用详细消息(即 s)和一个嵌套异常(即 ex,throwable) 进行构造。嵌套异常 ex 在构造函数的第三种形式中被指定为参数,通常是 rmi 调用过程中发生的基本 i/o 异常。 getmessage 方法返回异常的详细消息,包括嵌套异常(如果有)中的消息。 printstacktrace 方法在类 java.lang.throwable 中将被覆盖掉,以打印嵌套异 常的堆栈跟踪。 4.3 naming 类 java.rmi.naming 类提供存储和获得对远程对象注册服务程序中远程对象进行 引用的方法。naming 类中的方法以如下形式的,url 格式的 java.lang.string 作为其中的一个参数: //host:port/name 其中 host 是注册服务程序所在的主机(远程或本地),port 是注册服务程序 接收调用的端口号,name 是注 册表未作出解释的简单字符串。host 和 port 是可选的。如果省略了 host,则主机缺省值为本地 主机。如果省略了 port, 则端口缺省值为 1099,即 rmi 系统注册服务程序 rmiregistry 所用的“众所 周知”的端口。 为远程对象绑定名称即为稍后使用的远程对象关联或注册名称,可用于查询该远 程对象。可以使用 naming 类的 bind 或 rebind 方法将远程对象与名称相关联 。 当远程对象已用 rmi 注册服务程序在本地主机上进行过注册(绑定)后,远程 (或本地)主机上的调用程序就可以按名称查询远程对象、获得其引用,然后在 对象上调用远程方法。必要时,某一主机上运行的服务器可以共享一个注册服务 程序。 服务器的各个进程也可创建和使用自己的注册服务程序 (详细信息,参见 java.rmi.registry.locateregistry.createregistry 方法) 。 package java.rmi; public final class naming { public static remote lookup(string url) throws notboundexception, java.net.malformedurlexception, remoteexceptio n; public static void bind(string url, remote obj) throws alreadyboundexception, java.net.malformedurlexception, remoteexce ption; public static void rebind(string url, remote obj) throws remoteexception, java.net.malformedurlexception; public static void unbind(string url) throws remoteexception, notboundexception, java.net.malformedurlexce ptio n; public static string[] list(string url) throws remoteexception, java.net.malformedurlexception; } lookup 方法返回与名称的文件部分相关联的远程对象。如果名称未绑定到对象上 ,则抛出 notboundexception。 bind 方法将把指定名称绑定到远程对象上。如果该名称已绑定到某一对象上, 则抛出 alreadyboundexception。 rebind 方法总将名称绑定到对象上,无论该名称是否已绑定。原有绑定将丢失。 unbind 方法将取消名称和远程对象间的绑定。如果没有绑定,则抛出 notbound exception。 list 方法返回一个 string 对象的数组,该对象包含注册服务程序中绑定 url 的快照。 为了向注册服务程序查询其内容列表,只需要 url 上的主机名和端口信息;因此 ,url 的“file”部分将被忽略。 ---------------------------------------------------------------------- ---------- 注意 - 这些方法也可能抛出 java.rmi.accessexception。accessexception 表 示调用程序无执 行特定操作的权限。例如,只有运行注册服务程序的主机上的本地客户机才允许 执行 bind、rebind 和 unbind 操作。但任何非本地客户机都可调用 lookup 操作。
|
|