You are on page 1of 38

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

Remote Method Invocation (RMI)


RMI

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:

RMIregistry (Naming class)

15

Java RMI client

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";
};

-Djava.security.manager -Djava.security.policy = myRMIapp.policy


19

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 vs. Sockets



RMI

Marshalling/Unmarshalling

( , )

Java RMI :
Shared Whiteboard
Shared Whiteboard =

:
, , , ..



24

server




version number (int)

whiteboard

version number server, version
number
25

Java RMI

To Shape remote interface

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

Java RMI Server


import java.rmi.*;
public class ShapeListServer{
public static void main(String args[]){
System.setSecurityManager(new RMISecurityManager());
try{
ShapeList aShapeList = new ShapeListServant();
1
Naming.rebind("rmi://127.0.0.1/ShapeList", aShapeList );
2
System.out.println("ShapeList server ready");
}catch(Exception e) {
System.out.println("ShapeList server main " + e.getMessage());}
}
}

29

Java RMI client ()


rmi://127.0.0.1/
ShapeList

O client

ShapeList interface ( 1)

allShapes (
2)
30

Java RMI client ()


client


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

all generated .class files in the same directory.


Before you can use the client and the server, you must
generate the necessary stub.
You may also need to generate a skeleton, if a server
runs on a machine that does not have Java 2, but has
an earlier version of JVM.
1. Generate stubs and skeletons (deprecated!)
To generate stubs and skeletons, you use the RMI

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

4. Run the client


java AddClient xxx.xxx.xxx.xxx 567 999
38

You might also like