Java RMI 之 Introduction
注:主要內(nèi)容參考官方文檔。同時(shí)已經(jīng)有人把翻譯過Sun上RMI的lesson,如果想學(xué)習(xí)技術(shù)實(shí)例,可以查看,參考資源給出了鏈接。
Overview
使用Java RMI,可以創(chuàng)建分布式的Java應(yīng)用程序,遠(yuǎn)程的Java對(duì)象的方法可以被不同的主機(jī)中不同的Java虛擬機(jī)調(diào)用。RMI使用對(duì)象序列化傳遞參數(shù),并且不會(huì)丟失類型,支持面向?qū)ο蟮亩鄳B(tài)性。
Introduction
1.1 Background(背景)
分布式系統(tǒng)要求計(jì)算能不同的地址空間上,也就是說在不同的主機(jī)上,要能進(jìn)行通訊,對(duì)于基本的Java通訊機(jī)制,Java支持Sokets,對(duì)于一般的通訊來說Sockets足夠了,但是Sockets要求客戶端和服務(wù)器端使用應(yīng)用級(jí)別的協(xié)議交換消息,并且這個(gè)協(xié)議比較笨重。
另外一個(gè)選擇就是RCP,RCP就是針對(duì)遠(yuǎn)程調(diào)用級(jí)別抽象出一個(gè)通訊接口。而不是直接的使用sockets,參數(shù)的調(diào)用被包裝起來,使用時(shí)看起來就像本地調(diào)用過程,RCP系統(tǒng)編碼參數(shù)和返回值使用的是外部數(shù)據(jù)請(qǐng)求,比如XDR
在分布式系統(tǒng)中,程序級(jí)別的對(duì)象必須存在不同的地址空間中,然而RCP不能很好的支持。為了匹配語法上的對(duì)象調(diào)用,分布式系統(tǒng)要求使用RMI,在這樣一個(gè)系統(tǒng)中,一個(gè)代理(存根)對(duì)象管理一個(gè)遠(yuǎn)程對(duì)象的調(diào)用。
規(guī)范中描述Java平臺(tái)RMI系統(tǒng)被是為Java應(yīng)用環(huán)境特別設(shè)計(jì)。Java RMI系統(tǒng)假定其擁有相似的JVM環(huán)境
1.2 System Goals(系統(tǒng)目標(biāo))
在Java中支持分布式對(duì)象的目標(biāo)如下:
* 支持不同的虛擬機(jī)之間無縫的調(diào)用遠(yuǎn)程對(duì)象。
* 支持從服務(wù)器端回執(zhí)給applets
* 保持大部分java對(duì)象原有的語義,以一種很自然的方式把分布式對(duì)象模型整合于Java語言
* 區(qū)分分布式對(duì)象模型和本地平臺(tái)的對(duì)象模型
* 盡可能簡(jiǎn)單的構(gòu)建可靠的分布式應(yīng)用
* 有Java運(yùn)行環(huán)境保護(hù)類型(Type)安全
* 遠(yuǎn)程對(duì)象支持各種語義上的引用(reference),比如:live references,持久引用和延遲激活。
使用安全管理器(security managers)和類加載器(class loaders)保護(hù)java平臺(tái)的環(huán)境安全。
Java Distributed Object Model
2.1 Distributed Object Applications
RMI應(yīng)用程序經(jīng)常是由兩個(gè)單獨(dú)的程序組成:服務(wù)器端和客戶端,
一個(gè)典型的服務(wù)器端應(yīng)用創(chuàng)建一系列遠(yuǎn)程對(duì)象,并使其對(duì)象的引用可訪問,等客戶端來調(diào)用遠(yuǎn)程對(duì)象的方法。一個(gè)典型的客戶端獲取一個(gè)服務(wù)器端遠(yuǎn)程對(duì)象(一個(gè)或多個(gè))的遠(yuǎn)程引用,然后調(diào)用他們的方法。RMI提供機(jī)制讓服務(wù)器端和客戶端進(jìn)行通訊和來回傳遞信息。像這樣一個(gè)應(yīng)用就是分布式應(yīng)用。
分布式應(yīng)用必須:
* 查找遠(yuǎn)程對(duì)象
應(yīng)用程序下面兩者之一的機(jī)制來獲得遠(yuǎn)程對(duì)象的引用。一個(gè)應(yīng)用程序可以使用RMI的簡(jiǎn)單命名在注冊(cè)處注冊(cè)它的遠(yuǎn)程對(duì)象,或者應(yīng)用程序可以像普通操作一樣傳遞和返回遠(yuǎn)程對(duì)象的引用。
*與遠(yuǎn)程對(duì)象進(jìn)行通訊、
遠(yuǎn)程對(duì)象之間的通訊具體操作有RMI來提供,使用時(shí)只要當(dāng)作正常的方法調(diào)用即可。
* 加載對(duì)象的字節(jié)碼使其可以作為參數(shù)或返回值傳遞。
因?yàn)镽MI允許調(diào)用者傳遞對(duì)象到遠(yuǎn)程對(duì)象,RMI提供必要的機(jī)制加載對(duì)象的代碼和傳輸它數(shù)據(jù)。
下 面這幅圖描述了一個(gè)RMI分布式應(yīng)用程序,用戶使用registry獲得一個(gè)遠(yuǎn)程對(duì)象的引用,Server端調(diào)用registry使用名字和一個(gè)遠(yuǎn)程對(duì)象 關(guān)聯(lián)。客戶端在Server的registry中查找遠(yuǎn)程對(duì)象的引用并調(diào)用其方法。同時(shí)這個(gè)圖也描述了,在需要時(shí),RMI系統(tǒng)使用已經(jīng)存在的 web server在client端和Server端之間加載Java Class的字節(jié)碼。RMI可以使用URL協(xié)議(如e.g., HTTP, FTP, file, etc),這些Java平臺(tái)都支持

