CS4273: Distributed System Technologies and Programming I - PowerPoint PPT Presentation

About This Presentation
Title:

CS4273: Distributed System Technologies and Programming I

Description:

CS4273: Distributed System Technologies and Programming I Lecture 12: Java RMI (Remote Method Invocation) * Client-Server Programming without RMI (RPC) Client Main ... – PowerPoint PPT presentation

Number of Views:232
Avg rating:3.0/5.0
Slides: 20
Provided by: PengJ
Category:

less

Transcript and Presenter's Notes

Title: CS4273: Distributed System Technologies and Programming I


1
CS4273 Distributed System Technologies and
Programming I
  • Lecture 12 Java RMI (Remote Method Invocation)

2
Client-Server Programming without RMI (RPC)
3
Remote Method Invocation (RMI)
Stub objects are auto-generated by rmic from
interface definition
4
An Example of RMI
  • A service routine
  • public class ProductImpl extends
    UnicastRemoteObject implements Product
  • public ProductImpl (String pd) throws
    RemoteException
  • descript pd
  • public String getDescription ()
  • return ProductDescipt descript .
  • private String descript
  • .
  • A client RMI call
  • Product p Naming.lookup(url-name)
  • String descript p.getDescription ()
  • System.out.println(descript)
  • An interface
  • interface Product extends Remote
  • String getDescription () throws
    RemoteException
  • String placeOrder () throws RemoteException
  • int getOrderNo () throws RemoteException
  • N.B. every rmi method must throw exceptions, due
    to the un-reliability of RMI.

5
Inheritance Graph of Interface and Remote servers
6
Naming Conventions for RMI classes and Files
  • Interface def file no suffix (e.g. Product)
  • Interface impl file Impl suffix (e.g.
    ProductImpl)
  • Server program file Server suffix (e.g.
    ProductServer)
  • Client program file Client suffix (e.g.
    ProductClient)
  • Stub program file _Stub suffix (e.g.
    ProductImpl_Stub)

