Remote Method Invocation - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Remote Method Invocation

Description:

RPC call function on another computer ... A stub is a proxy for a remote object. ... UnicastRemoteObject exports object on anonymous port for point-to-point ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 32
Provided by: cyndi8
Category:

less

Transcript and Presenter's Notes

Title: Remote Method Invocation


1
Chapter 13
  • Remote Method Invocation

2
Based on RPC
  • Remote Procedure Call 1980s
  • RPC call function on another computer
  • marshalling data (package arguments, return
    values) related to function call
  • RPC Disadvantages
  • limited data types
  • functions must be described in special Interface
    Definition Language (IDL)

3
RMI
  • java object registers as being remotely
    accessible
  • client obtains remote reference
  • call syntax same as object in same program (no
    IDL)
  • RMI does marshalling of data (like RPC)
  • Can transfer objects like Java object
    serialization

4
RMI - Disadvantages
  • Only Java
  • CORBA still preferred for interoperability and
    speed
  • Web Services becoming more popular for
    cross-platform/cross-language options

5
RMI process
  • Define remote interface what methods can the
    client invoke?
  • Implement the remote object by convention has
    same name as interface, ends with Impl
  • Define client app that uses remote interface
  • Compile and execute

6
RMI Interface
  • java.rmi.Remote
  • tagging interface (implemented in Remote class
    which our RMI classes extend)
  • no methods
  • can be accessed with appropriate security
    permission from any JVM connection to remote
    object
  • class that implements Remote must be exported to
    make object accessible (call super in
    constructor)
  • All method arguments/return values must be
    serializable or primitives

7
RMI Interface - cont
  • Remote exception
  • Checked exception
  • Use on remote methods in case of communication
    problem (not necessarily on all methods in class)

