Class4
Upcoming SlideShare
Loading in...5
×
 

Class4

on

  • 1,454 views

Hariprasanna V (9843824677)

Hariprasanna V (9843824677)

Statistics

Views

Total Views
1,454
Views on SlideShare
1,454
Embed Views
0

Actions

Likes
0
Downloads
72
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Defining the remote interfaces : A remote interface specifies the methods that can be invoked remotely by a client. Clients program to remote interfaces, not to the implementation classes of those interfaces. Part of the design of such interfaces is the determination of any local objects that will be used as parameters and return values for these methods; if any of these interfaces or classes do not yet exist, you need to define them as well. Implementing the remote objects: Remote objects must implement one or more remote interfaces. The remote object class may include implementations of other interfaces (either local or remote) and other methods (which are available only locally). If any local classes are to be used as parameters or return values to any of these methods, they must be implemented as well. Implementing the clients: Clients that use remote objects can be implemented at any time after the remote interfaces are defined, including after the remote objects have been deployed.
  •   /** 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 * a Rem (not a RemImpl; it doesn't have this). */

Class4 Class4 Presentation Transcript

  • Advanced Java Unit 4 RMI CMSC 291 Shon Vick
  • Agenda
    • 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
  • Overview 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
    View slide
  • CORBA
    • 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
    View slide
  • 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
  • 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 Server Request Response
  • 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
  • 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 ORB Java IDL Server
  • 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
  • Role of IDL ORB I D L I D L I D L I D L I D L I D L Client Side Object Implementation Side COBOL C Ada C++ Small talk JAVA I D L I D L I D L I D L I D L I D L C++ COBOL Small talk JAVA C C++ Ada ORB
  • How the IDL is Used IDL IDL Compiler Skeleton Code Stub Code Client Code Object Impl Code Language Compiler and Linker ORB Stub Client Skel Object
  • Programming 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
  • 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
  • RMI
    • 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
  • An 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
  • RMI Distributed Object Applications
    • Locate remote objects
    • Communicate with remote objects
    • Load class bytecodes for objects that are passed around
  • Locating 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
  • Communicating 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
  • Loading 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
  • 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.
  • Remote 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.
  • Layers Client Server Stub Skeleton Remote Reference Layer Transport Layer
  • What 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
  • Using 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.
  • Using 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.
  • Design and Implement the Application Components
    • Defining the remote interfaces
    • Implementing the remote objects
    • Implementing the clients
  • Compile 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.
  • 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.
  • The Four Required Classes
    • To use RMI you need to build man classes
      • An interface for the remote object to be used by both the client and the server
      • 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
      • The object implementation – implements the interface of step 1
      • The RMI server – which creates an instance of the object from step 3
  • Steps for Compiling and Running the System
    • Compile the client and the server
    • Generate the client stub using rmic
    • Start the RMI registry – this only needs to be done once
    • Start the the server – done on same machine as in step 3
    • Start the client – this doesn’t have to be on the same machine as 3 and 4
  • A Simple Example
    • In this example the remote object just returns a message string
      • The interface
      • The client
      • The Remote Object Implementation
      • The RMI Server
  • The 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 ; }
  • The 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
  • The 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
  • The Client Code
    • Import the needed Packages
    import java.rmi.*; // For Naming, RemoteException, etc. import java.net.*; // For MalformedURLException import java.io.*; // For Serializable interface
    • 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
  • 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);
    • // …
  • The Remote Object Implementation
    • This class must extend UnicastRemoteObject and implement the interface discussed previously
    • The constructor show throw a RemoteException
  • The 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."); } }
  • The 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).
  • The 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
  • RemServer 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 an Object Registers an Object
  • Putting 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
  • Next Unit
    • RMI/CORBA Issues
    • More Examples
    • Start Chapter 11 - Threads
  • Next Time
    • Study Chapter 7 and review Example
    • Homework posted Monday AM
    • Read Chapter 11 - Threads
  • 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