You are on page 1of 7

RMI Introduction

Java RMI
• Socket Solution
– focus on data communication, no semantics
Content – user defined protocols are necessary which define semantics
– language interoperability
1. RMI Architecture
• SOAP
2. Using RMI: Step by Step
– typed transport layer
3. Marshalling – automatic generation of server-side & client-side proxies
4. Remote Class Loading – language interoperability

Prof. Dr. Dominik Gruntz • RMI (since JDK 1.1)


FH Aargau – Java-only distributed object system:
ƒ only Java objects can participate, no language interoperability
gruntz@fh-aargau.ch
– remote objects can be used as if they were local ones
http://www.cs.fh-aargau.ch/~gruntz ƒ support of distributed garbage collection!

(C) Fachhochschule Aargau

14 January 2004 1 14 January 2004 Nordwestschweiz


2

Stub & Skeleton Layer Excursion: Proxy Pattern

• Stub • Skeleton • Proxy Pattern [Gamma et al] (stub = proxy)


– representation of server object on – representation of client object on
client side server side
– implements the same interface as – calls the services on server object
server object
– knows how to forward method calls – knows how to forward results
– client object has a local reference – skeleton object has a local
to stub object reference to server object
– [obsolete in version 1.2]

Client Server

Stub Skeleton – proxy represents real object


– proxy and real object implement the same interface
– interface must only contain methods

(C) Fachhochschule Aargau (C) Fachhochschule Aargau


14 January 2004 Nordwestschweiz
3 14 January 2004 Nordwestschweiz
4
RMI Architecture: Summary Remote Reference Layer

• RMI Layers • RemoteRef


– stub/skeleton layer – interprets and manages references to remote objects
ƒ objects used by client and server – leasing for distributed garbage collection
applications
– remote reference layer
• Naming/Registery Service
ƒ creation/management of remote references
ƒ distributed garbage collection
– transport protocol layer • Invocation Semantics
ƒ binary data protocol – 1.1: unicast / point-to-point
– 1.2: support for activation of dormant remote service objects
– By using a layered architecture each of the layers could be enhanced or ƒ Remote Object Activation
replaced without affecting the rest of the system. ƒ RMI will instantiate a dormant object and restore its state from disk
For example, the transport layer could be replaced by a UDP/IP layer – not yet: multicast semantics
without affecting the upper layers.
(C) Fachhochschule Aargau (C) Fachhochschule Aargau
14 January 2004 Nordwestschweiz
5 14 January 2004 Nordwestschweiz
6

Transport Layer

• Connection between JVMs


– stream-based network connections over TCP/IP
– Java Remote Method Protocol on top of TCP/IP
– since 1.3: RMI-IIOP is available (Internet Inter-ORB protocol, OMG)

(C) Fachhochschule Aargau (C) Fachhochschule Aargau


14 January 2004 Nordwestschweiz
7 14 January 2004 Nordwestschweiz
8
Using RMI 1. Remote Interfaces

1. Define interfaces for remote classes • Remote


2. Create and compile implementation classes for the remote – all remotable interfaces must extend interface java.rmi.Remote
(tagging interface)
classes
– all methods must throw a java.rmi.RemoteException
3. Create stub and skeleton classes using the rmic command (extension of java.io.IOException)
4. Create and compile the server application (registration)
• Example
5. Create and compile a client program to access the remote
package rmi.calculator;
objects public interface Calculator extends java.rmi.Remote {
public long add(long a, long b) throws java.rmi.RemoteException;
6. Start the RMI Registry and the server application
public long sub(long a, long b) throws java.rmi.RemoteException;
7. Test the client public long mul(long a, long b) throws java.rmi.RemoteException;
public long div(long a, long b) throws java.rmi.RemoteException;
}

(C) Fachhochschule Aargau (C) Fachhochschule Aargau


14 January 2004 Nordwestschweiz
9 14 January 2004 Nordwestschweiz
10

2. Implementation 2. Implementation: Example

package rmi.calculator;
• UnicastRemoteObject
public class CalculatorImpl extends java.rmi.server.UnicastRemoteObject
– implementation must implement the defined remote interface implements Calculator {
– implementation extends class UnicastRemoteObject or calls explicitly public CalculatorImpl() throws java.rmi.RemoteException { }
UnicastRemoteObject.exportObject public long add(long a, long b) {
ƒ link to RMI system return a + b;
}
ƒ base class performs
RMI linking and remote object public long sub(long a, long b) {
initialization return a - b;
}
ƒ constructor may throw
public long mul(long a, long b) {
a RemoteException
return a * b;
}
• Activatable public long div(long a, long b) {
– base class to be used for return a / b;
activatable objects }
}