8
rmic
  • rmic is the Java RMI Compiler that generates
    stubs used to connect remote objects
  • rmic v1.2 MyServiceImp creates
    MyServiceImpl_Stub.class
  • A stub is a proxy for a remote object. It
    implements only the remote interfaces, no local
    (i.e. helper) interfaces.
  • If the stub doesnt match the class (that is, if
    the remote methods have changed), a marshalling
    error can occur.
  • rmic is run from the command prompt, in the
    directory that contains the class file (or the
    beginning path for the class file, e.g., the
    directory containing com if the path is
    com.deitel.advjhtp1.rmi.weather

9
Weather Service Example
Remote
Weather List Model
contains
Weather Service ltltinterfacegtgt
Weather Service Client
uses
Weather Cell Renderer
Weather Service Impl
creates
returns
Weather Bean
contains
Weather Item
not uml
10
Weather Service Example
  • Service side Details
  • Gets information by html scraping
  • http//iwin.nws.noaa.gov/iwin/us/traveler.html
  • Interface
  • extends java.rmi.Remote
  • getWeatherInformation is remote method
  • Implementation
  • extends java.rmi.server.UnicastRemoteObject
  • UnicastRemoteObject exports object on anonymous
    port for point-to-point communication using
    standard socket connection
  • Notice call to super() in default constructor,
    which can throw RemoteException. Alternative
    would be to call static method exportObject
    explicitly.
  • Implementation has additional updateWeatherConditi
    ons method to contact Travelers Forecast web
    page, create beans

11
Weather Service Example, cont.
  • main method
  • URL to obtain remote reference is
    rmi//hostpost/remotObjectName, in this case
    rmi//localhost/WeatherService
  • host is computer running rmiregistry
  • port is port number on which registry is running,
    default for rmi is 1099
  • localhost is synonymous with 127.0.0.1
  • static method rebind of class java.rmi.Naming
    binds WeatherServiceImpl to rmi registry (and
    replaces other objects with same name, so
    preferred to bind)

12
Weather Service Example - cont
  • WeatherBean
  • Stores the weather forecast for one city
  • Data beans, objects with getters and setters.
  • Often use to interact with databases, web apps,
    etc.
  • If only getters, then read-only
  • WeatherBean has city name, temperature,
    description, graphic image
  • static initialization block prior to constructor
    - ensures image names are available as soon as
    class loads
  • Properties are like hash table.

13
Weather Service Example cont.
  • Weather Service Client
  • just extends JFrame (no remote class)
  • finds remote object using Naming.lookup (must
    have url of remote service)
  • Access WeatherService, not WeatherServiceImpl
  • refers to stub object on client. Stub passes
    call to rmi system. MUST match version on
    server!
  • uses remote object to do method call
  • remote access is in a try-block that catches
    ConnectionException

14
Weather Service Example - cont
  • WeatherListModel
  • Adapter design pattern. Enables two objects with
    incompatible interfaces to communicate with each
    other. Like electrical plugs.
  • List is not compatible with JList, which requires
    a ListModel. How did they know what methods to
    write? See ListModel on Java website.
  • WeatherCellRenderer
  • displays weather information inside a JList,
    using a WeatherItem
  • WeatherItem
  • formats one line of the weather table

15
Weather Service Example - cont
  • Compile the classes
  • Produce the stub of WeatherServiceImpl
  • rmic v1.2 com.deitel.advjhtp1.rmi.weather.Weather
    ServiceImpl
  • Start the registry from a command prompt
  • rmiregistry
  • NOTE the registry must be able to find the stub.
    Either the stub directory must be on your path,
    or you need to run rmiregistry from the root of
    your application.
  • Run the WeatherServiceImpl
  • Run the WeatherServiceClient

16
RMI Chat Application
  • UnicastRemoteObject runs continuously
  • java.rmi.activation.Activatable activate when
    client invokes request
  • RMI activation daemon (rmid) enables activatable
    objects
  • Cant run activatable on campus, we use modified
    ChatServer that runs continuously.

17
Chat Application Server Classes
Remote

Stoppable ChatServerltinterfacegt --------------- s
topServer
ChatServer ltinterfacegt --------------- registerCli
ent unregisterClient postMessage
(ours is not stoppable)
ChatServerImpl --------------------- main
UnicastRemoteObject
ChatServerAdministrator --------------------------
------- startServer terminateServer main
Interfaces to allow reusable architecture
e.g., with CORBA
(we dont use ChatServerAdministrator)
18
Chat Application Client Classes
Remote
DisconnectListener ltinterfacegt -------------------
------ serverDisconnected
MessageListener ltinterfacegt --------------- messag
eReceived
ChatClient ltinterfacegt --------------- deliverMess
age serverStopping
MessageManager ltinterfacegt -----------------------
connect disconnect sendMessage setDisconnectListe
ner
MyMessageListener
1
RMIMessageManager ------------------------------ s
etDisconnectListener fireServerDiscconnected
DisconnectHandler
1
UnicastRemoteObject
ClientGUI (JFrame)
MessageDisplayer
1
1
1
Runnable
ChatMessage (like bean)
DeitelMessenger ----------------------- main
19
Chat application - ServerImpl
  • registers with rmi registry (in main)
  • ChatServerImpl keeps a HashSet of clients
  • register/unregisterClient maintain HashSet
  • postMessage
  • takes ChatMessage bean as parameter
  • bean includes sender, message (getters/setters)
  • bean MUST be serializable
  • calls deliverMessage on clients (iterates
    HashSet)

20
DeitelMessenger client main
  • sets security manager to enable download of
    stub
  • creates RMIMessageManager
  • creates ClientGUI
  • displays GUI (waits for events)

21
RMI Message Manager
  • uses class java.rmi.Naming
  • provides methods for storing and obtaining
    references to remote objects in the rmi registry
  • Takes string //hostport/name
  • host defaults to localhost
  • port defaults to 1099
  • Bind/Rebind associates name with remote object
  • Lookup returns an object reference, can use
    reference to call methods

22
RMI Message Manager cont.
  • Registry may be shared by all servers on a host
    or individual process may create its own registry
  • connect(MessageListener)
  • does a lookup on ChatServer
  • Pass this as argument to registerClient on
    ChatServer. Enables RMIMessageManager to receive
    callbacks.
  • sets a MessageListener to handle received
    messages

23
RMIMessageManager cont.
  • deliverMessage(ChatMessage)
  • calls messageReceived of MessageListener, passing
    in sender message from bean
  • sendMessage(sender, message)
  • create bean, call postMessage of ChatServer
  • serverStopping
  • sets ChatServer to NULL, message

24
ClientGUI
  • Class that incorporates MessageManager,
    MessageListener and DisconnectListener
  • WindowAdapter to disconnect if close window
  • connectAction calls MessageManager connect,
    sets DisconnectListener
  • most actions just call corresponding functions,
    display status, etc.

25
Dynamic Class Download
  • For clients to download stubs dynamically
  • RMI object specifies java.rmi.server.codebase
  • Classes must be available for download from an
    http server
  • http server must be running on ChatServer machine

26
Executing Chat
  • First start the server
  • rmiregistry This must not be run in directory
    with ChatServerImpl_Stub.class (or with that
    class in its classpath).  NOTE If your home
    computer cannot find rmiregistry, you may need to
    supply a complete path.  For example, in CTLM we
    used to run this command as        "C\Program
    Files\Java\jdk1.5.0_04\bin\rmiregistry.exe"
  • Now you need to start the http server running.
    We'll do this from inside Eclipse Set up a Run
    configuration for examples.classServer.ClassFileSe
    rver
  • Program Arguments 2001 ClassServer\classes
  • VM Arguments none
  • 2001 is port, ClassServer\classes is where youve
    placed the root for it to locate the .class
    files. May be able to use . (if set up in same
    project)
  • Now you need to start the Chat Server.  We'll do
    this from inside EclipseSet up a Run
    configuration for com.deitel.messenger.rmi.server.
    ChatServerImpl       Program Arguments
    none       VM Arguments -Djava.rmi.server.codeba
    sehttp//localhost2001/
  • Then start the client
  • Set up a run configuration for com.deitel.messenge
    r.rmi.client.DeitelMessenger       Program
    Arguments none       VM Arguments
    -Djava.security.policyclient.policyA common
    error here is not having client.policy in the
    correct location. Another possibility is not
    specifying the VM arguments correctly. 

27
Attaching from a different computer
  • run ipconfig on the server to find out the IP
    address
  • Start server normally
  • rmiregistry must be running on client machine (in
    source root directory)
  • Add ip address of server as Program argument (not
    VM argument) for DeitelMessenger. Notice if
    statement that checks for args, routes to host
    other than localhost if arg included.

28
RMI Exercise
  • Modify ChatServer and ChatServerImpl to register
    an ID String with each client.
  • Messages may be sent to a specific client or to
    All
  • Update MessageManager and RMIMessageManager to
    operate with the new ChatServerImpl
  • Modify ClientGUI to accept To field
  • Hint I used a Hashtable instead of a set to
    contain the clients

29
Info on ChatServerAdministrator(not used on
campus)
  • Activatable objects execute in ActivationGroup
    security
  • Starts new virtual machine for each Activation
    Group
  • create new group with needed security policy
  • register group with RMI activation system
  • can customize commands daemon executes on
    starting, not done for Chat
  • each group gets an incarnation number

30
ChatServerAdministrator cont.
  • ActivationDesc config for Activatable remote
    object
  • name of class
  • codebase with remote objects class files
  • marshalledObject with initialization info (null
    for Chat)
  • Call register take ActivationDesc, return
    remote object stub. Bind to RMI registry.
  • Locate Registry does not actually connect to
    remote host obtains reference to bootstrap
    remote object registry.

31
Activatable cont.
  • stopServer
  • Use Thread to ensure time to unbind from registry
    before object terminates. Unbind done in
    ChatServerAdmin.
Write a Comment
User Comments (0)
About PowerShow.com