Java RMI 之 Introduction
注:主要內容參考官方文檔。同時已經有人把翻譯過Sun上RMI的lesson,如果想學習技術實例,可以查看,參考資源給出了鏈接。
Overview
Java Remote Method Invocation (Java RMI) enables the programmer to create distributed Java technology-based
to Java technology-based applications, in which the methods of remote Java objects can be invoked from
other Java virtual machines, possibly on different hosts. RMI uses object serialization to marshal and
unmarshal parameters and does not truncate types, supporting true object-oriented polymorphism.
使用Java RMI,可以創建分布式的Java應用程序,遠程的Java對象的方法可以被不同的主機中不同的Java虛擬機調用。RMI使用對象序列化傳遞參數,并且不會丟失類型,支持面向對象的多態性。
Introduction
1.1 Background(背景)
Distributed systems require that computations running in different address spaces, potentially on different
hosts, be able to communicate. For a basic communication mechanism, the JavaTM programming language
supports sockets, which are flexible and sufficient for general communication. However, sockets require the
client and server to engage in applications-level protocols to encode and decode messages for exchange, and the design of such protocols is cumbersome and can be error-prone.
An alternative to sockets is Remote Procedure Call (RPC), which abstracts the communication interface to the
level of a procedure call. Instead of working directly with sockets, the programmer has the illusion of
calling a local procedure, when in fact the arguments of the call are packaged up and shipped off to the
remote target of the call. RPC systems encode arguments and return values using an external data
representation, such as XDR.
RPC, however, does not translate well into distributed object systems, where communication between
program-level objects residing in different address spaces is needed. In order to match the semantics of
object invocation, distributed object systems require remote method invocation or RMI. In such systems, a
local surrogate (stub) object manages the invocation on a remote object.
The Java platform's remote method invocation system described in this specification has been specifically
designed to operate in the Java application environment. The Java programming language's RMI system assumes the homogeneous environment of the Java virtual machine (JVM), and the system can therefore take advantage
of the Java platform's object model whenever possible.
分布式系統要求計算能不同的地址空間上,也就是說在不同的主機上,要能進行通訊,對于基本的Java通訊機制,Java支持Sokets,對于一般的通訊來說Sockets足夠了,但是Sockets要求客戶端和服務器端使用應用級別的協議交換消息,并且這個協議比較笨重。
另外一個選擇就是RCP,RCP就是針對遠程調用級別抽象出一個通訊接口。而不是直接的使用sockets,參數的調用被包裝起來,使用時看起來就像本地調用過程,RCP系統編碼參數和返回值使用的是外部數據請求,比如XDR
在分布式系統中,程序級別的對象必須存在不同的地址空間中,然而RCP不能很好的支持。為了匹配語法上的對象調用,分布式系統要求使用RMI,在這樣一個系統中,一個代理(存根)對象管理一個遠程對象的調用。
規范中描述Java平臺RMI系統被是為Java應用環境特別設計。Java RMI系統假定其擁有相似的JVM環境
1.2 System Goals(系統目標)
The goals for supporting distributed objects in the Java programming language are:
* Support seamless remote invocation on objects in different virtual machines
* Support callbacks from servers to applets
* Integrate the distributed object model into the Java programming language in a natural way while
retaining most of the Java programming language's object semantics
* Make differences between the distributed object model and local Java platform's object model apparent
* Make writing reliable distributed applications as simple as possible
* Preserve the type-safety provided by the Java platform's runtime environment
* Support various reference semantics for remote objects; for example live (nonpersistent) references,
persistent references, and lazy activation
* Maintain the safe environment of the Java platform provided by security managers and class loaders
在Java中支持分布式對象的目標如下:
* 支持不同的虛擬機之間無縫的調用遠程對象。
* 支持從服務器端回執給applets
* 保持大部分java對象原有的語義,以一種很自然的方式把分布式對象模型整合于Java語言
* 區分分布式對象模型和本地平臺的對象模型
* 盡可能簡單的構建可靠的分布式應用
* 有Java運行環境保護類型(Type)安全
* 遠程對象支持各種語義上的引用(reference),比如:live references,持久引用和延遲激活。
使用安全管理器(security managers)和類加載器(class loaders)保護java平臺的環境安全。
Java Distributed Object Model
2.1 Distributed Object Applications
RMI applications are often comprised of two separate programs: a server and a client. A typical server
application creates a number of remote objects, makes references to those remote objects accessible, and
waits for clients to invoke methods on those remote objects. A typical client application gets a remote
reference to one or more remote objects in the server and then invokes methods on them. RMI provides the
mechanism by which the server and the client communicate and pass information back and forth. Such an
application is sometimes referred to as a distributed object application.
Distributed object applications need to:
* Locate remote objects
Applications can use one of two mechanisms to obtain references to remote objects. An application can
register its remote objects with RMI's simple naming facility, the rmiregistry, or the application can pass
and return remote object references as part of its normal operation.
* Communicate with remote objects
Details of communication between remote objects are handled by RMI; to the programmer, remote communication
looks like a standard method invocation.
* Load class bytecodes for objects that are passed as parameters or return values
Because RMI allows a caller to pass objects to remote objects, RMI provides the necessary mechanisms for
loading an object's code as well as transmitting its data.
RMI應用程序經常是由兩個單獨的程序組成:服務器端和客戶端,
一個典型的服務器端應用創建一系列遠程對象,并使其對象的引用可訪問,等客戶端來調用遠程對象的方法。一個典型的客戶端獲取一個服務器端遠程對象(一個或多個)的遠程引用,然后調用他們的方法。RMI提供機制讓服務器端和客戶端進行通訊和來回傳遞信息。像這樣一個應用就是分布式應用。
分布式應用必須:
* 查找遠程對象
應用程序下面兩者之一的機制來獲得遠程對象的引用。一個應用程序可以使用RMI的簡單命名在注冊處注冊它的遠程對象,或者應用程序可以像普通操作一樣傳遞和返回遠程對象的引用。
*與遠程對象進行通訊、
遠程對象之間的通訊具體操作有RMI來提供,使用時只要當作正常的方法調用即可。
* 加載對象的字節碼使其可以作為參數或返回值傳遞。
因為RMI允許調用者傳遞對象到遠程對象,RMI提供必要的機制加載對象的代碼和傳輸它數據。
The illustration below depicts an RMI distributed application that uses the registry to obtain references to
a remote object. The server calls the registry to associate a name with a remote object. The client looks
up the remote object by its name in the server's registry and then invokes a method on it. The illustration also shows that the RMI system uses an existing web server to load bytecodes of classes written in the Java programming language, from server to client and from client to server, for objects when needed. RMI can load
class bytecodes using any URL protocol (e.g., HTTP, FTP, file, etc.) that is supported by the Java platform.
下
面這幅圖描述了一個RMI分布式應用程序,用戶使用registry獲得一個遠程對象的引用,Server端調用registry使用名字和一個遠程對象
關聯。客戶端在Server的registry中查找遠程對象的引用并調用其方法。同時這個圖也描述了,在需要時,RMI系統使用已經存在的
web server在client端和Server端之間加載Java Class的字節碼。RMI可以使用URL協議(如e.g., HTTP,
FTP, file, etc),這些Java平臺都支持

