L6-RMIProgramming
L6-RMIProgramming
http://java.sun.com/docs/books/tutorial/rmi/
Outline
Introduction to RMI
RMI Architecture
RMI Programming and a Sample Example:
Server-Side RMI programming
Client-Side RMI programming
Advanced RMI Concepts
Security Policies
Exceptions
Dynamic Loading
A more advanced RMI application
File Server
2
What is Java RMI?
3
RMI Architecture & Components
Remote reference module is responsible for providing addressing to the
proxy (stub) object
Proxy is used to implement a stub and provide transparency to the client. It is
invoked directly by the client (as if the proxy itself was the remote object),
and then marshal the invocation into a request
Communication module is responsible for networking
Dispatcher selects the proper skeleton and forward message to it
Skeleton un-marshals the request and calls the remote object
client server
remote
skeleton object B
object Aproxy for B Request & dispatcher
for B’s class
Reply
servant
Remote Communication CommunicationRemote reference
4 module module module
reference module
Invocation Lifecycle
Client Server
Stub Skeleton
Serializes Receives, Receives, Serialises
arguments, deserialises deserialises response,
transmit
2 7
response arguments
3 6 transmit
Network
5
Steps of implementing an RMI application
6
RMI Programming and Examples
Application Design
Remote Interface
Exposes the set of methods and properties available
Defines the contract between the client and the server
Constitutes the root for both stub and skeleton
Servant component
Represents the remote object (skeleton)
Implements the remote interface
Server component
Main driver that makes available the servant
It usually registers with the naming service
Client component
7
Example application – Hello World
Server side
Create a HelloWorld interface
Implement HelloWorld interface with methods
Create a main method to register the HelloWorld
service in the RMI Name Registry
Generate Stubs and Start RMI registry
Start Server
Client side
Write a simple Client with main to lookup
HelloWorld Service and invoke the methods
8
Explanation: Server Side
9
Server Side : Define an interface
(cs652/HelloWorld.java)
package cs652;
import java.rmi.Remote;
import java.rmi.RemoteException;
10
Implementing a remote
interface(cs652/HelloWorldImpl.java)
package cs652;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class HelloWorldImpl extends UnicastRemoteObject implements HelloWorld {
public HelloWorldImpl() throws RemoteException {
super();
}
public String sayHello(String who) throws RemoteException {
return "Hello "+who+" from your friend RMI 433-652 :-)";
}
public static void main(String[] args) {
String hostName = "localhost";
String serviceName = "HelloWorldService";
if(args.length == 2){
hostName = args[0];
serviceName = args[1];
}
try{
HelloWorld hello = new HelloWorldImpl();
Naming.rebind("rmi://"+hostName+"/"+serviceName, hello);
System.out.println("HelloWorld RMI Server is running...");
}catch(Exception e){}
}
}11
Generate Stubs and Start RMI registry
Compile
javac cs652/HelloWorld.java
javac cs652/HelloWorldImpl.java
Generate Stubs
rmic cs652.HelloWorldImpl
Start RMI registry
start rmiregistry (windows)
rmiregistry 10000 & (unix)
Start HelloWorld Server
java cs652.HelloWorldImpl
java cs652.HelloWorldImpl localhost:10000
HelloWorldService
12
Explanation: Client Side
Get
Lookup Service
Service Reference
Client
Invoke RMI Remote Object
Service
13
Client Side : A Simple Client
(cs652/RMIClient.java)
package cs652;
import java.rmi.Naming;
public class RMIClient {
public static void main(String[] args) {
String hostName = "localhost";
String serviceName = "HelloWorldService";
String who = "Raj";
if(args.length == 3){
hostName = args[0];
serviceName = args[1];
who = args[2];
}else if(args.length == 1){
who = args[0];
}
try{
HelloWorld hello = (HelloWorld)Naming.lookup("rmi://"+hostName+"/"+serviceName);
System.out.println(hello.sayHello(who));
}catch(Exception e){
e.printStackTrace();
}
}
}
14
Compile and Run
Compile
javac cs652/RMIClient.java
Run it
java cs652.RMIClient
java cs652.RMIClient localhost HelloWorldService
Raj
java cs652.RMIClient manjra.cs.mu.oz.au:10000
HelloWorldService Raj
15
Security Manager
16
Security Manager (cont.)
17
File: “local.policy” contents
Specific permissions:
grant {
permission java.net.SocketPermission "*:1024-65535","connect,accept";
permission java.io.FilePermission "/home/globus/RMITutorial/-", "read";
};
18
Exceptions
19
Passing objects
20
RMI Dynamic Class Loading
21
Sample scenario : File Server
23
Programming File Browser
24
Run it!
Two steps
Start the server
start rmiregistry (be aware of your classpath)
java cs652.rmi.server.RMIFileServer
Start the client
java -Djava.security.manager -Djava.security.policy=local.policy
cs652.demo.RMIFileBrowser
Note: Required classes including the generated stubs must be visible
by java classloader.
25
Potential error - no security manager!
26
Successful execution
27
Summary: RMI Programming
28