Cup 13: RMI - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Cup 13: RMI

Description:

A default manager is provided in java.rmi ... Install a Security Manager. RMISecurityManager. Instantiate the remote object ... java QuoteOfTheDay ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 20
Provided by: timma87
Category:
Tags: rmi | cup | install | java

less

Transcript and Presenter's Notes

Title: Cup 13: RMI


1
Cup 13 RMI
  • Special Topics Java

Dr. Tim Margush Department of Mathematics and
Computer Science The University of Akron
2
Remote Method Invocation
  • Remote - Objects exist on different systems
    (Distributed or Client-Server Computing)
  • Remote Method - A method belonging to a class
    called a server
  • RMI - A method in a client class calls a method
    in a server class

3
RMI Registry
  • A separate application running on machine A
    (providing remote objects)
  • Links the client and server together
  • Server starts and registers in the RMI Registry
    of machine A
  • Client on machine B contacts RMI Registry via a
    socket with a request
  • If allowed, a reference to the remote object is
    returned

4
Stubs
  • The client's reference to a remote object allows
    method calls across the internet
  • Arguments and return values are passed via object
    serialization
  • A Skeleton object is created on the server
    machine to perform the actual method calls and
    communication with the server object
  • The Security Manager supervises the communication
    between skeleton and server

5
RMISecurityManager
  • RMI interactions are governed by some security
    manager
  • Client Applets use the Applet Security Manager
  • Client Applications require installation of an
    RMI Security Manager
  • A default manager is provided in java.rmi
  • A bootstrap process is typically used to install
    the security manager before running an application

6
Arguments and Parameters
  • Passing an object to a local method is
    accomplished by copying the reference to the
    object
  • RMI requires copying the object to the remote
    machine where it is reconstructed (in the
    skeleton)
  • The skeleton invokes the remote method, passing a
    reference to the reconstructed object

7
A Simple RMI Quote Server
  • Remote Interface
  • Defines the public methods of the remote object
  • The remote object will implement this interface
  • Remote methods must throw RemoteException, in
    case an error occurs during the RMI process

8
The QuoteProtocol
  • This is the interface we will implement
  • import java.rmi.
  • public interface QuoteProtocol extends Remote
  • public String getQuote(int qnum)
  • throws RemoteException
  • public int getMaxQuoteNumber()
  • throws RemoteException

9
The Remote Object
  • import java.rmi.
  • import java.rmi.server.UnicastRemoteObject
  • //UnicastRemoteObject provides server-type stuff
  • public class QuoteOfTheDay
  • extends UnicastRemoteObject
  • implements QuoteProtocol
  • //RMI requires this default constructor
  • public QuoteOfTheDay()
  • throws RemoteException

10
Implement QuoteProtocol
  • Q is a private array of Strings containing the
    quotes to be served (declaration omitted)
  • public String getQuote(int qnum)
  • return Qqnum //no check for limit - sloppy!
  • public int getMaxQuoteNumber()
  • return Q.length-1

11
Remote Bootstrap
  • public static void main(String args)
  • System.setSecurityManager
  • (new RMISecurityManager())
  • try
  • QuoteOfTheDay qod new QuoteOfTheDay()
  • Naming.rebind("quoteoftheday", qod)
  • catch (Exception e)
  • System.out.println(e.getMessage())

12
Creating the Stub and Skeleton
  • After compiling the class...
  • javac QuoteOfTheDay.java
  • Create the stub and skeleton
  • rmic QuoteOfTheDay
  • QuoteOfTheDay_Stub.class
  • QuoteOfTheDay_Skel.class
  • (be sure CLASSPATH gives access to java's
    Classes.zip and the QuoteOfTheDay.class file)

13
Registering a Remote Object
  • Install a Security Manager
  • RMISecurityManager
  • Instantiate the remote object
  • remObjRef new objRemote()
  • Bind the remote object to the registry
  • Naming.rebind("someName", remObjRef)
  • The RMI Registry must be running before binding
    can occur

14
Starting the Registry
  • The RMI Registry is an application found in the
    jdk's bin directory
  • Run it as a background process
  • rmiregistry port
  • default port is 1099
  • Be sure the CLASSPATH variable for this session
    includes the location of the Stub and Skeleton!

15
Getting in a Bind
  • With the Registry process active, you can now
    register (bind) remote objects
  • java QuoteOfTheDay
  • Runs main, installs security manager,
    instantiates remote object and registers it
  • This will work only on a LAN due to a bug
  • Workaround - use this switch to fully qualify the
    hostname of the server
  • java -Djava.rmi.server.hostnamesomeHost.edu
    QuoteOfTheDay

16
RMI Client
  • The client must instantiate the stub for the
    remote object
  • The remote interface datatype is used for this
    purpose
  • import java.rmi.
  • import java.rmi.registry.
  • public class Quote
  • static QuoteProtocol qod

17
Instantiating the Stub
  • Naming.lookup finds the registered object by
    name, instantiates a Stub, and returns a
    reference it
  • try
  • String regName "rmi//130.101.87.25/quoteofthed
    ay"
  • qod (QuoteProtocol) Naming.lookup(regName)
  • catch (Exception ex)
  • System.out.println("Cannot get remote
    object " ex)

18
Invoking a Remote Method
  • The Stub object allows the client to invoke any
    method in the remote interface
  • try
  • int num qod.getMaxQuoteNumber()
  • String someQuote qod.getQuote(num)
  • System.out.println("Quote " num " is "
    aQuote)
  • catch (Exception ex)
  • System.out.println("Error " ex)

19
An Applet Client
  • Set up the RMI access in the Applet's Start
    method
  • regName "rmi//"
  • getCodeBase().getHost() "/quoteoftheday"
  • qod (QuoteProtocol)Naming.lookup(regName)
  • MQN qod.getMaxQuoteNumber()
Write a Comment
User Comments (0)
About PowerShow.com