Rmi
description
Transcript of Rmi
-
Java-RMI Lab
-
Outline Let first builds a simple home-made
framework This is useful to understand the main
issues We see later how java-rmi works and how
it solves the same issues
-
Generic architecture
object A object BskeletonRequestproxy for B
Reply
CommunicationRemote Remote referenceCommunication
modulemodulereference module module
for Bs class& dispatcher
remoteclient server
servant
Local proxy: Remote methods appear as local Remote Reference Module: local-remote object mapDispatcher: Dispatch the request to a SkeletonSkeleton: Invoke the method on B; send back the reply
-
Implementation
object Aproxy for Counter
Remote Communication
modulereference module
client
Wed like something like this
Client Object
Receive input
Call method on the proxy
-
Implementation
public int inc(int value){//marshal value
//prepare a request
//send the request to the Server//The address is in the//Remote Reference Module
// on in a remote reference data structure
}
public int dec(int value){
}
object Aproxy for Counter
Remote Communication
modulereference module
client
Proxy
-
Implementation Communication module
object Bskeleton
Communication Remote reference
modulemodule
for Bs class
& dispatcher
remote
server
servant
CommunicationModuleRegister an object
(uses Object Table)
Initialize
Receive request
Dispatch to the Skeleton
-
Implementation Skeleton
object Bskeleton
Communication Remote reference
modulemodule
for Bs class
& dispatcher
remote
server
servant
Skeleton &Dispatcher
Initialize(bound to a Counter)
Receive request
Dispatch to the Object
Send the reply
-
Implementation details See RMI-LAB on the web site
Exercise: add a new object
-
Lesson learned Key (and borrowing) aspects Managing communication Managing remote reference Implements support modules (proxy,
skeleton)
-
Java-rmi solution Managing communication
Embedded into the JVM Managing remote reference
Rmiregistry, remote reference layer Implements support modules (proxy,
skeleton) Proxy: automatically generated from the code Skeleton: no longer needed thanks to
reflection (see later)
-
Lab1: Remote counterimport java.rmi.*; //Import rmi API
public interface Counter extends Remote //makes Counter a remote interface{int inc(int i) throws RemoteException; //manage or throws this exception.int dec(int i) throws RemoteException;
}
-
Step2: interface implementationimport java.rmi.*;
public class Counter_impl implements Counter{
private int counter;
public Counter_impl() throws RemoteException {counter = 0;}public int inc(int i) throws RemoteException {counter++;return counter;}public int dec(int i) throws RemoteException {counter--;return counter;}
}
-
import java.rmi.RemoteException;import java.rmi.server.UnicastRemoteObject;import java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;public class Server{
public static void main(String args[]) throws RemoteException{
//Create a remote object..Counter c = new Counter_impl() ;
Counter stub = (Counter)UnicastRemoteObject.exportObject(c, 0);
//bind "counter" to the stubRegistry registry = LocateRegistry.getRegistry();registry.rebind("counter", stub);System.out.println("Counter bound");
}}
Export object = It can receive requests
-
Client import java.rmi.RemoteException;import java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;public class Client{
public static void main(String args[]){try{
Registry registry = LocateRegistry.getRegistry(args[0]);Counter c = (Counter) registry.lookup("counter");int i = c.inc(10);System.out.println(i);
}catch (Exception e){e.printStackTrace();}}
}
-
Running the example On the same machine via classpath
Run rmiregisrty: rmiregistry (make sure CLASSPATH is unset)
Run the server: java -classpath . Server Run the client: java -classpath . Client localhost
-
Class downloading JAVA-RMI allows to
download the definition of an object's class if the class is not defined in the receiver's Java virtual machine.
Classes definitions are typically made network accessible through a web server
-
Class downloading Class downloading are
controlled by a Security manager
Without a security manager installed, RMI will not download classes, other than from the local class path.
This restriction ensures that the operations performed by downloaded code are subject to a security policy.
-
Installing Security Manager
if (System.getSecurityManager() == null) { System.setSecurityManager(new SecurityManager());
}
java -Djava.rmi.server.codebase="http://xxxx. " -Djava.security.policy=java.policy
codebase = Where classes are network accessible
policy file
grant { permission java.security.AllPermission; };
-
LAB2 Account example
-
LAB3 Shared whiteboard