Class4

46
UMBC Advanced Java Unit 4 RMI CMSC 291 Shon Vick

description

Hariprasanna V (9843824677)

Transcript of Class4

Page 1: Class4

UMBC

Advanced JavaUnit 4RMI

CMSC 291

Shon Vick

Page 2: Class4

2

UMBCAgenda

• Present some ideas about distributed computing

• Briefly look at CORBA and contrast it with RMI

• Look at the Java mechanism for Remote Method Invocation (RMI)

• See some examples

Page 3: Class4

3

UMBCOverview of RMI Applications

• Pure Java answer to RPC, DCOM, and CORBA

• CORBA allows object on different machines to communicate

• RMI allows objects on different JVMs potentially on different machines to communicate

Page 4: Class4

4

UMBCCORBA

• The Common Object Request Brokering System (CORBA) glues together diferent objects models regardless of implementation language

• In order to do so it uses a common interface definition language that needs to be compiled separately for every language for which there is a binding

Page 5: Class4

5

UMBC

Introduction to CORBA

• So what is an object (class, interface) in the CORBA sense?– Collection of data items usually called

attributes or slots– Collection of behavioral attachments usually

called methods

Page 6: Class4

6

UMBC What Is Distributed Computing All About

• Goal is to break up a monolithic application into smaller components

• Client - user of a resource• Server - provider of a resource

Client ServerRequest

Response

Page 7: Class4

7

UMBC Technical Overview of Architecture

• The client and object implementation are isolated from the ORB by an IDL interface

• CORBA requires that objects be defined by OMG IDL

• A method request does not pass directly from client to server but rather is mediated by the ORB

• Method invocation is the same whether the object is local or remote

Page 8: Class4

8

UMBC CORBA Interfaces

• IDL - interface definition language – CORBA is language independent– Can provide a standard notation for software components

– IDL supports library function interfaces just as well as distributed objects across a network

C++

IDL

Client

Java

IDL

Server

ORB

Page 9: Class4

9

UMBC IDL - Separating Interface From Implementation

• A standard notation language for defining interfaces to objects

• OMG IDL is a simple subset of C++

• IDL is like a contract for defining an API

• Designed to be efficient across networks

Page 10: Class4

10

UMBCRole of IDL

ORB

C

C++

Ada

I D L

I D L

I D L

I D L

I D L

I D L

Client Side

Object Implementat

ion Side COBOL

C

Ada

C++

Smalltalk

JAVA

I D L

I D L

I D L

I D L

I D L

I D L

ORB

C++

COBOL

Smalltalk

JAVA

Page 11: Class4

11

UMBCHow the IDL is Used

IDL

IDL Compiler

Skeleton CodeStub Code

Client CodeObject Impl Code

Language Compiler and Linker

ORB

Stub

Client

Skel

Object

Page 12: Class4

12

UMBCProgramming Steps

• Define the IDL interfaces

• Implement these interfaces with C++ classes

• Write a client mainline to bind to server

• Write a server mainline which creates instances of the classes

• Register the server

Page 13: Class4

13

UMBC Comparison of RMI and CORBA

• RMI is Java to Java based– JNI allows Java to other languages

• No mapping of objects – no IDL• RMI deals only with byte code• No complicated request broker - just a

simple registry• Makes a distinction for a remote object -

allows errors to be handled

Page 14: Class4

14

UMBCRMI

• An Overview of RMI Applications• Looking at a Simple Example

– Walking through a RMI Server– Walking through a Client Program – Running the applications

• Next time– Additional Examples– Reviewing Some More Examples

Page 15: Class4

15

UMBCAn Overview of RMI Applications

• Two Parts to a RMI based distributed object application– Server

• creates remote objects • makes them available to client • waits for clients to invoke methods these remote

objects

– Client: • Use the references to the remote Objects

Page 16: Class4

16

UMBC RMI Distributed Object Applications

• Locate remote objects

• Communicate with remote objects

• Load class bytecodes for objects that are passed around

Page 17: Class4

17

UMBCLocating Remote Objects

• Applications can use one of two mechanisms to obtain references to remote objects. – An application can register its remote objects

with the rmiregistry - RMI's simple naming facility

