A Short Java RMI Tutorial

22
+ A Short Java RMI Tutorial Albert Guo [email protected]

Transcript of A Short Java RMI Tutorial

Page 1: A Short Java RMI Tutorial

+

A Short Java RMI TutorialAlbert [email protected]

Page 2: A Short Java RMI Tutorial

+Agenda

Introducing RMI The RMI classes and interfaces The General RMI Architecture Demo – build up 4 required classes Demo – compiling and running the Weather Server

2

Page 3: A Short Java RMI Tutorial

+Introducing RMI

An RMI application is often composed of two separate programs, a server and a client. The server creates remotes objects and makes references to those objects accessible. Then it waits for clients to invoke methods on the objects. The client gets remote references to remote objects in the server and invokes methods on those remote objects.

Users use object as though it were a regular, local object Network connections happen automatically behind the scenes Java ‘serialization’ lets you pass complex data structures over

the network without writing code to parse and reconstruct them

3

Page 4: A Short Java RMI Tutorial

+Introducing RMI (cont.)

A distributed object application has to handle the following properties: Locate remote objects Communicate with remote objects Load class bytecodes for objects that are passed as

parameters or return values

4

Page 5: A Short Java RMI Tutorial

+Interfaces and Classes in the java.rmi package

5

Page 6: A Short Java RMI Tutorial

+The RMI classes and interfaces6

Page 7: A Short Java RMI Tutorial

+The General RMI Architecture

7

Page 8: A Short Java RMI Tutorial

+Remote object interface implemenation

8

Page 9: A Short Java RMI Tutorial

+RMI Architecture9

Page 10: A Short Java RMI Tutorial

+Runtime Architecture10

Page 11: A Short Java RMI Tutorial

+Stub and Skeleton

Package identifier of remote object

Package method identifier Marshall parameters Send package to server

skeleton

Unmarshall parameters Calls return value or

exception Marshall method return Send package to client stub

Stub Operation Skeleton Operation

11

Page 12: A Short Java RMI Tutorial

+Demo – build up 4 required classes

12

Page 13: A Short Java RMI Tutorial

+Class Diagram – four required classes

13

1

2

3 4

Page 14: A Short Java RMI Tutorial

+The interface for remote object14

The remote interface must satisfy the following conditions:• It must extend the interface Remote.• Each remote method declaration in the remote interface must throw RemoteException

Page 15: A Short Java RMI Tutorial

+The remote object implementation The class must extend UnicastRemoteObject and

implement the remote object interface The constructor should throw RemoteException

15

Page 16: A Short Java RMI Tutorial

+The remote object implementation

16

Page 17: A Short Java RMI Tutorial

+The RMI Server

The server builds an object and register it with a particular URL

Using Naming.bind(throw AlreadyBoundException if a previous binding exists) or Naming.rebuind (replace any previous bindings)

17

Page 18: A Short Java RMI Tutorial

+The RMI Client Lookup the object from the host using Naming.lookup,

cast it to any appropriate type, then use it like a local object

18

Page 19: A Short Java RMI Tutorial

+The RMI Client19

Page 20: A Short Java RMI Tutorial

+Demo – compiling and running the Weather Server

20

Page 21: A Short Java RMI Tutorial

+Compiling and running the Weather Server 1. generate the client stub and server Skelton

2. startup the RMI registry

21

Page 22: A Short Java RMI Tutorial

+Compiling and running the Weather Server 3. startup the server

4. start the client

22