Professional Documents
Culture Documents
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
Client Server
Transport Layer
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 }
}
• 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
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
Conclusion
• Demo Calculator
• Further Topic
– remote activation (rmid) since JKD 1.2
• Übung 5
– RMI Bank