Download Distributed Programming via RMI RMI Issues Architecture of RMI

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
RMI Issues
CITS3213: Concurrent Programming Part II - Topic 5
Distributed Programming via RMI
• Concurrent programs quite often involve many computers.
• From the other view: concurrency is difficult to avoid if a system
will run on many computers.
• Such systems are called distributed systems.
• Generally each computer will run part of the whole system, with
communication between the parts via a network.
• How can we make it easy to build distributed systems, and avoid
the need to program the low-level network communication?
Java VM1 :
Java VM2
.................
.................
Call A in VM2
method A
Figure 1: The scenario for an RMI
When you execute a Java program :
• It runs in the context of a Virtual Machine (the Java interpreter),
• Objects created by the program are accessed using references.
• References are meaningful only in the context of the local address
space of the JVM executing the program.
• A standard solution is to support remote procedure calls (RPCs).
• So how can you call method A of an object in JVM2 from a program executing in JVM1?
• Remote method invocation (RMI) is the object-oriented version.
• The address spaces are different and unrelated.
• The basic idea is that we should be able call methods on remote
objects in exactly the same way as for local objects.
• But, how is this possible?
There are really two issues here :
• How can you expose the services a (server) program provides (e.g.,
JVM2 above) so that they can be used from a different JVM (e.g.,
JVM1)?
• Then how do you handle the problem of accessing object references
across different JVM address spaces?
1
2
Architecture of RMI
Architecture of RMI
Client
Client
Server
Make normal Java
method call
Define and implement
an interface
(extends Remote )
in Java
Use a client ”stub” :
Use a server skeleton :
converts a reference
to a string
converts string to
reference
Server
Java Program
(JVM 1)
Java program
(JVM 2)
client ”stub”
server ”skeleton”
TCP/IP
TCP/IP
(Internet)
The process of sending
calls from one
namespace to another
is called
marshaling
(Internet)
Figure 3: Architecture of RMI
• The client can locally call methods on instances of the stub class.
Figure 2: The scenario for an RMI
Steps :
• The stub code forwards this call to the server skeleton.
• The skeleton makes the corresponding call on the remote object.
1. Compile the Java client and server programs in the normal way.
• Arguments & results are marshalled over the network.
2. Generate a client stub (or proxy) and server skeleton using the
Java RMI compiler (rmic).
• Object arguments & results are converted to remote references.
3
4
Code Outline for the Server
Code Outline for the Client
First, the interface :
public interface Hello extends java.rmi.Remote{
String sayHello() throws java.rmi.RemoteException;
}
Then the server code implements the interface and extends
UnicastRemoteObject:
The client finds the remote object using a look-up service provided by
RMI. It can then access server methods defined by the server interface.
import java.rmi.*;
public class RemoteHelloClient{
void InvokeRemoteMethod(){
String message="";
try{
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class HelloImplementation extends UnicastRemoteObject
implements Hello {
//locate registry; get remote object reference
Hello obj = (Hello)Naming.lookup
("//pc-114.cs.uwa.edu.au/HelloServer");
public String sayHello() throws RemoteException {
return ("This is a test of RMI.");
}
//Invoke the remote method on this object
message = obj.sayHello();
} catch (Exception e) {
System.out.println("RemoteHelloClient exception");
}
System.out.println(message);
// main() constructs and registers server object (later)
}
}
}
5
6
Deploying the Implementation
The Server Registers Itself in the Registry
• Compile Java source files,
• Generate stubs (client proxies) using the rmic compiler on the
class files that contain remote object implementations. (Skeletons
aren’t needed in recent versions of Java.)
The main() method for the server :
public static void main(String args[]){
• Start the RMI bootstrap registry - this provides support for constructing a ”catalogue” of object names vs references that are available on the server.
//Create and install a security manager
System.setSecurityManager(new RMISecurityManager());
• Start the server. The server registers itself in the registry.
try{
//Construct a server object and give it a name
HelloImplementation obj = new HelloImplementation();
• Start the client. The client locates the registry on the server and
uses it to get an object reference which can then be used to call
the remote method(s).
//Register the server object in the registry
Naming.bind("HelloServer", obj);
System.out.println("HelloServer bound in registry");
} catch(Exception e) {
System.out.println("HelloImplementation error");
}
}
7
8
Start the Client
RMI Summary
• RMI allows a Java program executing in one JVM environment to
invoke methods in a different JVM.
The client :
• Locates the registry on the server,
• uses it to get an object reference,
• Objects that have methods that can be remotely invoked must be
registered in a server Remote Object registry.
• uses this object reference to call the remote method
• The client consults this registry to obtain remote object references.
• The basic steps are :
public static void main(String args[]){
System.setSecurityManager(new RMISecurityManager());
– define a remote interface
– write an implementation class
RemoteHelloClient rhc = new RemoteHelloClient();
rhc.InvokeRemoteMethod();
– write a client that uses the remote interface
– compile the files
}
– start the registry, server and client
9
10