(C) Fachhochschule Aargau (C) Fachhochschule Aargau


14 January 2004 Nordwestschweiz
11 14 January 2004 Nordwestschweiz
12
3. Create stubs / skeletons 4. Server

• rmic rmi.calculator.CalculatorImpl generates class files • RMI service must be hosted in a server process
– CalculatorImpl_Stub – creates an instance
– CalculatorImpl_Skel Remote – registers object in naming service

RemoteObject • Naming / Registry service


Calculator RemoteStub
– RMI can use different naming services
ƒ Simple service: RMI Registry
UnicastRemote- ƒ JNDI
Object – Location of registry
Skeleton
CalculatorImpl_Stub ƒ LocateRegistry.getRegistry (with host/port as optional parameters)
ƒ URL: rmi://hostname:port/servicename
<<rmic>>
• default port: 1099 rmi://localhost/Bank
CalculatorImpl CalculatorImpl_Skel • default host: localhost rmi://:1099/Bank rmi:///Bank
<<rmic>>
• default protocol: rmi: ///Bank
(C) Fachhochschule Aargau (C) Fachhochschule Aargau
14 January 2004 Nordwestschweiz
13 14 January 2004 Nordwestschweiz
14

4. Server: Example 5. Client: Example

package rmi.calculator; import java.rmi.*;


import java.net.*;
import java.rmi.Naming; public class CalculatorClient {
public static void main(String[] args) {
public class Server { try {
Calculator c = (Calculator)Naming.lookup(
public static void main(String args[]) { "rmi://localhost/CalculatorService");
try { System.out.println( c.sub(4, 3) );
Calculator c = new CalculatorImpl(); System.out.println( c.add(4, 5) );
System.out.println( c.mul(3, 6) );
Naming.rebind("CalculatorService", c); System.out.println( c.div(9, 3) );
} }
catch (Exception e) { catch (MalformedURLException e) {
System.out.println("MalformedURLException "+e);
System.out.println("Trouble: " + e); }
} catch (RemoteException e) {
} System.out.println("RemoteException "+e);
}
} catch (NotBoundException e) {
System.out.println("NotBoundException "+e);
}
– bind/rebind is only possible on localhost catch (java.lang.ArithmeticException e) {
System.out.println("ArithmeticException "+e);
}
}
}
(C) Fachhochschule Aargau (C) Fachhochschule Aargau
14 January 2004 Nordwestschweiz
15 14 January 2004 Nordwestschweiz
16
4./5. Naming / Registry 6. Start of RMI-Registry & Server

• bind (String, Remote) can only be called on localhost • rmiregistry <port>


– binds a specified name to a remote object – default port: 1099
– may throw AlreadyBoundException – error if port is already used by another process (e.g. another rmiregistry)
• rebind (String, Remote) can only be called on localhost – daemon has to be started in directory which contains used classes or the
– rebinds a specified name to a remote object classes have to be on the CLASSPATH
• unbind (String) can only be called on localhost
– destroys the binding for a specified name • java rmi.calculator.Server
• lookup (String) – registers instance under name CalculatorService in local registry
– returns a reference to the remote object associated with the name
• list (String)
– lists the names present in a registry

(C) Fachhochschule Aargau (C) Fachhochschule Aargau


14 January 2004 Nordwestschweiz
17 14 January 2004 Nordwestschweiz
18

Marshalling - How are parameters


Example: List of all registered objects transferred to remote object?

