This presentation introduces basic concepts about the Java Remote Method Invocation, a.k.a. RMI. These slides introduce the following concepts:
- Remote interfaces
- Dynamic code loading
- Serialization
- Security manager
- Exporting remote objects
- Compiling and running RMI programs
- Distributed garbage collection
The presentation is took from the Java course I run in the bachelor-level informatics curriculum at the University of Padova.
Direct Style Effect Systems -The Print[A] Example- A Comprehension Aid
Java - Remote method invocation
1. REMOTE METHOD INVOCATION
PROGRAMMAZIONE CONCORRENTE E DISTR.
Università degli Studi di Padova
Dipartimento di Matematica
Corso di Laurea in Informatica, A.A. 2015 – 2016
rcardin@math.unipd.it
3. Programmazione concorrente e distribuita
INTRODUCTION
RMI allows applications to call object methods
located remotely
A server program creates some remote objects
A client program obtains a remote reference and
then invokes methods on them
Distributed object application
Sharing resources, processing load across systems
RMI provides mechanism by which the server and the
client communicate
Using low level communication (i.e. sockets) is harder and
less performing
Serialization has a cost and its difficult to share some state
3Riccardo Cardin
4. Programmazione concorrente e distribuita
INTRODUCTION
Distributed object applications need to do the
following
Locate remote objects
Application can register its remote objects with RMI's simple
naming facility, the RMI registry
Communicate with remote objects
Details of communication between remote objects are
handled by RMI
Load class definitions for objects that are passed
around
RMI provides mechanisms for loading an object's class
definitions as well as for transmitting an object's data
4Riccardo Cardin
6. Programmazione concorrente e distribuita
CREATING DISTRIBUTED APPLICATIONS
Developing a distributed application using RMI
involves these general steps:
Designing and implementing the components
Defining the remote interfaces
Implementing the remote objects
Implementing the clients
Compiling sources
Making classes network accessible
Typically a web server is used for this purpose
Starting the application
Registry, server, client, ...
6Riccardo Cardin
8. Programmazione concorrente e distribuita
REMOTE INTERFACES
Distributed application are made up of remote
interfaces and classes
Objects with methods that can be invoked across Java
virtual machines are called remote objects.
A remote interfaces has the following characteristics
Extends the interface java.rmi.Remote
Each method of the interface
declares java.rmi.RemoteException in its throws clause
Remote objects are treated like an object reference
A local representation (remote stub) is sent to other JVMs
Implementation of the Remote proxy pattern
Any other object is copied before being sent
8Riccardo Cardin
9. Programmazione concorrente e distribuita
REMOTE INTERFACES
Each interface defines a specific protocol
It’s important to clearly separate methods’ interfaces
from their implementation
Methods of an interface that extends Remote can be
invoked from another JVM
Remote methods MUST throw a RemoteException
A checked exception used to indicate either a communication
failure or a protocol error
Remote interfaces are defined server side
9Riccardo Cardin
// The interface implements java.rmi.Remote and its unique method
// throws a java.rmi.RemoteException
public interface Compute extends Remote {
<T> T executeTask(Task<T> t) throws RemoteException;
}
10. Programmazione concorrente e distribuita
REMOTE INTERFACES
A remote interface must be implemented
The implementing class can declare more methods,
but these will not be seen by the clients
Parameters of remote methods follow these rules:
Remote objects are passed by reference. Changes made
remotely are reflected in the original remote object
A stub is a proxy to the remote object that communicates
with the original remote object
Local objects are passed by copy, using serialization
Changes are not propagated back in any way
10Riccardo Cardin
public class ComputeEngine implements Compute {
// Implement at least remote methods
}
12. Programmazione concorrente e distribuita
DYNAMIC CODE LOADING
Interfaces that are not marked as remote must
be serializable
Type that are not marked as remote are passed by
copy, using serialization
Use java.io.Serializable has a marked interface
The implementation will be dinamically downloaded
by RMI into the host JVM
This is possibile beacuse all objects are written in Java
12Riccardo Cardin
// This is not a remote intefaces, but it is used in the protocol
// defined by a remote method. It’s implementation must implement
// also the Serializable interface
public interface Task<T> {
T execute();
}
13. Programmazione concorrente e distribuita
SERIALIZATION
Representation of an object as a sequence of
bytes, that includes data as well as type
The process is JVM independent
Serialization process uses the stream API
ObjectInputStream and ObjectOutputStream types
Deserialization is the opposite process
The type must implement java.io.Serializable
All fields in the class must be serializable. If a field is not, it
must be marked as transient
Transient fields are not serialized and are lost intentionally
A transient variable cannot be final or static
Serialization is used by RMI, EJB and so on
13Riccardo Cardin
14. Programmazione concorrente e distribuita
SECURITY MANAGER
Defines the outer boundaries of the sandbox for
downloaded (untrusted) code
It descends from class java.lang.SecurityManager
Security policies specify actions that are unsafe
Accept or open a socket from/to a host and port
Modify a thread (change its priority, ...)
Interact with file system
Interact with system properties
Exit an application
For every unsafe action there is a corresponding
checkXXX method
Actions not allowed throw a SecurityException
14Riccardo Cardin
15. Programmazione concorrente e distribuita
SECURITY MANAGER
RMI programs must install a security manager
Otherwise RMI will not download classes
Only one security manager can be installed
By default, an application has no security manager installed
Policies are specified using *.policy files
Server and client application must specify their policy file
Default file: java.home/lib/security/java.policy
Use -Djava.security.policy property specify a file
15Riccardo Cardin
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
grant codeBase "file:/home/ann/src/" {
permission java.security.AllPermission;
};
17. Programmazione concorrente e distribuita
EXPORTING REMOTE OBJECTS
Remote objects need to be exported to the RMI
runtime
Exporting means create stub objects that expose the
remote methods to clients
Stub is the client placeholder for the remote object
The stub implements the remote interface
It is a remote proxy to the remote object
2° arg to exportObject is the listening port for incoming
connection
17Riccardo Cardin
Compute engine = new ComputeEngine();
// Listening to port 0 means to listen on every port
Compute stub =
(Compute) UnicastRemoteObject.exportObject(engine, 0);
19. Programmazione concorrente e distribuita
EXPORTING REMOTE OBJECTS
RMI Registry: a remote map for other remote
objects, indexed by names
The server register a remote object (stub), the client
retrieve it using the same name
The java.rmi.Naming type provides methods for
storing and obtaining references to remote objects
Available methods are bind, unbind and rebind
The name argument specifies the host and port of the registry
The object represents the remote object (stub) to expose to
clients
19Riccardo Cardin
// Name is a string that represents an URL without the scheme
// //host:port/name
Naming.bind(name, object)
20. Programmazione concorrente e distribuita
EXPORTING REMOTE OBJECTS
RMI Registry
The creation and lookup of a new Registry can be
done using the type LocateRegistry
A registry is a remote object binded to a fixed name
This type offers also an API to interact with the registry
For security reasons, an application can bind an object with a
registry running on the same host
Using the lookup method, a client can retrieve a stub
20Riccardo Cardin
// Retrieves the default registry listening on port 1099
Registry registry = LocateRegistry.getRegistry();
// Asks the registry to bind a stub to a name
registry.rebind(name, stub);
// The name was the same used to bind the object
Compute comp = (Compute) registry.lookup(name);
21. Programmazione concorrente e distribuita
EXPORTING REMOTE OBJECTS
21Riccardo Cardin
A client looks up from the
RMI registry a stub binded
to a name
A server bind a stub to a
specific name in a RMI
registry
The client calls remote
methods, passing
serializable type as
arguments
1
2
3
22. Programmazione concorrente e distribuita
COMPILING
Compilation of an RMI program requires a series
of different steps
Create an archive (JAR) with remote interfaces
This archive will be shared across server and clients
The above jar needs to be shared with the registry
Definitions that can be automatically downloaded
have to be placed in a public folder
For example, use an HTTP web server for this purpose
Build server classes
22Riccardo Cardin
javac compute/Compute.java compute/Task.java
jar cvf compute.jar compute/*.class
javac -cp c:homeannpublic_htmlclassescompute.jar
engineComputeEngine.java
23. Programmazione concorrente e distribuita
COMPILING
Compilation of an RMI program requires a series
of different steps
Build client classes
The Task interface needs to be public accessible
The client needs also the jar of remote interfaces
Defining security policies for server and clients
Create a *.policy file for each actor, specifying which
actions are allowed to be performed by the downloaded code
23Riccardo Cardin
javac -cp /home/jones/public_html/classes/compute.jar
client/ComputePi.java client/Pi.java
cp client/Pi.class /home/jones/public_html/classes/client
grant codeBase "file:/home/ann/src/" {
permission java.security.AllPermission;
};
24. Programmazione concorrente e distribuita
RUNNING
Starting the server components
Start the RMI registry, given with the JDK platform
Start the server program
Use java.rmi.server.codebase to specify the location of
remote interfaces archive
Use java.rmi.server.hostname to specify the host for
stubs exported from the server JVM
By default RMI uses the the IP address of the host
Use java.security.policy to specify the policy file
24Riccardo Cardin
start rmiregistry 1099 :: By default the registry run on port 1099
java -cp c:homeannsrc;c:classescompute.jar
-Djava.rmi.server.codebase= file:/c:/classes/compute.jar
-Djava.rmi.server.hostname=mycomputer.example.com
-Djava.security.policy=server.policy engine.ComputeEngine
25. Programmazione concorrente e distribuita
RUNNING
Starting the client component
Start the client program
Use java.rmi.server.codebase to specify the location of
client’s interfaces implementation
Use java.security.policy to specify the policy file
The host name of the server can be supplied as an argument
of the program
25Riccardo Cardin
java -cp c:homejonessrc;c:classescompute.jar
-Djava.rmi.server.codebase=file:/c:/classes/
-Djava.security.policy=client.policy
client.ComputePi mycomputer.example.com 45
26. Programmazione concorrente e distribuita
RUNNING
26Riccardo Cardin
The registry search the stub
implementation into the web
server
The implementation of client
interfaces are loaded by the
server from the public codebase
28. Programmazione concorrente e distribuita
DISTRIBUTED GARBAGE COLLECTION
RMI uses a reference-counting algorithm
Keeps track of all live references within each JVM
The first reference to a remote object sends a “referenced”
message to the server for that object. Reference count is
incremented
As live references are found to be unreferenced in local JVM,
the count is decremented. When the last reference has been
discarded, an unreferenced message is sent
When a remote object is not referenced by any client, the
RMI runtime refers to it using a weak reference
Due to connection delays, it is possible that
premature collection of remote object will occur
A reference to this object will thrown a RemoteException
28Riccardo Cardin
30. Programmazione concorrente e distribuita
REFERENCES
The Java Tutorials: RMI
https://docs.oracle.com/javase/tutorial/rmi/
The Java Tutorials: Security Manager
https://docs.oracle.com/javase/tutorial/essential/environment/sec
urity.html
Java security: How to install the security manager and customize
your security policy
http://www.javaworld.com/article/2077067/core-java/java-
security---how-to-install-the-security-manager-and-customize-your-
security-policy.html
Default Policy Implementation and Policy File Syntax
http://docs.oracle.com/javase/7/docs/technotes/guides/security/P
olicyFiles.html
30Riccardo Cardin
31. Programmazione concorrente e distribuita
REFERENCES
Java RMI : What is the role of the stub-skeleton that are generated
by the rmic compiler
http://stackoverflow.com/questions/16886889/java-rmi-what-is-
the-role-of-the-stub-skeleton-that-are-generated-by-the-rmic
Java – Serialization
http://www.tutorialspoint.com/java/java_serialization.htm
Garbage Collection of Remote Objects
https://docs.oracle.com/javase/8/docs/platform/rmi/spec/rmi-
arch4.html
31Riccardo Cardin