Professional Documents
Culture Documents
02c - Σδυ50 - Οσσ3 - Rmi
02c - Σδυ50 - Οσσ3 - Rmi
RMI
,
,
(remote
method invocation RMI)
3
RMI
RMI
RMI
(), ()
,
-, :
RMI
(Retry Request Message):
, ,
(Duplicate Filtering):
,
,
(Retransmission of
Results):
history ,
,
7
Invocation Semantics
Maybe invocation semantics:
, , .
.
, .
At-Least-Once Invocation Semantics: ,
, .
,
.
(.. ).
At-Most-Once Invocation Semantics: ,
, . ,
.
8
RMI compiler
Stub Proxy
Skeleton
Stubs Skeletons
Client
Server
server
Network
Stub
(marshalling)
Skeleton
(unmarshalling)
server
Java RMI
interfaces java.rmi
RMI compiler
rmic command line compiler
JDK
rmic [full class name including packages]
:
rmic RMITest
:
RMITest_Stub
RMITest_Skel
-keep
stub skeleton
(by value vs. by reference)
To RMI :
(remote objects)
by reference
(serializable objects)
JVMs
by value
by value
Remote
Serializable
Java RMI
5.16
To Shape remote interface
remote
( 2)
GraphicalObject serializable
( 1)
13
Java RMIregistry
(naming service)
RMIregistry
remote
java.rmi.Naming
registry URL-style :
//computerName:portNumber/human-readable-objectName
computerName:
RMIregistry
portNumber: RMIregistry
human-readable-objectName:
15
O client
ShapeList interface ( 1)
allShapes (
2)
16
RMISecurityManager
main client server
security manager
RMI
client/server
O default security manager
RMISecurityManager
,
security manager
RMISecurityManager
17
Policy
..
, , sockets
..
, ,
, , ( )
policy SecurityManager
default java.home/lib/security/java.policy
java.net.SocketPermission policy
18
Policy
grant {
permission java.security.AllPermission;
};
grant {
// Allow to connect to any port above 1024
permission java.net.SocketPermission "localhost:1024-", "connect, resolve";
// Allow to accept to any port above 1024
permission java.net.SocketPermission "localhost:1024-", "accept, resolve";
};
Java RMI 1
,
java.rmi.Remote
java.rmi.RemoteException
(servant class)
java.rmi.UnicastRemoteObject
20
Java RMI 2
(server skeleton)
() (client stub)
javac
RMI, rmic
21
Java RMI 3
,
22
RMI
Marshalling/Unmarshalling
( , )
Java RMI :
Shared Whiteboard
Shared Whiteboard =
:
, , , ..
24
server
version number (int)
whiteboard
version number server, version
number
25
Java RMI
remote
( 2)
GraphicalObject serializable
( 1)
26
GraphicalObject class
: , ,
line color, fill color
/
27
ShapeList
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.util.Vector;
public class ShapeListServant extends UnicastRemoteObject implements ShapeList {
private Vector theList;
// contains the list of Shapes
1
private int version;
public ShapeListServant()throws RemoteException{...}
public Shape newShape(GraphicalObject g) throws RemoteException {
2
version++;
Shape s = new ShapeServant( g, version);
theList.addElement(s);
3
return s;
}
public Vector allShapes()throws RemoteException{...}
public int getVersion() throws RemoteException { ... }
}
28
29
O client
ShapeList interface ( 1)
allShapes (
2)
30
allShapes(),
getAllState() Shape interface
,
, newShape() server,
GraphicalObject
O
version number server (
)
server ,
31
Java RMI :
add server
32
Java RMI
import java.rmi.*;
public interface AddServerIntf extends Remote {
double add(double d1, double d2) throws RemoteException;
}
33
remote object
import java.rmi.*;
import java.rmi.server.*;
public class AddServerImpl extends UnicastRemoteObject implements
AddServerIntf {
public AddServerImpl() throws RemoteException
{
}
public double add(double d1, double d2) throws RemoteException
{
return d1 + d2;
}
}
34
RMI server
import java.net.*;
import java.rmi.*;
public class AddServer {
public static void main(String args[]) {
try {
AddServerImpl addServerImpl = new AddServerImpl();
Naming.rebind("AddServer", addServerImpl);
}
catch(Exception e) { System.out.println("Exception: " + e); }
}
}
35
RMI client
import java.rmi.*;
public class AddClient {
public static void main(String args[]) {
try {
String addServerURL = "rmi://" + args[0] + "/AddServer";
AddServerIntf addServerIntf =
(AddServerIntf)Naming.lookup(addServerURL);
System.out.println("The first number is: " + args[1]);
double d1 = Double.valueOf(args[1]).doubleValue();
System.out.println("The second number is: " + args[2]);
double d2 = Double.valueOf(args[2]).doubleValue();
System.out.println("The sum is: " + addServerIntf.add(d1, d2));
}
catch(Exception e) {
System.out.println("Exception: " + e);
}
}
}
36
Compile all aforementioned Java source files and keep
compiler:
rmic AddServerImpl
This command generates two new files:
AddServerImpl_Skel.class (skeleton) and
AddServerImpl_Stub.class (stub).
37
2. Start the RMI registry
Go to the directory on the server machine where you
keep files mentioned above.
If your server is on another Windows machine: start
rmiregistry
If your server is on a UNIX machine: rmiregistry &
3. Start the server
In the same directory:
java AddServer