资源预览内容
第1页 / 共145页
第2页 / 共145页
第3页 / 共145页
第4页 / 共145页
第5页 / 共145页
第6页 / 共145页
第7页 / 共145页
第8页 / 共145页
第9页 / 共145页
第10页 / 共145页
亲,该文档总共145页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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.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 invocation. 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 Object 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 object 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 computer. 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 machine 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 class (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 program, although the java.net 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 Procedure 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, Chapter 5: “Remote Objects”. Java RMI, Troy Bryan Downing, IDG books, 1998. “Getting Started Using RMI”, and other documents, at: http:/java.sun.com/products/jdk/rmi/,dbccsit.fsu.edu,8,Getting 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 “client” 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.RemoteException 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.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 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 local 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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号