import java.rmi.*;
public class ListRMIRegistry { • Primitive Parameters
public static void main(String[] args) {
if (args.length>0) {
– passed by value, in a machine-independent format
String host = args[0]; • Serializable Objects
if(args.length>1){
try{host = host + ":" + Integer.parseInt(args[1]);} – serializable objects are copied
catch(NumberFormatException e){}
} – => call by value
try{String s[] = Naming.list("rmi://"+host);
for(int i=0; i<s.length; i++)System.out.println(s[i]); • Remote Object Parameters
} – only the reference to the remote object is passed,
catch(java.net.MalformedURLException e){
System.err.println("MalformedURL in host: ” + host); i.e. a new proxy is generated
}
catch(RemoteException e){ – => call by reference
System.err.println("Cannot contact registry on: ” + host); • Non-Serializable/Remote Objects
}
} – cannot be transferred
else {
System.out.println("Usage: java ListRMIREgistry <host> [<port>]"); – checked at runtime (not by rmic!)
}
}
}
(C) Fachhochschule Aargau (C) Fachhochschule Aargau
14 January 2004 Nordwestschweiz
19 14 January 2004 Nordwestschweiz
20
Factory Classes Callback

• Often not all objects can be registered • Callbacks are necessary for complex communications
Registry local = LocateRegistry.getRegistry(); – e.g. UI notification (=> Observer Pattern ! )
local.bind(“147-332-01”, new Account(“D. Gruntz”));
local.bind(“147-332-02”, new Account(“H-P. Oser”));
local.bind(“147-332-03”, new Account(“P. Kamm”));
local.bind(“147-332-04”, new Account(“S. Huber”)); Client Server
– number of used objects is not known
– registry has to be kept consistent with database
– Client implements Remote objects and installs it in server
– Client becomes a server
• Factory Object
public interface Bank extends Remote {
public Account getAccount(String number) throws RemoteException; • Example: QuoteServer
}
– Account has to be an interface which extends Remote
– Client Proxy is automatically generated

(C) Fachhochschule Aargau (C) Fachhochschule Aargau


14 January 2004 Nordwestschweiz
21 14 January 2004 Nordwestschweiz
22

Example: QuoteServer Client behind a Router / NAT

• IQuoteServer interface • NAT Problem


public interface IQuoteServer extends java.rmi.Remote {
public void addQuote(String quote) throws java.rmi.RemoteException;
info> java rmi.quotes.QuoteServer
public void addQuoteListener(IQuoteListener c)
QuoteServer.main: creating registry
throws java.rmi.RemoteException;
QuoteServer.main: creating server
}
QuoteServer.main: binding server
QuoteServer bound in registry
• IQuoteListener interface addQuoteLsitener called
public interface IQuoteListener extends java.rmi.Remote { rmi.quotes.QuoteListener_Stub[RemoteStub [ref: [endpoint:
public void update(String q) throws java.rmi.RemoteException; [192.168.0.3:1177](remote),objID:[f6a746:f9f2a5e362:-8000, 0]]]]
} client must have disconnected!

(C) Fachhochschule Aargau (C) Fachhochschule Aargau


14 January 2004 Nordwestschweiz
23 14 January 2004 Nordwestschweiz
24
Dynamically Loaded Classes Dynamically Loaded Classes (2)

• Required classes can be loaded over the network • Start of Server


– e.g. provided by a webserver – specify codebase for downloading class files
– other protocols are also possible (file://, ftp://, ….) ƒ java -Djava.rmi.server.codebase=http://loki.cs.fh-
aargau.ch:8080 rmi.calculator.Server

• Class Loading / Security • Start of Client


– permission to access server has to be provided (due to security manager)
– a special class loader is provided: RMIClassLoader
ƒ java -Djava.security.policy=policy.txt
ƒ ClassLoaders: Seminar Talk !!!
rmi.calculator.Client loki.cs.fh-aargau.ch
– security manager has to support remote class loading
– policy file
ƒ System.setSecurityManager(new RMISecurityManager())
grant {
• Start of RMI-Registry // connect to or accept connections on unprivileged ports
// (ports greater than 1024) on host loki.cs.fh-aargau.ch
– rmiregistry must not contain needed classes in its path!!! permission java.net.SocketPermission
”info.cs.fh-aargau.ch:1024-", "connect,resolve";
};
(C) Fachhochschule Aargau (C) Fachhochschule Aargau
14 January 2004 Nordwestschweiz
25 14 January 2004 Nordwestschweiz
26

Conclusion

• Demo Calculator

• Further Topic
– remote activation (rmid) since JKD 1.2

• Übung 5
– RMI Bank

• Eclipse RMI Plugin


– http://sourceforge.net/projects/lunar-eclipse/
– http://www.genady.net/rmi/

(C) Fachhochschule Aargau


14 January 2004 Nordwestschweiz
27

You might also like