2.3 The Distributed and Nondistributed Models Contrasted
Java SE平臺(tái)的分布式對(duì)象模型普通對(duì)象模型的區(qū)別:
* 遠(yuǎn)程對(duì)象的Client端只與遠(yuǎn)程接口進(jìn)行交互,不觸及接口的實(shí)現(xiàn)。
* 不管是參數(shù)傳遞還是值返回,遠(yuǎn)程方法調(diào)用時(shí)傳遞的都是值的Copy而不是引用,因?yàn)橐粋€(gè)對(duì)象的引用只有在單個(gè)虛擬機(jī)中有用。
* 遠(yuǎn)程對(duì)象傳遞的是引用,而不是copy實(shí)際的遠(yuǎn)程實(shí)現(xiàn)。
* Java.lang.Object為遠(yuǎn)程對(duì)象定義了一些專門的方法
* 調(diào)用遠(yuǎn)程對(duì)象天生就比調(diào)用本地方法,在失敗mode上要復(fù)雜。所以客戶端要處理遠(yuǎn)程調(diào)用中出現(xiàn)的額外的異常
參考資源:
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
注:主要內(nèi)容參考官方文檔。同時(shí)已經(jīng)有人把翻譯過Sun上RMI的lesson,如果想學(xué)習(xí)技術(shù)實(shí)例,可以查看,參考資源給出了鏈接。
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.
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,可以創(chuàng)建分布式的Java應(yīng)用程序,遠(yuǎn)程的Java對(duì)象的方法可以被不同的主機(jī)中不同的Java虛擬機(jī)調(diào)用。RMI使用對(duì)象序列化傳遞參數(shù),并且不會(huì)丟失類型,支持面向?qū)ο蟮亩鄳B(tài)性。
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.
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.
分布式系統(tǒng)要求計(jì)算能不同的地址空間上,也就是說在不同的主機(jī)上,要能進(jìn)行通訊,對(duì)于基本的Java通訊機(jī)制,Java支持Sokets,對(duì)于一般的通訊來說Sockets足夠了,但是Sockets要求客戶端和服務(wù)器端使用應(yīng)用級(jí)別的協(xié)議交換消息,并且這個(gè)協(xié)議比較笨重。
另外一個(gè)選擇就是RCP,RCP就是針對(duì)遠(yuǎn)程調(diào)用級(jí)別抽象出一個(gè)通訊接口。而不是直接的使用sockets,參數(shù)的調(diào)用被包裝起來,使用時(shí)看起來就像本地調(diào)用過程,RCP系統(tǒng)編碼參數(shù)和返回值使用的是外部數(shù)據(jù)請(qǐng)求,比如XDR
在分布式系統(tǒng)中,程序級(jí)別的對(duì)象必須存在不同的地址空間中,然而RCP不能很好的支持。為了匹配語法上的對(duì)象調(diào)用,分布式系統(tǒng)要求使用RMI,在這樣一個(gè)系統(tǒng)中,一個(gè)代理(存根)對(duì)象管理一個(gè)遠(yuǎn)程對(duì)象的調(diào)用。
規(guī)范中描述Java平臺(tái)RMI系統(tǒng)被是為Java應(yīng)用環(huán)境特別設(shè)計(jì)。Java RMI系統(tǒng)假定其擁有相似的JVM環(huán)境
1.2 System Goals(系統(tǒng)目標(biāo))
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
* 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中支持分布式對(duì)象的目標(biāo)如下:
* 支持不同的虛擬機(jī)之間無縫的調(diào)用遠(yuǎn)程對(duì)象。
* 支持從服務(wù)器端回執(zhí)給applets
* 保持大部分java對(duì)象原有的語義,以一種很自然的方式把分布式對(duì)象模型整合于Java語言
* 區(qū)分分布式對(duì)象模型和本地平臺(tái)的對(duì)象模型
* 盡可能簡(jiǎn)單的構(gòu)建可靠的分布式應(yīng)用
* 有Java運(yùn)行環(huán)境保護(hù)類型(Type)安全
* 遠(yuǎn)程對(duì)象支持各種語義上的引用(reference),比如:live references,持久引用和延遲激活。
使用安全管理器(security managers)和類加載器(class loaders)保護(hù)java平臺(tái)的環(huán)境安全。
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.
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應(yīng)用程序經(jīng)常是由兩個(gè)單獨(dú)的程序組成:服務(wù)器端和客戶端,
一個(gè)典型的服務(wù)器端應(yīng)用創(chuàng)建一系列遠(yuǎn)程對(duì)象,并使其對(duì)象的引用可訪問,等客戶端來調(diào)用遠(yuǎn)程對(duì)象的方法。一個(gè)典型的客戶端獲取一個(gè)服務(wù)器端遠(yuǎn)程對(duì)象(一個(gè)或多個(gè))的遠(yuǎn)程引用,然后調(diào)用他們的方法。RMI提供機(jī)制讓服務(wù)器端和客戶端進(jìn)行通訊和來回傳遞信息。像這樣一個(gè)應(yīng)用就是分布式應(yīng)用。
分布式應(yīng)用必須:
* 查找遠(yuǎn)程對(duì)象
應(yīng)用程序下面兩者之一的機(jī)制來獲得遠(yuǎn)程對(duì)象的引用。一個(gè)應(yīng)用程序可以使用RMI的簡(jiǎn)單命名在注冊(cè)處注冊(cè)它的遠(yuǎn)程對(duì)象,或者應(yīng)用程序可以像普通操作一樣傳遞和返回遠(yuǎn)程對(duì)象的引用。
*與遠(yuǎn)程對(duì)象進(jìn)行通訊、
遠(yuǎn)程對(duì)象之間的通訊具體操作有RMI來提供,使用時(shí)只要當(dāng)作正常的方法調(diào)用即可。
* 加載對(duì)象的字節(jié)碼使其可以作為參數(shù)或返回值傳遞。
因?yàn)镽MI允許調(diào)用者傳遞對(duì)象到遠(yuǎn)程對(duì)象,RMI提供必要的機(jī)制加載對(duì)象的代碼和傳輸它數(shù)據(jù)。
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.
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.
下 面這幅圖描述了一個(gè)RMI分布式應(yīng)用程序,用戶使用registry獲得一個(gè)遠(yuǎn)程對(duì)象的引用,Server端調(diào)用registry使用名字和一個(gè)遠(yuǎn)程對(duì)象 關(guān)聯(lián)。客戶端在Server的registry中查找遠(yuǎn)程對(duì)象的引用并調(diào)用其方法。同時(shí)這個(gè)圖也描述了,在需要時(shí),RMI系統(tǒng)使用已經(jīng)存在的 web server在client端和Server端之間加載Java Class的字節(jié)碼。RMI可以使用URL協(xié)議(如e.g., HTTP, FTP, file, etc),這些Java平臺(tái)都支持

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.
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平臺(tái)的分布式對(duì)象模型普通對(duì)象模型的區(qū)別:
* 遠(yuǎn)程對(duì)象的Client端只與遠(yuǎn)程接口進(jìn)行交互,不觸及接口的實(shí)現(xiàn)。
* 不管是參數(shù)傳遞還是值返回,遠(yuǎn)程方法調(diào)用時(shí)傳遞的都是值的Copy而不是引用,因?yàn)橐粋€(gè)對(duì)象的引用只有在單個(gè)虛擬機(jī)中有用。
* 遠(yuǎn)程對(duì)象傳遞的是引用,而不是copy實(shí)際的遠(yuǎn)程實(shí)現(xiàn)。
* Java.lang.Object為遠(yuǎn)程對(duì)象定義了一些專門的方法
* 調(diào)用遠(yuǎn)程對(duì)象天生就比調(diào)用本地方法,在失敗mode上要復(fù)雜。所以客戶端要處理遠(yuǎn)程調(diào)用中出現(xiàn)的額外的異常
參考資源:
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