java rmi essentials based on mastering rmi rickard oberg
TRANSCRIPT
![Page 1: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/1.jpg)
Java RMI Essentials
Based on Mastering RMI
Rickard Oberg
![Page 2: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/2.jpg)
2
Essentials of Remote Invocation
• What is RMI?– The Principles of RMI– How Does RMI Differ from Ordinary Java
• RMI/JRMP Architecture– Stubs– Marshalling– RMI Threading & Network Connection
Management– Distributed Garbage Collection– Naming
• Summary
![Page 3: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/3.jpg)
3
What is RMI?
• RMI is a specification (API) for accessing
objects from a remote JVM.
• What is specified?
– How objects are to be coded.
– How objects can be located & invoked.
– How parameters & returned values are passed.
• Java Remote Method Protocol (JRMP) is
Sun’s implementation of the RMI API.
![Page 4: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/4.jpg)
4
The Principles of RMI
• Meta-principle
Make RMI like MI as much as possible.
• Objects are invoked by calling methods.
• Expose interfaces not implementation.
• Exceptions report errors in the computation.
• GC determines the lifecycle of objects.
• Get classes that are not part of system
classpath via classloading.
![Page 5: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/5.jpg)
5
How Does RMI Differ from Local MI?
• Remote exceptions– Remote methods throw java.rmi.RemoteExeption.
• Pass by value– All arguments are pass-by-value.– Serializing may hurt performance for large objects.
• LatencyInvocations take much longer to complete.
• SecurityArguments/returned value are sent over a network. Is
privacy an issue?
![Page 6: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/6.jpg)
6
RMI/JRMP Architecture
• Stubs
• Marshalling
• RMI Threading & Network Connection
Management
• Distributed Garbage Collection
• Naming
![Page 7: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/7.jpg)
7
Stubs
• Stub– The client has a proxy for the remote object: the stub.
– The stub implements the remote object’s interface[s].
– The RMI compiler (rmic) generates the stub.
– Remote methods invoked by the client are delegated to the JRMP engine.
– The JRMP forwards the call to the server.
– The server executes the method.
– The result is returned to the client.
![Page 8: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/8.jpg)
8
MyServer
MyServer Stub
RemoteRef[host=myhost, port=1234, ObjID=0]
JRMP<<call>>
CLIENT
MyServerImpl
MyServer ServerSocket[port=1234]
End-point
<<send>>
Object table
<<access>>
JRMP
exported objects
SERVER
<<call>
MyServer instance maps to ObjID=0
![Page 9: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/9.jpg)
9
The Remote Interface
• It is a set of remotely invoked methods.• It has the following characteristics:_______________________________________public interface MyRemoteInterface
extends java.rmi.Remote // possibly indirectly{
public <return_type> myMethod( <type> p1, … )throws java.rmi.RemoteException
// declare other remote methods …}
_______________________________________• All parameters & return type are serializable.
![Page 10: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/10.jpg)
10
The Hello Interface
package masteringrmi.helloworld.interfaces;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HelloWorld extends Remote
{
public String helloWorld( String name )
throws RemoteException;
}
![Page 11: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/11.jpg)
11
The Hello Interface …
• Style: create a contract package
It contains the “contract” between client & server:
• Remote interfaces of server objects
• Application exceptions throwable by any remote interface
• Data container classes for data moved between client & server.
– Client & server get contract classes & interfaces.
– Server also gets implementation classes.
![Page 12: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/12.jpg)
12
Implementing the Remote Interface
public class HelloWorldImpl extends UnicastRemoteObject
implements HelloWorld
{
public HelloWorldImpl() throws RemoteException {}
public String helloWorld( String name )
{
return “Hello “ + name + “!”;
}
}
![Page 13: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/13.jpg)
13
Implementing the Remote Interface …
public class HelloWorldImpl implements HelloWorld
{
public HelloWorldImpl() throws RemoteException
{
UnicastRemoteObject.exportObject( this );
}
public String helloWorld( String name )
{
return “Hello “ + name + “!”;
}
}
![Page 14: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/14.jpg)
14
Implementing the Remote Interface …
• UnicastRemoteObject constructor exports the objectMakes it available for incoming invocations.
• The exportObject method does this explicitly.
• Extending UnicastRemoteObject inherits distributed implementations of: – equals, hashCode, & toString.
![Page 15: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/15.jpg)
15
RMI/JRMP Architecture
• Stubs
• Marshalling
• RMI Threading & Network Connection
Management
• Distributed Garbage Collection
• Naming
![Page 16: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/16.jpg)
16
Marshalling
• Marshalling creates a byte[] from an object.
• Unmarshalling does the reverse.
• To marshal, Java serializes the object.
• To unmarshal, Java deserializes the byte[].
public class foo implements Serializable
Foo Copy of FooBytesSerialization Deserialization
![Page 17: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/17.jpg)
17
Dynamic Classloading
• We defer discussion of this until later.
![Page 18: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/18.jpg)
18
Security
• To dynamically download stubs, the client: – Sets a security manager:
______________________________________________________import java.rmi.RMISecurityManager;. . .if ( System.getSecurityManager() == null )
System.setSecurityManager( new RMISecurityManager() ); ________________________________________________
– Has a policy file that permits downloading________________________________________________grant {
permission java.security.AllPermission;}________________________________________________
![Page 19: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/19.jpg)
19
Class Versioning
• Server changes are propagated to clients that
subsequently download the stub.
• What about running clients that already have
the stub?
• Basically, this is a problem.
– serial version UID can be used to detect
incompatibilities.
– Jini further ameliorates the version problem.
![Page 20: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/20.jpg)
20
RMI/JRMP Architecture
• Stubs
• Marshalling
• RMI Threading & Network Connection
Management
• Distributed Garbage Collection
• Naming
![Page 21: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/21.jpg)
21
RMI Threading & Network Connection Management
“Since RMI on the same remote object
may execute concurrently, a remote
object implementation needs to make
sure its implementation is thread-safe.”
The RMI specification, section 3.2
![Page 22: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/22.jpg)
22
Network Connections
• RMI specifies socket factory interfaces to get
sockets on the client & server:
– Java.rmi.server.RMIClientSocketFactory
– Java.rmi.server.RMIServerSocketFactory
• Override the default implementation
(to provide encryption, authentication, …)
Selecting an features at run time is desirable: Some
jobs want encryption, some do not.
• Custom implementations are discussed later.
![Page 23: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/23.jpg)
23
Threading Model
• The JRMP implementation for the server– Instantiates a thread for each connection.
– It listens for its associated client’s calls.
– It handles each call to completion.
• The JRMP implementation for the client– Concurrent calls from a client cause concurrent
threads connecting to the server.
– This may swamp a server.
– If your client makes concurrent calls, you may want a different implementation.
![Page 24: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/24.jpg)
24
RMI/JRMP Architecture
• Stubs
• Marshalling
• RMI Threading & Network Connection
Management
• Distributed Garbage Collection
• Naming
![Page 25: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/25.jpg)
25
Distributed Garbage Collection
• RMI has distributed garbage collection (DGC).• Server tracks clients who have its stub.• Server keeps a count of such clients.• Count is decremented when client:
– explicitly relinquishes reference OR– doesn’t renew its lease (default 10 min.), e.g., client crashed:
• “Leasing” is due to Miller & Drexler, in – Incentive Engineering for Computational Resource Management. K. E.
Drexler & M. S. Miller, B. A. Huberman (ed.), (Studies in Computer Science & Artificial Intelligence), 1988.
• If local & remote references == 0, it is garbage.
![Page 26: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/26.jpg)
26
The Unreferenced Interface
• A server can implement Unreferenced
• It has 1 method: unreferenced.
• It is called when there are no remote references to the object.
• Being bound in the RMI registry counts as a remote reference.– It must unbind itself before unreferenced
can be invoked.
![Page 27: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/27.jpg)
27
RMI/JRMP Architecture
• Stubs
• Marshalling
• RMI Threading & Network Connection
Management
• Distributed Garbage Collection
• Naming
![Page 28: Java RMI Essentials Based on Mastering RMI Rickard Oberg](https://reader036.vdocuments.site/reader036/viewer/2022081501/56649e405503460f94b31bed/html5/thumbnails/28.jpg)
28
Naming
• Rmiregistry allows:
– A server to store its (serialized) stub on a
web server
– A client to download & deserialize the stub.
• The essential information: the url of the
serialized stub file.