7
Client-Server Binding in RMI
  • An RMI registration server (rmiregistry) on a
    site is responsible for locating any appl servers
    on that site. This rmiregistry should run on the
    server site before any appl servers start.
  • An appl server needs to register itself to
    rmiregistry on the same site when it is created.
  • A client looks up an appl server from rmiregistry
    on that site by specifying the servers URL-addr
  • (rmi//svr-site/svr-name).
  • A naming class
  • java.rmi.server.Naming
  • provides an interface for servers
  • to reg / de-reg and for clients to look up
    servers.

8
An Example of RMI
  • A simple interface implementation program
  • import java.rmi. import java.rmi.server. //
    file rmi/svr/ProductImpl.java
  • public class ProductImpl extends
    UnicastRemoteObject implements Product
  • private String descript
  • public ProductImpl (String pd) throws
    RemoteException
  • descript pd
  • public String getDescription ()
  • return "ProductDescript"
    descript "."
  • A server program // creates an impl obj and
    registers it
  • public class ProductSvr // file
    rmi/svr/ProductSvr.java
  • public static void main (String args)
  • try ProductImpl p1 new
    ProductImpl (Jias Warehouse")
  • Naming.rebind
    ("warehouse", p1) // register svr by name
  • catch (Exception e)
    System.out.println("Error" e)

9
Compile Server Program
  • We have the following files in servers
    directory
  • Product.java // interface file
  • ProductImpl.java // interface implementation
    file
  • ProductSvr.java // server program file
  • Compile the interface file javac Product.java
  • It can be skipped. When compiling
    ProductImpl.java, it is auto-compiled.
  • Compile the server implementation javac
    ProductImpl.java
  • Generate stub class using RMIC rmic
    ProductImpl
  • It generates file ProductImpl_Stub.class (for
    both client and server).
  • N.B. ProductImpl java must be compiled before
    using rmic.
  • Compile the server program javac
    ProductSvr.java
  • N.B. the server links procedures in
    ProductImpl_Stub at run-time.

10
A Simple Client Program
  • // file java/rmi/clnt/ProductClnt.java
  • //usage java ProductClnt server-hostname
  • import java.rmi. import java.rmi.server.
  • public class ProductClnt
  • public static void main (String args)
  • String url "rmi//"args0
  • try
  • Product p1 (Product)
    Naming.lookup (url "/warehouse")
  • System.out.println(p1.getDescript
    ion())
  • System.out.println(p1.placeOrder(
    ))
  • System.out.println("OrderNo "
    p1.getOrderNo())
  • catch (Exception e)
    System.out.println("Error" e)

11
Build Client Program
  • Copy interface file Product.java to local
    directory.
  • Copy the client stub file ProductImpl_Stub.class
    (generated by rmic) to the local directory.
  • Compile the client source code javac
    ProductClnt.java
  • when compiling ProductClnt.java, it automatically
    compiles the interface Product.java if it has not
    been compiled yet. The compiler locates the
    interface Product.java by naming conventions.
  • client calls stub procedures in
    ProductImpl_Stub.class at run-time. It is in a
    separate class file, without linking to the
    client at compilation time.

12
Run the Example of RMI
  • Start RMI registry program on the server site
  • rmiregistry
  • Start the server program on the server site
  • java ProductSvr
  • Start the client program on the client site
  • java ProductClnt args

13
Naming class for client-server Binding
  • RMI uses class java.rmi.server.Naming for
    client-server binding
  • functions, which includes
  • static Remote lookup (String url)
  • returns the remote object for the URL
  • static void bind(String name, Remote obj)
  • binds name to the remote object obj
  • static void rebind(String name, Remote obj)
  • binds name to remote obj replaces any
    existing one
  • static void unbind(String name)
  • unbinds the name
  • static String list(String url)
  • returns an array of strings of urls registered
    at the registry server.

14
View Database of RMI Registration Server
  • // file java/rmi/showBinding.java
  • import java.rmi. import java.rmi.server.
  • public class showBindings
  • public static void main ( String args)
  • try
  • String bindings
    Naming.list("")
  • for (int i 0 i lt
    bindings.length i)

  • System.out.println(bindingsi)
  • catch(Exception e)
    System.out.println("Error"e)

15
Summary of Programming with RMI
  • On Server Site
  • write an interface file.
  • write the interface implementation program, which
    implements all methods in the interface.
  • write a server program, which initializes a
    server-impl object and registers the server
    object with the registry.
  • compile all the source files on server side.
  • generate stub class by using rmic.

16
Summary of Programming with RMI (Cont.)
  • On Client Site
  • copy the interface file and the generated stub
    class file to the local directory (from the
    server site).
  • write a client program that uses rmi.
  • compile the client program.
  • Run the Client Server
  • run the registry server on server site.
  • run server and client on server and client sites
    respectively.

17
Applets Using RMI
18
Programming Applets using RMI
  • The server (and rmiregistry server) must run on
    the Web server, bcs applets can only communicate
    with their home server!!!
  • Copy the interface file (Product.java) and the
    stub class file (ProductImpl_Stub.class) to the
    local directory.
  • Write applet "ProductApplet.java" and compile it.
  • Run "rmiregistry" and server "ProductSvr" on the
    Web Server site.
  • Run applet in Web Browser.
  • N.B. No need to change the server program
  • when moving a client to an applet.

19
An Example of RMI Applet Program
  • // file java/rmi/applet/ProductApplet.java
  • import java.rmi. import java.rmi.server.
    import java.applet.Applet
  • public class ProductApplet extends JApplet
  • private String message "Initializing"
  • public void init()
  • String url "rmi//"
    getCodeBase().getHost() // web-site !!!
  • try Product p1 (Product)
    Naming.lookup (url "warehouse")
  • message p1.getDescription()
    // rmi
  • repaint()
  • catch (Exception e)
    System.out.println("Error" e)
  • public synchronized void paint(Graphics g)
  • g.drawString(message, 5, 50)
Write a Comment
User Comments (0)
About PowerShow.com