Advanced Programming

25
Advanced Programming Rabie A. Ramadan Lecture 4

description

Advanced Programming. Rabie A. Ramadan Lecture 4. A Simple Use of Java Remote Method Invocation (RMI). Consider the following program organization:. If the network is the computer, we ought to be able to put the two classes on different computers. method call. AnotherClass. SomeClass. - PowerPoint PPT Presentation

Transcript of Advanced Programming

Page 1: Advanced Programming

Advanced Programming

Rabie A. Ramadan

Lecture 4

Page 2: Advanced Programming

A Simple Use of Java Remote Method Invocation (RMI)

2

Page 3: Advanced Programming

“The network is the computer”*

3

Consider the following program organization:

If the network is the computer, we ought to be able to put the two classes on different computers

SomeClass AnotherClass

method call

returned object

RMI is one technology that makes this possible

computer 1 computer 2

Page 4: Advanced Programming

RMI and other technologies

4

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

Page 5: Advanced Programming

What is needed for RMI?

5

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

Page 6: Advanced Programming

Terminology

6

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

Page 7: Advanced Programming

Processes

7

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

Page 8: Advanced Programming

Interfaces

8

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

Page 9: Advanced Programming

Classes

9

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

Page 10: Advanced Programming

Conditions for serializability

10

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

Page 11: Advanced Programming

The Remote Interface

11

In RMI, a common remote interface is the minimum amount of information that must be shared in advance between “client” and “server” machines. It defines a high-level “protocol” through which the machines will communicate.

A remote interface is a normal Java interface, which must extent the marker interface java.rmi.Remote.• Corollaries: because the visible parts of a remote object are defined

through a Java interface, constructors, static methods and non-constant fields are not remotely accessible (because Java interfaces can’t contain such things).

All methods in a remote interface must be declared to throw the java.rmi.RemoteException exception.

Page 12: Advanced Programming

A Simple Example

12

A file MessageWriter.java contains the interface definition: import java.rmi.* ;

public interface MessageWriter extends Remote {

void writeMessage(String s) throws RemoteException ; }

This interface defines a single remote method, writeMessage().

Page 13: Advanced Programming

java.rmi.Remote

13

The interface java.rmi.Remote is a marker interface.

It declares no methods or fields; however, extending it tells the RMI system to treat the interface concerned as a remote interface.• In particular we will see that the rmic compiler

generates extra code for classes that implement remote interfaces.

• This code allows their methods to be called remotely.

Page 14: Advanced Programming

java.rmi.RemoteException

14

Requiring all remote methods be declared to throw RemoteException was a philosophical choice by the designers of RMI.

RMI makes remote invocations look syntactically like local invocation. In practice, though, it cannot be defend from problems unique to distributed

computing—unexpected failure of the network or remote machine.

Forcing the programmer to handle remote exceptions helps to encourage thinking about how these partial failures should be dealt with.

See the influential essay: “A Note on Distributed Computing” by Waldo et al, republished in The Jini Specification: http://java.sun.com/docs/books/jini

Page 15: Advanced Programming

The Remote Object

15

A remote object is an instance of a class that implements a remote interface.

Most often this class also extends the library class java.rmi.server.UnicastRemoteObject.

This class includes a constructor that exports the object to the RMI system when it is created, thus making the object visible to the outside world.

Usually you will not have to deal with this class explicitly—your remote object classes just have to extend it.

One fairly common convention is to name the class of the remote object after the name of the remote interface it implements, but append “Impl” to the end.

Page 16: Advanced Programming

A Remote Object Implementation Class

16

The file MessageWriterImpl.java contains the class declaration:

import java.rmi.* ; import java.rmi.server.* ;

public class MessageWriterImpl extends UnicastRemoteObject implements MessageWriter {

public MessageWriterImpl() throws RemoteException { }

public void writeMessage(String s) throws RemoteException { System.out.println(s) ; } }

Page 17: Advanced Programming

Compiling the Remote Object Class

17

To compile classes that implement Remote, you must use the rmic compiler. The reasons will be discussed later. For example:

rabie$ rmic MessageWriterImpl

Page 18: Advanced Programming

Client and Server Programs

18

We have completed the Java files for the remote object class itself, but we still need the actual client and server programs that use this class.

In general there are some pieces of administrivia one has to deal with—publishing class files and installing security managers.

We initially make the simplifying assumption that both client and server have copies of all class files for MessageWriter.• Then “publishing class files” is not an issue, and we also

don’t need a security manager, because all code is “local”, and therefore trusted.

Page 19: Advanced Programming

A Server Program

19

We assume the file HelloServer.java contains the class declaration:

import java.rmi.* ;

public class HelloServer { public static void main(String [] args) throws Exception {

MessageWriter server = new MessageWriterImpl() ;

Naming.rebind(“messageservice”, server) ; }}

Page 20: Advanced Programming

Remarks

20

This program does two things:• It creates a remote object with local name server.

• It publishes a remote reference to that object with external name “MessageWriter”.

The call to Naming.rebind() places a reference to server in an RMI registry running on the local host (i.e., the host where the HelloServer program is run).

Client programs can obtain a reference to the remote object by looking it up in this registry.

Page 21: Advanced Programming

A Client Program

21

We assume the file HelloClient.java contains the class declaration:

import java.rmi.* ;

public class HelloClient { public static void main(String [] args) throws Exception {

MessageWriter server = (MessageWriter) Naming.lookup(

“rmi://rabieramadan.org/messageservice”) ;

server.writeMessage(“Hello, other world”) ; }}

Page 22: Advanced Programming

Remarks

22

Again the program does two things:• It looks up a reference to a remote object with external name

“MessageWriter”, and stores the returned reference with local name server.

• Finally (!), it invokes the remote method, writeMessage(), on server.

The call to Naming.lookup() searches in a remote RMI registry. Its argument is a URL, with protocol tag “rmi”.

This example assumes the remote object lives on the host “rabieramadan.org”, and has been registered in the default RMI registry (which happens to listen on port 1099) on that machine.

Page 23: Advanced Programming

Compiling and Running the Example

23

Compile HelloServer and HelloClient on their respective hosts, e.g.:

sirah$ javac HelloServer

merlot$ javac HelloClient

Either ensure client and server share the current directory, or copy all files with names of the form MessageWriter * .class to the client’s current directory.

Page 24: Advanced Programming

Running HelloClient/HelloServer

24

Page 25: Advanced Programming

See you Next time

25