Beruflich Dokumente
Kultur Dokumente
My Machine
My Object
Todays World...
Network and Distributed Objects
Overview
What options do I have for distributed application development? Developers who program using the Java programming language can choose several solutions for creating distributed applications programs. 1) Java RMI technology 2) Java IDL technology (for CORBA programmers) 3) Enterprise JavaBeans technology In this section we shall be talking about Java RMI and IDL (Corba) technologies
4
JAVA RMI
Remote Method Invocation
Outline
1) 2) 3) 4) 5) RMI Concepts RMI architecture Implementing and running RMI system Implementing activatable RMI server Summary
Introduction
Remote Method Invocation (RMI) technology was first introduced in JDK1.1. RMI allows programmers to develop distributed Java programs with the same syntax and semantics used for non-distributed programs. RMI is based on a similar, earlier technology for procedural programming called remote procedure call (RPC)
7
Introduction
Disadvantages of RPC a) RPC supports a limited set of data types. Therefore it is not suitable for passing and returning Java Objects b) RPC requires the programmer to learn a special interface definition language (IDL) to describe the functions that can be invoked remotely
8
Introduction
The RMI architecture defines a) How objects behave. b) How and when exceptions can occur. c) How memory is managed. d) How parameters are passed to, and returned from, remote methods. The remote object model for Enterprise JavaBeans (EJB) is RMI- based.
9
Introduction
RMI is designed for Java-to-Java distributed applications. RMI is simpler and easier to maintain than using socket. Other options for creating Java-to-non-Java distributed applications are: a) Java Interface Definition Language (IDL) b) Remote Method Invocation (RMI) over Internet Inter-ORB Protocol (IIOP) -- RMIIIOP.
10
RMI architecture
RMI allows the code that defines the behavior and the code that implements the behavior to remain separate and to run on separate JVMs. At client side, RMI uses interfaces to define behavior. At server side, RMI uses classes to define implementation.
11
RMI Layer
Java Virtual Machine Client Object Java Virtual Machine Remote Object
Stub
Skeleton
Transport Layer
TCP
Transport Layer
12
Remote Objects
Remote Objects
Live on server Accessed as if they were local
13
Remote Objects
14
Skeleton
lives on server unmarshals argument data marshals results data receives requests from stub delivers response to stub
16
17
Transport Layer
The Transport Layer makes the connection between JVMs. All connections are streambased network connections that use TCP/IP. Dispatching messages between stub and skeleton.
18
RMI Flow
1. Server Creates Remote Object Client Virtual Machine 2. Server Registers Remote Object Client Server Virtual Machine Remote Object
1
Stub
Skeleton Server
2
RMI Flow
Client Virtual Machine Client Server Virtual Machine Remote 3. Client requests object from Registry Object 4. Registry returns remote reference (and stub gets created) Stub
3 4
Skeleton Server
RMI Flow
Client Virtual Machine Client
5 6 7
Stub
Skeleton Server
5. Client invokes stub method 6. Stub talks to skeleton 7. Skeleton invokes remote object Fred method Registry Virtual Machine
21
The steps...
Create the Interface to the server Create the Server Create the Client Compile the Interface (javac) Compile the Server (javac) Compile the Client (javac) Generate Stubs and Skeletons (rmic)
22
23
24
25
26
27
28
29
javac
javac
AdderImpl.class (classfile)
rmic
Windows:
start rmiregistry
(uses port 1099 by default)
You can also bind rmiregistry to a different port by indicating the new port number as :
32
import java.rmi.AlreadyBoundException; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.Naming; import java.net.MalformedURLException;
public class RMIServer { public static void main(String []args) throws RemoteException, MalformedURLException, AlreadyBoundException { try{ // LocateRegistry.createRegistry(1099); Registering Remote Classes AdderImpl adder = new AdderImpl(); System.out.println("Server has started"); Instantiate a new object and register Naming.bind (rmi://localhost/adder", adder); (bind it) in the rmiregistry System.out.println("Adder bound"); }catch (Exception e) { Start rmicregistry.exe System.out.println ("Trouble: " + e); } } 33 }
RMI URLs
rmi://host[:port]/name
default port is 1099 Specifies hostname of registry can also use relative URLs
name only assumes registry is on local host
34
35
36
RMI URLs
rmi://host[:port]/name
default port is 1099 Specifies hostname of registry can also use relative URLs
name only assumes registry is on local host
Remember that the stub and skeleton classes get generated by the rmic compiler
37
38
39
Passing Parameters
All parameters passed from an RMI client to an RMI server must either be serializable or be a remote object. For serializable: a) Data is extracted from the local object and sent across the network to the remote server. b) Object is then reconstructed in the remote server. c) Any changes to the object in the RMIServer will not be reflected in the object held in the RMI client and vice versa. 40
Passing Parameters
For a remote object: a) Stub information, not a copy of data, is actually sent over RMI. b) Any call made to the parameter object become a remote calls back to the actual object. c) Changes made in one JVM are reflected in the original JVM.
41
42
43
Security
Your program should guarantee that the classes that get loaded do not perform operations that they are not allowed to perform. A more conservative security manager than the default should be installed. The following code should be added to the main method of the server and client program:
44
import java.rmi.RMISecurityManager;
public class RMIServer { public static void main(String []args) throws RemoteException, MalformedURLException, AlreadyBoundException {
RMISecurityManager
Easiest way to do this is to use java.rmi.RMISecurityManager as:
System.setSecurityManager(new RMISecurityManager());
This by default restricts all code from making socket connections. Obviously this is too strict. Need a policy file to allow client to make network connection to rmi port. It would look something like:
grant{ permission java.net.SocketPermission *:1024-65535, connect}
Activatable Server
Before Java 2, the UnicastRemoteObject could be accessed only from a server program that created instances of the object and ran all the time. With Java 2 we got the Activatable and the rmid daemon. An activatable class needs only to be registered with the rmid. This is an advantage for systems that have many remote object classes but only a few of them are active at any one time. They save memory (and so gain performance).
47
Activatable Server
Enable server programs to wake up and start to run when they are needed. Java RMI Activation System Deamon (RMID) is introduced to handle this task. When a client requests a reference to the server from the rmiregistry, the rmid program, which holds the servers details, will be requested to start up the server and return the reference to the client. After that, the rmiregistry will be able to provide the reference of the server directly.
48
MyRemoteInterface.java
import java.rmi.*; public interface MyRemoteInterface extends Remote { public Object callMeRemotely() throws RemoteException; }
49
50
52
ActivatableImplementation.java
53
Example
http://www.javacamp.org/morecl asses/rmi/rmi23.html
63
java.rmi.activation.Activatable 1.2
protected Activatable(ActivationID id, int port) Constructs the activatable object and establishes a listener on the given port. Use 0 for the port to have a port assigned automatically. static Remote exportObject(Remote obj, ActivationID id, int port) Makes a remote object activatable. Returns the activation receiver that should be made available to remote callers. Use 0 for the port to have a port assigned automatically. static Remote register(ActivationDescriptor desc) registers the descriptor for an activatable object and prepares it for receiving remote calls. Returns the activation receiver that should be made available to remote callers.
65
java.rmi.MarshalledObject 1.2
MarshalledObject(Object obj) constructs an object containing the serialized data of a given object. Object get() deserializes the stored object data and returns the object.
66
java.rmi.activation.ActivationGroupDesc 1.2
ActivationGroupDesc(Properties props, ActivationGroupDesc.CommandEnvironment env)
constructs an activation group descriptor that specifies virtual machine properties for a virtual machine that hosts activated objects. The env parameter contains the path to the virtual machine executable and command-line options, or it is null if no special settings are required.
67
java.rmi.activation.ActivationGroup 1.2
static ActivationSystem getSystem() returns a reference to the activation system.
68
java.rmi.activation.ActivationSystem 1.2
ActivationGroupID registerGroup ( ActivationGroupDesc group)
69
java.rmi.activation.ActivationDesc 1.2
ActivationDesc(ActivationGroupID id, String className, String classFileURL, MarshalledObject data)
70
Callback
Server
...
Client A client issues a request to the server repeatedly until the desired response is obtained. Client A client registers itself with the server, and wait until the server calls back.
73
RMI Callbacks
A callback client registers itself with an RMI server. The server makes a callback to each registered client upon the occurrence of a certain event. Server
Clients C1 The callback list C2 RMI calls callback C4
C3
C5
74
SomeInterface_stub.class 3,4
X
SomeInterface_skel.class SomeServer.class
CallbackInterface_skel.class 5
CallbackInterface_stub.class 1. Client looks up the interface object in the RMIregistry on the server host. 2. The RMIRegistry returns a remote reference to the interface object. 3. Via the server stub, the client process invokes a remote method to register itself for callback, passing a remote reference to itself to the server. The server saves the reference in its callback list. 4. Via the server stub, the client process interacts with the skeleton of the interface object to access the methods in the interface object. 5. When the anticipated event takes place, the server makes a callback to each registered client via the callback interface stub on the server side and the callback interface skeleton on the client side.
75
ClientImpl.class
ServerImpl_Stub.class ClientImpl_skel.class
ServerImpl.class
ClientImpl_Stub.class ServerImpl_skel.class
76
Implementation
Object-Oriented Callbacks
RMI Callbacks
Listener interface and the event source must be implemented as an RMI service. For the listener
to register itself with the remote event source, it must invoke a remote method and pass an object reference to the remote listener interface it defines.
server must
extend UnicastRemoteObject, to offer a implement Temperature Sensor interface
implement the TemperatureListener interface, register itself with the remote temperature sensor service, client will be notified of changes as they occur, using a remote callback
RMI Components
java.rmi: client-side RMI classes, interfaces, and exceptions java.rmi.server: server-side RMI classes, interfaces, and exceptions java.rmi.registry: Classes for naming services java.rmi.dgc: distribute garbage collection
100
102
104