class4

Post on 15-May-2015

1.229 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Hariprasanna V (9843824677)

TRANSCRIPT

UMBC

Advanced JavaUnit 4RMI

CMSC 291

Shon Vick

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

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

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

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

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

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

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

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

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

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

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

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

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

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

16

UMBC RMI Distributed Object Applications

• Locate remote objects

• Communicate with remote objects

• Load class bytecodes for objects that are passed around

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

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

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

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.

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.

22

UMBCLayers

Client Server

Stub Skeleton

Remote Reference Layer

Transport Layer

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

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.

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.

26

UMBC Design and Implement the Application Components

• Defining the remote interfaces

• Implementing the remote objects

• Implementing the clients

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.

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.

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

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

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

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;}

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

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

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

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

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);// …

38

UMBC The Remote Object Implementation

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

• The constructor show throw a RemoteException

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."); }}

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).

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

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

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

44

UMBCNext Unit

• RMI/CORBA Issues

• More Examples

• Start Chapter 11 - Threads

45

UMBCNext Time

• Study Chapter 7 and review Example

• Homework posted Monday AM

• Read Chapter 11 - Threads

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

top related