2.3 The Distributed and Nondistributed Models Contrasted
The Java SE platform's distributed object model differs from the Java SE platform's object model in these
ways:
* Clients of remote objects interact with remote interfaces, never with the implementation classes of
those interfaces.
* Non-remote arguments to, and results from, a remote method invocation are passed by copy rather than by
reference. This is because references to objects are only useful within a single virtual machine.
* A remote object is passed by reference, not by copying the actual remote implementation.
* The semantics of some of the methods defined by class java.lang.Object are specialized for remote
objects.
* Since the failure modes of invoking remote objects are inherently more complicated than the failure
modes of invoking local objects, clients must deal with additional exceptions that can occur during a remote
method invocation.
Java SE平臺的分布式對象模型普通對象模型的區別:
* 遠程對象的Client端只與遠程接口進行交互,不觸及接口的實現。
* 不管是參數傳遞還是值返回,遠程方法調用時傳遞的都是值的Copy而不是引用,因為一個對象的引用只有在單個虛擬機中有用。
* 遠程對象傳遞的是引用,而不是copy實際的遠程實現。
* Java.lang.Object為遠程對象定義了一些專門的方法
* 調用遠程對象天生就比調用本地方法,在失敗mode上要復雜。所以客戶端要處理遠程調用中出現的額外的異常
參考資源:
RMI lesson翻譯: http://www.tkk7.com/jht/archive/2007/05/09/116216.html
官方文檔:http://java.sun.com/javase/6/docs/technotes/guides/rmi/index.html