– An application can pass and return remote object references as part of its normal operation

Page 18: Class4

18

UMBCCommunicating with Remote Objects

• Details of communication between remote objects are handled by RMI

• From the perspective of the programmer it’s as if the objects are local

• There is some overhead that makes it slower but the communication looks like a standard Java method invocation

Page 19: Class4

19

UMBCLoading the Class

• The types and the behavior of an object, previously available only in a single virtual machine can be transmitted to another, possibly remote, virtual machine.

• Contrast with the reading and writing of objects that we have discussed in the previous units

Page 20: Class4

20

UMBC Remote Interfaces, Objects and Methods

• A distributed application built using Java RMI is made up of interfaces and classes

• An object becomes remote by implementing a remote interface, which has the following characteristics– A remote interface extends the interface

java.rmi.Remote. – Each method of the interface declares

java.rmi.RemoteException in its throws clause, in addition to any application-specific exceptions.

Page 21: Class4

21

UMBCRemote Objects

• RMI treats a remote object differently from a non remote object when the object is passed from one virtual machine to another.

• Rather than making a copy of the implementation object in the receiving virtual machine, RMI passes a remote stub for a remote object.

• The stub acts as a proxy for the remote object and is basically a remote reference.

• The caller invokes a method on the local stub, which is responsible for carrying out the method call on the remote object.

Page 22: Class4

22

UMBCLayers

Client Server

Stub Skeleton

Remote Reference Layer

Transport Layer

Page 23: Class4

23

UMBCWhat happens Where?

• Layer 1 is the Application Layer• Layer 2 is the client stub/skeleton layer. These are

the proxy objects these are produced by the rmic command

• Layer 3 is the remote reference that deals with the the actual remote invocations

• Layer 4 is the transport layer responsible for actually setting up the connections and handling the transport of data between machines

Page 24: Class4

24

UMBCUsing Remote Objects

• A stub for a remote object implements the same set of remote interfaces that the remote object implements

• This allows a stub to be cast to any of the interfaces that the remote object implements

• Only those methods defined in a remote interface are available to be called in the receiving virtual machine.

Page 25: Class4

25

UMBCUsing RMI – General Steps

• Design and implement the components of your distributed application.

• Compile sources and generate stubs and skeletons

• Make classes network accessible

• Start the application.

Page 26: Class4

26

UMBC Design and Implement the Application Components

• Defining the remote interfaces

• Implementing the remote objects

• Implementing the clients

Page 27: Class4

27

UMBCCompile Sources and Generate Stubs

• This is a two-step process. – In the first step you use the javac compiler to compile

the source files containing the implementation of the remote interfaces and implementations, the server classes, and the client classes.

– In the second step you use the rmic compiler to create stubs for the remote objects. RMI uses a remote object's stub class as a proxy in clients so that clients can communicate with a particular remote object.

Page 28: Class4

28

UMBC Make Classes Network Accessible/ Starting

• In this step you make everything--the class files associated with the remote interfaces, stubs, and other classes that need to be downloaded to clients--accessible via a Web server

• Starting the application includes running the RMI remote object registry, the server, and the client.

Page 29: Class4

29

UMBCThe Four Required Classes

• To use RMI you need to build man classes1. An interface for the remote object to be used by both

the client and the server

2. The RMI client which looks up the object on a remote machine, cast it to the needed type of the interface given in the step 1

3. The object implementation – implements the interface of step 1

4. The RMI server – which creates an instance of the object from step 3

Page 30: Class4

30

UMBC Steps for Compiling and Running the System

1. Compile the client and the server2. Generate the client stub using rmic3. Start the RMI registry – this only needs to

be done once4. Start the the server – done on same

machine as in step 35. Start the client – this doesn’t have to be on

the same machine as 3 and 4

Page 31: Class4

31

UMBCA Simple Example

• In this example the remote object just returns a message string

1. The interface

2. The client

3. The Remote Object Implementation

4. The RMI Server

Page 32: Class4

32

UMBCThe interface

import java.rmi.*; /** The RMI client will use this interface directly. * The RMI server will make a real remote object that * implements this, then register an instance of it * with some URL. */ public interface Rem extends Remote { public String getMessage() throws RemoteException;}

