《cis6930javaremotemethodinvocation-indiana…:cis6930java远程方法调用-印第安娜…》由会员分享,可在线阅读,更多相关《cis6930javaremotemethodinvocation-indiana…:cis6930java远程方法调用-印第安娜…(145页珍藏版)》请在金锄头文库上搜索。
1、dbccsit.fsu.edu,1,CIS 5930-04 Spring 2001,http:/aspen.csit.fsu.edu/it1spring01 Instructors: Geoffrey Fox , Bryan Carpenter Computational Science and Information Technology Florida State University Acknowledgements: Nancy McCracken Syracuse University,Part 5: Java RMIRemote Method Invocation,dbccsit.
2、fsu.edu,2,Remote Method Invocation,Java RMI is a mechanism that allows a Java program running on one computer (e.g., the client) to apply a method to an object on a different computer (e.g., the server). In itself, the syntax of the remote invocation looks exactly like an ordinary Java method invoca
3、tion. The remote method call can be passed arguments computed in the context of the local machine. It can return arbitrary values computed in the context of the remote machine. The RMI system transparently forwards these arguments and results. RMI is an implementation of the of the Distributed Objec
4、t programming modelsimilar to CORBA, but simpler, and specialized to the Java language,dbccsit.fsu.edu,3,Example,Local Machine,Remote Machine,res = obj.meth(arg) ;,ResType meth(ArgType arg) . . . return new ResImpl(. . .) ; ,Assume code running in the local machine holds a remote reference to an obj
5、ect obj on a remote machine:,obj,dbccsit.fsu.edu,4,Remote objectsthese are normal Java objects, but their class extends some RMI library class that incorporates support for remote invocation. Remote referencesobject references that effectively refer to remote objects, typically on a different comput
6、er. Remote interfacesnormal Java interfaces, that specify the “API” of a remote object. They should extend the marker interface, java.rmi.Remote. The remote interface must be known to both the local and remote code.,Central Components of Java RMI,dbccsit.fsu.edu,5,Registriesplaces where the local ma
7、chine initially looks to find a reference to a remote object. Serializationreduction of Java objects to a representation that can be communicated as a byte stream (for arguments and results). Dynamic class loadingneeded in various places. One example is when a remote method returns an object whose c
8、lass (e.g. ResImpl) was not previously known on the calling machine. Also used for stubssee later. Security managerused to control the behavior of code loaded from a remote host.,Some Supporting Technologies,dbccsit.fsu.edu,6,Related Approaches to Networking,Sockets Traditionally quite hard to progr
9、am, although the package makes it relatively easy to establish a socket network connection to another host. Communication takes place via streams, but you must define the detailed protocols for message exchange yourself. High-level compared with Unix sockets, but low-level compared to RMI. Remote P
10、rocedure Call (RPC) An earlier UNIX protocol to allow calling remote procedures. Programmers register their application with a host port mapper. Protocols for parameter-passing support a limited number of types. CORBA See next semesters course. . .,dbccsit.fsu.edu,7,References,Core Java 2, Volume II
11、, Chapter 5: “Remote Objects”. Java RMI, Troy Bryan Downing, IDG books, 1998. “Getting Started Using RMI”, and other documents, at: http:/ Started,dbccsit.fsu.edu,9,The Remote Interface,In RMI, a common remote interface is the minimum amount of information that must be shared in advance between “cli
12、ent” and “server” machines. It defines a high-level “protocol” through which the machines will communicate. A remote interface is an ordinary Java interface, which must extent the marker interface java.rmi.Remote. All methods in a remote interface must be declared to throw the java.rmi.RemoteExcepti
13、on exception.,dbccsit.fsu.edu,10,A Simple Example,A file MessageWriter.java contains the interface definition: import java.rmi.* ; public interface MessageWriter extends Remote void writeMessage(String s) throws RemoteException ; This interface defines a single remote method, writeMessage().,dbccsit
14、.fsu.edu,11,java.rmi.Remote,The interface java.rmi.Remote is a marker interface. It declares no methods or fields; however, extending it tells the RMI system to treat the interface concerned as a remote interface. In particular we will see that the rmic compiler generates extra code for classes that
15、 implement remote interfaces. This code allows their methods to be called remotely.,dbccsit.fsu.edu,12,java.rmi.RemoteException,Requiring all remote methods be declared to throw RemoteException was a philosophical choice by the designers of RMI. RMI makes remote invocations look syntactically like l
16、ocal invocation. In practice, though, it cannot defend from problems unique to distributed computingunexpected failure of the network or remote machine. Forcing the programmer to handle remote exceptions helps to encourage thinking about how these partial failures should be dealt with. See the influential essay: “A Note on Distributed Computing” by Waldo et al, republished in The Jini Specif