Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

DDU IOT

Department of Computer Science


CoSc2084
5. RMI (Remote Method Invocation)

1
Introduction
 Remote Method Invocation (RMI) is a distributed
systems technology that allows one JVM (machine)
to invoke object methods that will be run on another
JVM (machine) located elsewhere on a network.
 This technology is very important for the
development of large-scale systems, as it makes it
possible to distribute resources and processing load
across more than one machine.
 Using RMI, objects can invoke methods on other
objects located remotely as easily as if they were on
the local host machine.
2
Java Virtual Machine

Object computer 1
main(String[ ] args)

Method request computer 2

Java Virtual Machine

RemoteObject
Method response method1(…)
method2(…)
3
Introduction
 Each RMI service is defined by an interface,
which describes object methods that can be
executed remotely. This interface must be
shared by all developers who will write software
for that service.
 More than one implementation of the interface
can be created, and developers do not need to be
aware of which implementation is being used or
where it is located.
4
Introduction
 How Does RMI Work?
 Systems that use RMI for communication
typically are divided into two categories:
 Servers
A server provides an RMI service
 Clients
A client invokes object methods of this service.

5
Introduction
 RMI servers must register with a lookup service,
to allow clients to find them, or they can make
available a reference to the service in some other
way.

 The Java platform includes an application called


rmiregistry (which runs as a separate process).
This application allows other applications to
register RMI services or obtain a reference to a
named service.
6
Introduction
 Each service registration is associated with a name to
allow clients to select the appropriate service.
 If a service is moved to another server, the client need
only look up the registry again to find the new location.

RMI REGISTRY

RMI Server RMI Server RMI Server


7
Introduction
 Once a server has registered, it will then
wait for incoming RMI requests from
clients.

8
Introduction
 RMI clients will send RMI messages to invoke
an object method remotely. However, a client
must first obtain a reference for the remote
object which is normally done by looking up a
service in the RMI registry.
 The client requests a particular service name,

and receives a URL to the remote resource. The


URL has the following format:
rmi://hostname:port/servicename
9
Introduction
 Once an object reference is obtained, the
client can then interact with the remote
service.
 The networking details of requests are
completely transparent to the application
developer.
 This is achieved through a division of the
RMI system into two components i.e. the
stub and the skeleton. 10
Introduction
 The stub object acts as a proxy object, conveying
object requests to the remote RMI server.
 It implements a particular RMI interface, which
the client application can use just like any other
object implementation.

 When the stub object receives a request, it passes


a message to a remote RMI service, waits for a
response, and returns the response to the calling
method. 11
Introduction
 Hence, the application developer need not
be concerned about where the RMI
resource is located, on which platform it is
running, or
 how it will fulfill the request.
 The RMI client simply invokes a method of
the proxy object which handles all the
implementation details.
12
RMI client application

stub object computer 1


somemethod(…)

request Computer 2

RMI server

response skeleton object


somemethod(…)

13
Introduction
 At the RMI server end, the skeleton object is
responsible for listening for incoming RMI requests and
passing these on to the RMI service.
 Note that the skeleton object does not provide an
implementation of an RMI service.
 It only acts as a receiver of requests and passes these
requests on further to the actual implementation object
that implements the RMI interface.
 The implementation object executes the appropriate
method and pass the results back to the stub object in
the RMI client.
14
Introduction
 TCP sockets are used in the communication
that occurs between stub and skeleton.

call

skeleton
Stub

RMI Client RMI Server


return

15
RMI and other technologies
 CORBA (Common Object Request Broker
Architecture) has long been king
 CORBA supports object transmission between virtually any
languages
 Objects have to be described in IDL (Interface Definition
Language), which looks a lot like C++ data definitions
 CORBA is complex and flaky
 Microsoft supported CORBA, then COM, now .NET
 RMI is purely Java-specific
 Java to Java communications only
 As a result, RMI is much simpler than CORBA
16
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
17
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 18
Processes
 For RMI, you need to be running three processes
 The Client
 The Server
 The Object Registry, rmiregistry, which is like a DNS
service for objects
 You also need TCP/IP active

19
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,
 The interface must be available to both client and server
 The class should only be on the server
20
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)
 Serializable objects can be transmitted from one computer to
another
 It probably isn’t a good idea for an object to be both
remote and serializable 21
Conditions for serializability
 If an object is to be serialized:
 The class must be declared as public
 The class must implement Serializable

 The class must have a no-argument

constructor
 All fields of the class must be serializable:

either primitive types or serializable objects

22
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
23
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 receiving object needs to know how the object is
implemented; it needs the class as well as the interface
 There is a way to transmit the class definition
 Accepting classes does pose a security issue 24
Security
 It isn’t safe for the client to use somebody else’s code
on some random server
 Your client program should use a more conservative security
manager than the default
 System.setSecurityManager(new RMISecurityManager());
 Most discussions of RMI assume you should do this on
both the client and the server
 Unless your server also acts as a client, it isn’t really
necessary on the server

25
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

26
Hello world server: interface
 import java.rmi.*;

public interface HelloInterface extends Remote {


public String say() throws RemoteException;
}

27
Hello world server: class
 import java.rmi.*;
import java.rmi.server.*;

public class Hello extends UnicastRemoteObject


implements HelloInterface {
private String message; // Strings are serializable

public Hello (String msg) throws RemoteException {


message = msg;
}

public String say() throws RemoteException {


return message;
}
}
28
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);
}
}
}

29
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);
}
}
}
30
rmic
 The class that implements the remote object should be
compiled as usual
 Then, it should be compiled with rmic:
 rmic Hello
 This will generate files Hello_Stub.class and
Hello_Skel.class
 These classes do the actual communication
 The “Stub” class must be copied to the client area
 The “Skel” was needed in SDK 1.1 but is no longer necessary

31
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

32
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 each other
3. The client’s Stub class returns the result

33
The End

34

You might also like