Page 33: Class4

33

UMBCThe RMI Client

• Looks up the object from the appropriate host using Naming.lookup

• Casts it to the appropriate type then uses it like a local object

• Unlike CORBA RMI has to know the host that is providing the remote services – uses URL of the form rmi:://host/path or rmi:

rmi:://host:port/path

– the default port is 1099

Page 34: Class4

34

UMBCThe RMI Client

• A number of exceptions must be caught– RemoteException– NotBoundException– MalformedURLException

• Requires java.rmi for RemoteException as well Naming and and NotBound Exception

• Requires java.net for MalformedURLException as we have already seen

• Many Clients will pass Serializable objects to the remote objects so importing java.io is usually done as well – not needed here however

Page 35: Class4

35

UMBCThe Client Code

• Import the needed Packages

import java.rmi.*; // For Naming, RemoteException, etc.import java.net.*; // For MalformedURLExceptionimport java.io.*; // For Serializable interface

Page 36: Class4

36

UMBC

public class RemClient { public static void main(String[] args) { try { String host = (args.length > 0) ? args[0] : "localhost"; // Get the remote object and store it in remObject: Rem remObject = (Rem)Naming.lookup("rmi://" + host + "/Rem"); // Call methods in remObject: System.out.println(remObject.getMessage()); //catch the exceptions … } }}

The Client Code Body

Can now use like a local object

Page 37: Class4

37

UMBC Catching the Exceptions in the Client

try{ // … } catch(RemoteException re) { System.out.println("RemoteException: " + re); } catch(NotBoundException nbe) { System.out.println("NotBoundException: " + nbe); } catch(MalformedURLException mfe) { System.out.println("MalformedURLException: " + mfe);// …

Page 38: Class4

38

UMBC The Remote Object Implementation

• This class must extend UnicastRemoteObject and implement the interface discussed previously

• The constructor show throw a RemoteException

Page 39: Class4

39

UMBCThe Remote Object

import java.rmi.*;import java.rmi.server.UnicastRemoteObject;

public class RemImpl extends UnicastRemoteObject implements Rem { public RemImpl() throws RemoteException {}  public String getMessage() throws RemoteException { return("Here is a remote message."); }}

Page 40: Class4

40

UMBCThe RemImpl Object

• This is the actual implementation of Rem that the RMI server uses.

• The server builds an instance of this then registers it with a URL

• The client accesses the URL and binds the result to Rem (not a RemImpl; it doesn't have this).

Page 41: Class4

41

UMBCThe RMI Server

• Builds an object and registers it with a particular URL using Naming.rebind to replace any other bindings or Naming.bind which throws a AlreadyBoundException if there is a previous binding

• Bind really means register here• You have to catch RemoteException and

URLMalformedException

Page 42: Class4

42

UMBCRemServer

mport java.rmi.*;import java.net.*; public class RemServer { public static void main(String[] args) { try { RemImpl localObject = new RemImpl(); Naming.rebind("rmi:///Rem", localObject); } catch(RemoteException re) { System.out.println("RemoteException: " + re); } catch(MalformedURLException mfe) { System.out.println("MalformedURLException: " + mfe); } }

}

Makes anObject

Registers an Object

Page 43: Class4

43

UMBCPutting it all Together

• Compile the Client and the Server

javac RemClient.java RemServer.java• Generate the Client Stub and Server Skeleton

rmic RemImpl• Starts the RMI registry

Rmiregistry [&]• Start the server Start the client

java RemServer [&] java RemClient

Page 44: Class4

44

UMBCNext Unit

• RMI/CORBA Issues

• More Examples

• Start Chapter 11 - Threads

Page 45: Class4

45

UMBCNext Time

• Study Chapter 7 and review Example

• Homework posted Monday AM

• Read Chapter 11 - Threads

Page 46: Class4

46

UMBC

Selected References

• Advanced Techniques for Java Developers Chapter 6,7

• http://java.sun.com/docs/books/tutorial/rmi/index.html

• Exploring Java, O’Reilly, Niemeyer & Peck• Java RMI Remote Method Invocation, Troy

Downing, IDG Books• The RMI example comes from Core Web

Programming, Marty Hall, Prentice Hall