RMI Introduction
Java RMI •
– focus on data communication, no semantics – user defined protocols are necessary which define semantics – language interoperability
Content 1. RMI Architecture 2. Using RMI: Step by Step 3. Marshalling 4. Remote Class Loading
•
14 January 2004
•
only Java objects can participate, no language interoperability
– remote objects can be used as if they were local ones support of distributed garbage collection! 2
Excursion: Proxy Pattern •
Skeleton
•
Proxy Pattern [Gamma et al]
(stub = proxy)
– representation of client object on server side – calls the services on server object – knows how to forward results – skeleton object has a local reference to server object – [obsolete in version 1.2]
Client
Server Stub
14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
14 January 2004
Stub & Skeleton Layer
– representation of server object on client side – implements the same interface as server object – knows how to forward method calls – client object has a local reference to stub object
RMI (since JDK 1.1) – Java-only distributed object system:
1
Stub
SOAP – typed transport layer – automatic generation of server-side & client-side proxies – language interoperability
Prof. Dr. Dominik Gruntz FH Aargau
[email protected] http://www.cs.fh-aargau.ch/~gruntz
•
Socket Solution
– proxy represents real object – proxy and real object implement the same interface – interface must only contain methods
Skeleton
(C) Fachhochschule Aargau Nordwestschweiz
3
14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
4
RMI Architecture: Summary
Remote Reference Layer
•
•
RMI Layers – stub/skeleton layer
– interprets and manages references to remote objects – leasing for distributed garbage collection
objects used by client and server applications
– remote reference layer creation/management of remote references distributed garbage collection
– transport protocol layer
•
Naming/Registery Service
•
Invocation Semantics – 1.1: unicast / point-to-point – 1.2: support for activation of dormant remote service objects
binary data protocol
Remote Object Activation RMI will instantiate a dormant object and restore its state from disk
– By using a layered architecture each of the layers could be enhanced or replaced without affecting the rest of the system. For example, the transport layer could be replaced by a UDP/IP layer without affecting the upper layers. 14 January 2004
RemoteRef
(C) Fachhochschule Aargau Nordwestschweiz
– not yet: multicast semantics
5
14 January 2004
7
14 January 2004
(C) Fachhochschule Aargau 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)
14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
(C) Fachhochschule Aargau Nordwestschweiz
8
Using RMI
1. Remote Interfaces
1. Define interfaces for remote classes
•
Remote – all remotable interfaces must extend interface java.rmi.Remote (tagging interface) – all methods must throw a java.rmi.RemoteException (extension of java.io.IOException)
2. Create and compile implementation classes for the remote classes 3. Create stub and skeleton classes using the rmic command 4. Create and compile the server application (registration) •
5. Create and compile a client program to access the remote objects
package rmi.calculator; public interface Calculator public long add(long a, public long sub(long a, public long mul(long a, public long div(long a, }
6. Start the RMI Registry and the server application 7. Test the client
14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
9
2. Implementation •
java.rmi.Remote { throws java.rmi.RemoteException; throws java.rmi.RemoteException; throws java.rmi.RemoteException; throws java.rmi.RemoteException;
14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
10
package rmi.calculator; public class CalculatorImpl extends java.rmi.server.UnicastRemoteObject implements Calculator { public CalculatorImpl() throws java.rmi.RemoteException { } public long add(long a, long b) { return a + b; } public long sub(long a, long b) { return a - b; } public long mul(long a, long b) { return a * b; } public long div(long a, long b) { return a / b; } }
UnicastRemoteObject
link to RMI system base class performs RMI linking and remote object initialization constructor may throw a RemoteException
Activatable – base class to be used for activatable objects
14 January 2004
extends long b) long b) long b) long b)
2. Implementation: Example
– implementation must implement the defined remote interface – implementation extends class UnicastRemoteObject or calls explicitly UnicastRemoteObject.exportObject
•
Example
(C) Fachhochschule Aargau Nordwestschweiz
11
14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
12
3. Create stubs / skeletons
4. Server
•
•
rmic rmi.calculator.CalculatorImpl generates class files – CalculatorImpl_Stub – CalculatorImpl_Skel
– creates an instance – registers object in naming service
Remote
RemoteObject
Calculator
•
RemoteStub
Naming / Registry service – RMI can use different naming services Simple service: RMI Registry JNDI
UnicastRemoteObject
– Location of registry
Skeleton
CalculatorImpl_Stub
LocateRegistry.getRegistry (with host/port as optional parameters) URL: rmi://hostname:port/servicename • default port: 1099 rmi://localhost/Bank • default host: localhost rmi://:1099/Bank rmi:///Bank • default protocol: rmi: ///Bank
<
>
CalculatorImpl
RMI service must be hosted in a server process
<>
CalculatorImpl_Skel
14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
13
4. Server: Example
14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
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) { try { Calculator c = (Calculator)Naming.lookup( "rmi://localhost/CalculatorService"); System.out.println( c.sub(4, 3) ); System.out.println( c.add(4, 5) ); System.out.println( c.mul(3, 6) ); System.out.println( c.div(9, 3) ); } catch (MalformedURLException e) { System.out.println("MalformedURLException "+e); } catch (RemoteException e) { System.out.println("RemoteException "+e); } catch (NotBoundException e) { System.out.println("NotBoundException "+e); } catch (java.lang.ArithmeticException e) { System.out.println("ArithmeticException "+e); } } }
public class Server { public static void main(String args[]) { try { Calculator c = new CalculatorImpl(); Naming.rebind("CalculatorService", c); } catch (Exception e) { System.out.println("Trouble: " + e); } } }
– bind/rebind is only possible on localhost
14 January 2004
14
(C) Fachhochschule Aargau Nordwestschweiz
15
14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
16
4./5. Naming / Registry
6. Start of RMI-Registry & Server
•
• rmiregistry <port>
bind
(String, Remote)
can only be called on localhost – binds a specified name to a remote object – may throw AlreadyBoundException
•
rebind
•
unbind
•
lookup
•
list
– default port: 1099 – error if port is already used by another process (e.g. another rmiregistry) – daemon has to be started in directory which contains used classes or the classes have to be on the CLASSPATH
(String, Remote)
can only be called on localhost – rebinds a specified name to a remote object
(String) can only be called on localhost – destroys the binding for a specified name
• java rmi.calculator.Server – registers instance under name CalculatorService in local registry
(String)
– returns a reference to the remote object associated with the name
(String)
– lists the names present in a registry
14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
17
import java.rmi.*; public class ListRMIRegistry { public static void main(String[] args) { if (args.length>0) { String host = args[0]; if(args.length>1){ try{host = host + ":" + Integer.parseInt(args[1]);} catch(NumberFormatException e){} } try{String s[] = Naming.list("rmi://"+host); for(int i=0; i<s.length; i++)System.out.println(s[i]); } catch(java.net.MalformedURLException e){ System.err.println("MalformedURL in host: ” + host); } catch(RemoteException e){ System.err.println("Cannot contact registry on: ” + host); } } else { System.out.println("Usage: java ListRMIREgistry [<port>]"); } } } (C) Fachhochschule Aargau Nordwestschweiz
(C) Fachhochschule Aargau Nordwestschweiz
18
Marshalling - How are parameters transferred to remote object?
Example: List of all registered objects
14 January 2004
14 January 2004
•
Primitive Parameters – passed by value, in a machine-independent format
•
Serializable Objects – serializable objects are copied – => call by value
•
Remote Object Parameters – only the reference to the remote object is passed, i.e. a new proxy is generated – => call by reference
•
Non-Serializable/Remote Objects – cannot be transferred – checked at runtime (not by rmic!)
19
14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
20
Factory Classes
Callback
•
•
Often not all objects can be registered Registry local = LocateRegistry.getRegistry(); 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”));
– e.g. UI notification (=> Observer Pattern ! )
Client
– number of used objects is not known – registry has to be kept consistent with database
•
Callbacks are necessary for complex communications
Server
– 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 14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
21
14 January 2004
Example: QuoteServer
Client behind a Router / NAT
•
•
IQuoteServer interface public interface IQuoteServer extends java.rmi.Remote { public void addQuote(String quote) throws java.rmi.RemoteException; public void addQuoteListener(IQuoteListener c) throws java.rmi.RemoteException; }
•
IQuoteListener interface
(C) Fachhochschule Aargau Nordwestschweiz
22
NAT Problem info> java rmi.quotes.QuoteServer QuoteServer.main: creating registry QuoteServer.main: creating server QuoteServer.main: binding server QuoteServer bound in registry addQuoteLsitener called rmi.quotes.QuoteListener_Stub[RemoteStub [ref: [endpoint: [192.168.0.3:1177](remote),objID:[f6a746:f9f2a5e362:-8000, 0]]]] client must have disconnected!
public interface IQuoteListener extends java.rmi.Remote { public void update(String q) throws java.rmi.RemoteException; }
14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
23
14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
24
Dynamically Loaded Classes
Dynamically Loaded Classes (2)
•
•
Required classes can be loaded over the network – e.g. provided by a webserver – other protocols are also possible (file://, ftp://, ….)
•
– specify codebase for downloading class files java -Djava.rmi.server.codebase=http://loki.cs.fhaargau.ch:8080 rmi.calculator.Server
•
Class Loading / Security
java -Djava.security.policy=policy.txt rmi.calculator.Client loki.cs.fh-aargau.ch
ClassLoaders: Seminar Talk !!!
– security manager has to support remote class loading
– policy file
System.setSecurityManager(new RMISecurityManager())
grant { // connect to or accept connections on unprivileged ports // (ports greater than 1024) on host loki.cs.fh-aargau.ch permission java.net.SocketPermission ”info.cs.fh-aargau.ch:1024-", "connect,resolve"; };
Start of RMI-Registry – rmiregistry must not contain needed classes in its path!!!
(C) Fachhochschule Aargau Nordwestschweiz
14 January 2004
25
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/
14 January 2004
Start of Client – permission to access server has to be provided (due to security manager)
– a special class loader is provided: RMIClassLoader
•
Start of Server
(C) Fachhochschule Aargau Nordwestschweiz
27
14 January 2004
(C) Fachhochschule Aargau Nordwestschweiz
26