RMI (Remote Method Invocation) - Java Technology for Accessing Remote Objects, Slides of Computer Science

Remote method invocation (rmi) in java, a technology that enables communication between objects located on different computers. It covers the concept of rmi, its advantages over corba, requirements, terminology, processes, interfaces, classes, conditions for serializability, security, and implementation with examples. Rmi allows a client object to invoke methods on a remote object, making it a powerful tool for building distributed applications.

Typology: Slides

2012/2013

Uploaded on 03/19/2013

dharanidhar
dharanidhar 🇮🇳

4.2

(6)

58 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
RMI
Remote Method Invocation
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download RMI (Remote Method Invocation) - Java Technology for Accessing Remote Objects and more Slides Computer Science in PDF only on Docsity!

RMI

Remote Method Invocation

“The network is the computer”*

  • Consider the following program organization:
    • If the network is the computer, we ought to be able to

put the two classes on different computers

  • For an opposing viewpoint, see http://www.bbspot.com/News/2001/04/network.html

SomeClass AnotherClass

method call

returned object

 RMI is one technology that makes this possible

computer 1 computer 2

What is needed for RMI

• Java makes RMI (Remote Method Invocation)

fairly easy, but there are some extra steps

• To send a message to a remote “server object,”

  • The “client object” has to find the object
    • Do this by looking it up in a registry
  • The client object then has to marshal the parameters

(prepare them for transmission)

  • Java requires Serializable parameters
  • The server object has to unmarshal its parameters, do its computation, and marshal its response
  • The client object has to unmarshal the response

• Much of this is done for you by special software

Terminology

  • A remote object is an object on another computer
  • The client object is the object making the request

(sending a message to the other object)

  • The server object is the object receiving the request
  • As usual, “client” and “server” can easily trade roles

(each can make requests of the other)

  • The rmiregistry is a special server that looks up

objects by name

  • Hopefully, the name is unique!
  • rmic is a special compiler for creating stub (client) and

skeleton (server) classes

Interfaces

• Interfaces define behavior

• Classes define implementation

• Therefore,

  • In order to use a remote object, the client must

know its behavior (interface), but does not need

to know its implementation (class)

  • In order to provide an object, the server must

know both its interface (behavior) and its class

(implementation)

• In short,

Classes

• A Remote class is one whose instances can be

accessed remotely

  • On the computer where it is defined, instances of

this class can be accessed just like any other

object

  • On other computers, the remote object can be

accessed via object handles

• A Serializable class is one whose instances

can be marshaled (turned into a linear

sequence of bits)

Remote interfaces and class

• A Remote class has two parts:

  • The interface (used by both client and server):
    • Must be public
    • Must extend the interface java.rmi.Remote
    • Every method in the interface must declare that it throws java.rmi.RemoteException (other exceptions may also be thrown)
  • The class itself (used only by the server):
    • Must implement a Remote interface
    • Should extend java.rmi.server.UnicastRemoteObject
    • May have locally accessible methods that are not in its Remote interface Docsity.com

Remote vs. Serializable

• A Remote object lives on another computer

(such as the Server)

  • You can send messages to a Remote object and

get responses back from the object

  • All you need to know about the Remote object is

its interface

  • Remote objects don’t pose much of a security

issue

• You can transmit a copy of a Serializable

object between computers

The server class

  • The class that defines the server object should extend UnicastRemoteObject - This makes a connection with exactly one other computer - If you must extend some other class, you can use exportObject() instead - Sun does not provide a MulticastRemoteObject class
  • The server class needs to register its server object:
    • String url = "rmi://" + host + ":" + port + "/" + objectName ;
      • The default port is 1099
    • Naming.rebind(url, object );
  • Every remotely available method must throw a RemoteException (because connections can fail)
  • Every remotely available method should be synchronized

Hello world server: interface

  • import java.rmi.*;

public interface HelloInterface extends Remote { public String say() throws RemoteException; }

Registering the hello world server

  • class HelloServer { public static void main (String[] argv) { try { Naming.rebind("rmi://localhost/HelloServer", new Hello("Hello, world!")); System.out.println("Hello Server is ready."); } catch (Exception e) { System.out.println("Hello Server failed: " + e); } } }

The hello world client program

  • class HelloClient { public static void main (String[] args) { HelloInterface hello; String name = "rmi://localhost/HelloServer"; try { hello = (HelloInterface)Naming.lookup(name); System.out.println(hello.say()); } catch (Exception e) { System.out.println("HelloClient exception: " + e); } } }

Trying RMI

• In three different terminal windows:

1. Run the registry program:

  • rmiregistry

2. Run the server program:

  • java HelloServer

3. Run the client program:

  • java HelloClient

 If all goes well, you should get the “Hello,

World!” message

Summary

1. Start the registry server, rmiregistry

2. Start the object server

1. The object server registers an object, with a

name, with the registry server

3. Start the client

1. The client looks up the object in the registry

server

4. The client makes a request

1. The request actually goes to the Stub class

2. The Stub classes on client and server talk to eachDocsity.com