You are on page 1of 16

Overview

1 Remote Method Invocation

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 1 / 16


Remote Method Invocation

Java RMI

RMI (Remote Method Invocation)

Access distributed objects (almost) identically to local objects


Same syntax (arguments, return values, etc.)
Same semantics (exceptions)
Scope
Only in Java
Significant changes in Java 1.2 (incompatible to earlier RMI)
Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 2 / 16
Remote Method Invocation

RMI Interface

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 3 / 16


Remote Method Invocation

RMI Architecture

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 4 / 16


Remote Method Invocation

Transparency

To the client, a remote object appears exactly like a local object


This is possible thanks to interfaces
You write the interface to the remote object
You write the implementation for the remote object
RMI automatically creates a stub class which implements the remote
object interface
The client accesses the stub exactly the same way it would access a
local copy of the remote object

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 5 / 16


Remote Method Invocation

The RMI Registry

Java RMI needs a naming service (like Sun RPC’s port mapper)
Servers register contact address information
Clients can locate servers
This is called RMI Registry
Unlike in RPC, you must start the RMI Registry yourself
It is a program called rmiregistry
By default runs on port 1099 (but you can specify another port
number: rmiregistry < port nb >)
Programs access the registry via the java.rmi.Naming class

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 6 / 16


Remote Method Invocation

An RMI Example

To write a minimalist RMI program you must write:


An interface for the remote object: Remote.java
An implementation for the remote object: RemoteImpl.java
A server which will run the remote object: RemoteServer.java
A client to access the server: RemoteClient.java
The RMI compiler rmic will generate the rest:
For Java version up to 1.1
A client stub: RemoteImpl Stub.class (already compiled)
A server skeleton: RemoteImpl Skel.class (already compiled)
For Java version 1.2 ... 1.4 (Java 2 ... Java 4)
A single stub, used for both client and server: RemoteImpl Stub.class
For Java 1.5 (Java 5) and higher
Nothing is needed!
Stubs and skeletons are included in the Java library

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 7 / 16


Remote Method Invocation

An RMI Example (continued)

Start by writing the interface for the remote object: Calculator.java

p u b l i c i n t e r f a c e C a l c u l a t o r e x t e n d s j a v a . r m i . Remote {
p u b l i c l o n g add ( l o n g a , l o n g b ) t h r o w s j a v a . r m i . R e m o t e E x c e p t i o n ;
p u b l i c l o n g sub ( l o n g a , l o n g b ) throws j a v a . rmi . RemoteException ;
}

You must respect a few rules


The interface must extend the java.rmi.Remote interface
All methods must throw the java.rmi.RemoteException exception
Compile the interface
javac Calculator . java

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 8 / 16


Remote Method Invocation

An RMI Example (continued)

Write an implementation for the remote object: CalculatorImpl.java

public c l a s s CalculatorImpl extends


j a v a . rmi . s e r v e r . UnicastRemoteObject
implements C a l c u l a t o r {
// I m p l e m e n t a t i o n s must h a v e an e x p l i c i t c o n s t r u c t o r
p u b l i c C a l c u l a t o r I m p l ( ) throws j a v a . rmi . RemoteException {
super ( ) ;
}
p u b l i c l o n g add ( l o n g a , l o n g b ) t h r o w s j a v a . r m i . R e m o t e E x c e p t i o n {
return a + b ;
}
p u b l i c l o n g sub ( l o n g a , l o n g b ) throws j a v a . rmi . RemoteException {
return a − b ;
}
}

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 9 / 16


Remote Method Invocation

An RMI Example (continued)

The implementation class must respect a few constraints


It must implement the interface (of course!)
It must inherit from the java.rmi.server.UnicastRemoteObject class
It must have an explicit constructor which throws the
java.rmi.RemoteException exception
Compile the implementation class
javac CalculatorImpl . java

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 10 / 16


Remote Method Invocation

An RMI Example (continued)

If you are using Java version earlier than 5.0, generate the stub and
skeleton:
rmic C a l c u l a t o r I m p l

This generates directly the CalculatorImpl Stub.class and (for Java ¡


2.0) the CalculatorImpl Skel.class files
Already compiled to byte code, you are not expected to run javac

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 11 / 16


Remote Method Invocation

An RMI Example (continued)

Write a server program: CalculatorServer.java

i m p o r t j a v a . r m i . Naming ;
public class CalculatorServer {
public CalculatorServer () {
try {
C a l c u l a t o r c = new C a l c u l a t o r I m p l ( ) ;
Naming . r e b i n d ( ” r m i : / / l o c a l h o s t / C a l c u l a t o r S e r v i c e ” , c ) ;
} catch ( Exception e ) {
System . o u t . p r i n t l n ( ” T r o u b l e : ” + e ) ;
}
}
p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {
new C a l c u l a t o r S e r v e r ( ) ;
}
}

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 12 / 16


Remote Method Invocation

An RMI Example (continued)

The server program creates a CalculatorImpl object


It registers the object to the local RMI registry
r e b i n d ( S t r i n g name , Remote o b j )

Associates a name to an object


Names are in URL form:
rmi : // < host name > [: port]/ < service name >
The server will wait for incoming requests
Compile your server
javac CalculatorServer . java

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 13 / 16


Remote Method Invocation

An RMI Example (continued)

Write a client program: CalculatorClient.java

import j a v a . net . MalformedURLException ;


i m p o r t j a v a . r m i . Naming ;
public class CalculatorClient {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
try {
C a l c u l a t o r c = ( C a l c u l a t o r ) Naming . l o o k u p ( ” r m i : / / l o c a l h o s t / C a l c u l a t o r S e r v i c e ” ) ;
System . o u t . p r i n t l n ( c . add ( 4 , 5 ) ) ;
System . o u t . p r i n t l n ( c . s u b ( 4 , 3 ) ) ;
}
catch ( Exception e ) {
System . o u t . p r i n t l n ( ” R e c e i v e d E x c e p t i o n : ” ) ;
System . o u t . p r i n t l n ( e ) ;
}
}
}

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 14 / 16


Remote Method Invocation

An RMI Example (continued)

Before invoking the server, the client must lookup the registr
It must provide the URL for the remote service
It gets back a stub which has exactly the same interface as the server
It can use it as a local object: long x = c.add(4,5)
Compile your client:
javac CalculatorClient . java

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 15 / 16


Remote Method Invocation

An RMI Example (end!)

Try your program!!


Start the RMI Registry: rmiregistry
rmiregistry &

The registry must have access to your classes


Either start the registry in the same directory as the classes, or make
sure the directory is listed in the $CLASSPATH variable
Start your server
java CalculatorServer

java CalculatorClient

Wondimagegn D. (AAIT ) Distributed System Programming September 29, 2019 16 / 16

You might also like