JavaRMI
-
Upload
sathishkumar-a -
Category
Documents
-
view
821 -
download
1
Transcript of JavaRMI
![Page 1: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/1.jpg)
George BlankUniversity Lecturer
![Page 2: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/2.jpg)
Java RMI
Introduction to RMI
![Page 3: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/3.jpg)
Java and the Web• Java’s popularity is due to its suitability for
use on the World Wide Web (WWW).• Several Web browsers have support for
Java:– Netscape Navigator– Internet Explorer– HotJava
• Java incorporates audio, video, and animation directly on the web page.
![Page 4: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/4.jpg)
Method Modifiers
• Instance Methods• Class Methods• Abstract Methods• Native Methods• Final Methods
![Page 5: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/5.jpg)
Method Modifiers with Effect on Scoping
• public: Method can be accessed by any class
• private: methods accessed only by class methods
• friendly: methods accessed by other methods in the same package
• protected: methods accessed by subclass methods.
![Page 6: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/6.jpg)
Interfaces
• A Java interface is similar to a class, except there is no data associated with the interface.
• Example:public interface MyInterface {
methods-with no implementation detailsfinal variables
}
![Page 7: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/7.jpg)
Properties of Interfaces
• The variables in an interface must be final.
• The methods are only declarations.• A class that extends another class is
guaranteed to support the contracts entered into by its superclass.
• Interfaces are used to approximate multiple inheritances
![Page 8: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/8.jpg)
Java Native Interface (JNI)
• The Java Native Interface (JNI) is part of the core JDK and provides a framework for interfacing to native code. The native code is not easily portable across different hardware platforms. So using native code takes way one of the major advantages of java. JNI was developed to bridge the gap.
(JNI section by Rekha Telkar)
![Page 9: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/9.jpg)
When to use JNI
• When the standard Java class library does not support the platform-dependent features needed by the application.
• You need to use a library written in another language, and wish to make it accessible to Java code through the JNI.
• You want to implement a small portion of time-critical code in a lower-level language such as assembly.
![Page 10: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/10.jpg)
JNI Capabilities
• You can use native methods to: – Create, inspect, and update Java objects
(including arrays and strings). – Call Java methods.– Catch and throw exceptions.– Load classes and obtain class information.– Perform runtime type checking.
![Page 11: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/11.jpg)
Enabling Java code with JNI
• You can use the JNI with the Invocation API to enable an arbitrary native application to embed the Java VM. Programmers can make their existing applications Java-enabled without having to link with the VM source code.
![Page 12: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/12.jpg)
Remote Method Invocation• RMI provides the means to invoke methods
remotely.• RMI allows for applications to communicate
and execute across multiple systems on a network.
• RMI is supported by the java.rmi, java.rmi.server, and java.rmi.registry
• Enhanced security of Java 2 requires a security policy implementation.
![Page 13: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/13.jpg)
Parts in a RMI System• Interface definitions for remote services
• Implementations of the remote services • Stub and Skeleton files • A server to host the remote services • An RMI Naming service that allows clients to
find the remote services • A class file provider (an HTTP or FTP server)
![Page 14: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/14.jpg)
RMI process
Java Client Java Server
Client Stub Server Skeleton
Client Method Called Method
Network transport Network Transport
arguments results
Network
Not neededIn Java 2
![Page 15: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/15.jpg)
RMI Server, Client, and Registry• The server process registers the remote
object X with the registry using the Naming.bind() method.
• The client calls Naming.lookup(), which contacts the registry and obtains a stub object for X.
• The client then uses the stub as if it is a local object.
![Page 16: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/16.jpg)
Stub Class
• A stub for a remote object is the client-side proxy for the remote object. Such a stub implements all the interfaces that are supported by the remote object implementation. The client-side stub responsibilities are shown on the next slide.
![Page 17: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/17.jpg)
Stub Class Responsibilities
• Initiating a call to the remote object (by calling the remote reference layer).
• Marshaling arguments to a marshal stream (obtained from the remote reference layer).
• Informing the remote reference layer that the call should be invoked.
• Unmarshaling the return value or exception from a marshal stream.
• Informing the remote reference layer that the call is complete.
![Page 18: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/18.jpg)
Skeleton Class• A skeleton for a remote object is a server-side entity
that contains a method which dispatches calls to the actual remote object implementation. The skeleton is responsible for:– Unmarshaling arguments from the marshal stream.– Making the up-call to the actual remote object
implementation. – Marshaling the return value of the call or an exception (if
one occurred) onto the marshal stream.
![Page 19: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/19.jpg)
Remote Reference Layer
• The remote reference layer deals with the lower level transport interface and is responsible for carrying out a specific remote reference protocol which is independent of the client stubs and server skeletons. The remote reference layer has two cooperating components: the client-side and the server-side components.
![Page 20: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/20.jpg)
Remote Reference Layer (2)
• The client-side component contains information specific to the remote server (or servers, if the remote reference is to a replicated object) and communicates via the transport to the server-side component. During each method invocation, the client and server-side components perform the specific remote reference semantics.
![Page 21: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/21.jpg)
Remote Reference Layer (3)
• For example, if a remote object is part of a replicated object, the client-side component can forward the invocation to each replica rather than just a single remote object.
• In a corresponding manner, the server-side component implements the specific remote reference semantics prior to delivering a remote method invocation to the skeleton.
![Page 22: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/22.jpg)
Remote Reference Layer (4)
• For example, the server side could handle ensuring atomic multiple delivery by communicating with other servers in the replica group.The remote reference layer transmits data to the transport layer via the abstraction of a stream-oriented connection. The transport takes care of the implementation details of connections. Although connections present a streams-based interface, a connectionless transport may be implemented beneath the abstraction
![Page 23: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/23.jpg)
RMI Registry
• The Registry tracks the addresses of the remote objects exported by applications
• It is the central management point for RMI
• Does not actually invoke remote methods
• Bind() links the object in the registry• Rebind() replaces object with a new one
![Page 24: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/24.jpg)
Parameter Passing
• When a remote procedure is executed, the java.rmi runtime encodes the arguments and sends them over the network to a server that decodes them.
• The server then invokes the method, encodes the results, and sends it back.
• Finally, the client-side java.rmi runtime decodes the result.
![Page 25: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/25.jpg)
Parameter Marshalling
• RMI stubs are responsible for packaging parameters used in a remote method in a block of bytes using the big-endian byte order. This is called parameter marshalling. A receiver object on the server must unmarshall the parameters or report errors.
![Page 26: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/26.jpg)
Building RMI Applications
• Define remote interfaces• Create classes that implement the
interfaces• Create stub and skeleton classes for the
implementation classes.• Create Security Policy
![Page 27: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/27.jpg)
A Distributed Hello World Program Using RMI
• It uses an applet to make a remote method call to the server from which it was downloaded to retrieve the message "Hello World!".
• When the applet runs, “Hello World!” is displayed on the client browser.
![Page 28: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/28.jpg)
Steps Involved
• Write The HTML and Java Source Files.• Compile and Deploy Class Files and
HTML Files. • Start the Remote Object Registry,
Server, and Applet
![Page 29: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/29.jpg)
Source Files
• The Java remote interface. (Hello.java)• The Java remote object (server) which
implements the remote interface. (HelloImpl.java)
• The Java applet that remotely invokes the remote method, sayHello(). (HelloApplet.java)
• The HTML code for the web page that references the applet. (hello.html)
![Page 30: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/30.jpg)
The Remote Interface
• Must be public.• Extends the interface java.rmi.Remote.• Each method must declare
java.rmi.RemoteException in its throws clause
• A remote object passed as an argument or return value must be declared as the remote interface.
![Page 31: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/31.jpg)
package examples.hello;
import java.rmi.Remote; import java.rmi.RemoteException;
public interface Hello extends java.rmi.Remote {String sayHello() throws java.rmi.RemoteException;
}
Remote Interface
![Page 32: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/32.jpg)
The Implementation Class
• Specify the remote interface(s) being implemented.
• Define the constructor for the remote object.
• Provide implementations for the methods that can be invoked remotely.
• Create and install a security manager.
![Page 33: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/33.jpg)
The Implementation Class (Cont’d)
• Create one or more instances of a remote object.
• Register at least one of the remote objects with the RMI remote object registry, for bootstrapping purposes.
![Page 34: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/34.jpg)
Server Code (1)
package examples.hello; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.RMISecurityManager; import java.rmi.server.UnicastRemoteObject;
public class HelloImpl extends UnicastRemoteObject implements Hello { public HelloImpl() throws RemoteException {
super(); }
![Page 35: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/35.jpg)
// Implementation of remote method
public String sayHello() { return "Hello World!";
}
Server Code (2)
![Page 36: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/36.jpg)
public static void main(String args[]) {
// Create and install a security manager if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); }
Server Code (3) main begins
![Page 37: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/37.jpg)
try {
HelloImpl obj = new HelloImpl(); // Bind this object instance to the name "HelloServer" Naming.rebind("//afsxx.njit.edu/HelloServer", obj);System.out.println("HelloServer bound in registry"); } catch (Exception e) { System.out.println("HelloImpl err: " + e.getMessage()); e.printStackTrace(); } } }
Server Code (4) main ends
![Page 38: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/38.jpg)
Notes on Server Code
• By extending remote class UnicastRemoteObject, the HelloImpl class can be used to create a remote object that: – Uses RMI's default sockets-based transport for
communication – Runs all the time (In Java 2 SDK, the object can be
activated (created) when a client requests it, using Remote Object Activation, rather than running all the time – extend java.rmi.activation.Activatable)
• To bind to a different port, for example 2001, use: Naming.rebind("//afsxx.njit.edu:2001/HelloServer", obj);
![Page 39: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/39.jpg)
Notes on Server Code (contd)
• Arguments to, or return values from, remote methods can be any data type for the Java platform, including objects, as long as those objects implement the interface java.io.Serializable.
• By default, local objects are passed by copy, which means that all data members (or fields) of an object are copied, except those marked as static or transient
• Remote objects are passed by reference. A reference to a remote object is actually a reference to a stub, which is a client-side proxy for the remote object.
![Page 40: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/40.jpg)
Notes on Server Code (contd)
• Security manager guarantees that the classes that get loaded perform only allowed operations.
• If no security manager is specified, no class loading, by RMI clients or servers, is allowed, aside from what can be found in the local CLASSPATH.
• Client Applets use the security manager already installed in the client browser.
• If the client were an application rather than an applet, Security manager would need to be installed.
• A security manager is required in any JVM that needs to download code, and RMI clients need to download RMI stubs (as well as any other custom classes or interfaces needed to communicate with the RMI server).
![Page 41: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/41.jpg)
A Remote Service Appletpackage examples.hello; import java.applet.Applet; import java.awt.Graphics; import java.rmi.Naming; import java.rmi.RemoteException;
public class HelloApplet extends Applet { String message = ""; // "obj" is the identifier that we'll use // to refer to the remote object that // implements the "Hello" interface
Hello obj = null;
![Page 42: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/42.jpg)
public void init() {try {
Hello obj = (Hello)Naming.lookup("//" + getCodeBase().getHost()
+ "/HelloServer"); message = obj.sayHello(); } catch (Exception e) { System.out.println("Applet exception: " + e.getMessage()); e.printStackTrace(); }}
Remote Applet Code (2)
![Page 43: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/43.jpg)
public void paint(Graphics g) { g.drawString(message, 25, 50); }
}
Remote Applet Code (3)
![Page 44: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/44.jpg)
Notes on Client Applet
• The applet gets a reference to the remote object implementation (advertised as "HelloServer") from the server host's rmiregistry.
• The applet invokes the remote sayHello method on the server's remote object.
• The applet invokes the paint method, causing the string "Hello World!" to be displayed in the drawing area of the applet.
![Page 45: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/45.jpg)
The Web Page (hello.html)<HTML><title>Hello World</title><center> <h1>Hello World</h1> </center>
<p><applet codebase="myclasses/ " code="examples.hello.HelloApplet" width=500 height=120></applet></HTML>
![Page 46: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/46.jpg)
Compiling the Java Source Code
• Create a directory myclasses in your public_html on AFS and compile the java code:
• javac -d $HOME/public_html/myclasses Hello.java HelloImpl.java
HelloApplet.java
![Page 47: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/47.jpg)
Generate Stubs and Skeletons
rmic -d $HOME/public_html/myclasses examples.hello.HelloImpl
The following files are created in the directory:$HOME/public_html/myclasses/examples/hello
HelloImpl_Stub.class HelloImpl_Skel.class (before Java 2 only)
![Page 48: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/48.jpg)
Java 2 SDK Stubs
• In the Java 2 SDK implementation of RMI, skeleton classes are obsolete. RMI uses reflection to make the connection to the remote service object.
• If you will never need support for 1.1 clients, rmic can be run with the -v1.2 option
• rmic –v1.2 -d $HOME/public_html/myclasses examples.hello.HelloImpl
• This creates only
HelloImpl_Stub.class
![Page 49: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/49.jpg)
Deploying Applet (1)
• Move the HTML File (hello.html) to the Deployment Directory:
$HOME/public_html/
![Page 50: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/50.jpg)
Sample Policy File for User (.java.policy) in $HOME
grant {permission java.net.SocketPermission "*:1024-65535","connect,accept";permission java.net.SocketPermission "*:80", "connect";
};Replace * with name or IP of client to restrict access
![Page 51: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/51.jpg)
Security Policy
• In a Unix environment, you will probably need to create a Security Policy for the Client Application but not for an applet. The default policy might work in Windows.
• To create a security policy, see a Java text book. Just Java 2, pages 351-368, gives an explanation. Some students have expressed a preference for the explanation in Core Java 2.
![Page 52: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/52.jpg)
Start the Remote Object Registry
• The RMI registry is a simple server-side bootstrap name server that allows remote clients to get a reference to a remote object.
• Start registry on server - UNIX: rmiregistry &
• Windows: start rmiregistry
• To start on a port other than the default 1099, for example port 2001:
rmiregistry 2001 &
![Page 53: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/53.jpg)
Starting the Server
java -Djava.rmi.server.codebase=http://afsxx.njit.edu/~username/myclasses/ -Djava.security.policy=$HOME/.java.policy examples.hello.HelloImpl
The output should look like this:
HelloServer bound in registry
![Page 54: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/54.jpg)
Starting the Server• When starting the server, the
java.rmi.server.codebase property must be specified, so that the stub class can be dynamically downloaded to the registry and then to the client.
• Run the server, setting the codebase property to be the location of the implementation stubs. Because the codebase property in this example references a directory, make sure that any other classes that may need to be downloaded have also been installed in the directory referenced by java.rmi.server.codebase
![Page 55: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/55.jpg)
Starting the Server
• Note: A stub class is dynamically downloaded to a client's virtual machine only when the class is not already available locally and the java.rmi.server.codebase property has been set properly to specify where the class files are located on the server.
• You can also specify the java.security.policy property for the server to set permissions.
![Page 56: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/56.jpg)
Common Exception
• If you forget the trailing slash on the codebase property, or if the class files can't be located at the source (they aren't really being made available for download) or if you misspell the property name, you'll get thrown a java.lang.ClassNotFoundException.
• You will also need to use a VPN if you access the RMI server from off-campus because AFS security does not allow remote connections from unsigned applets. Otherwise you will get a Java.rmi.ConnectException
![Page 57: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/57.jpg)
Running the Applet
appletviewer http://afsxx.njit.edu/~usrname/hello.html &
Or use the Browser URL:http://afsxx.njit.edu/~usrname/hello.html
String Hello World should display.
Note: The Applet sandbox allows them to connect only to their host URL. Therefore, the AFS hostname
should be of the AFS machine on which the RMI server is running.
![Page 58: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/58.jpg)
Output
BrowserNote: Use web.njit.edu,not afsxx as shown. Thatwill not work as Web server access to AFS is now disabled.
Appletviewer
![Page 59: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/59.jpg)
Advantages of RMI• Object-Oriented• Safe and Secure• Easy to Write/Easy to Use• Connects to Existing/Legacy Systems
(JNI)• Write Once, Run Anywhere• Distributed Garbage Collection• Parallel Computing
![Page 60: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/60.jpg)
Garbage Collection• The Java Virtual Machine has an automatic
garbage collector that will reclaim the memory from any object that has been discarded by the running program. Garbage collection is difficult if the server and client run on different machines. The RMI subsystem implements a reference counting-based distributed garbage collection (DGC) algorithm to provide automatic memory management facilities for remote server objects.
![Page 61: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/61.jpg)
How DGC works• The remote server keeps track of all external client
references. When a reference is made, the server increases the object reference count by one and marks the object as "dirty". When a client drops the reference, the DGC decreases its reference count by one and marks the object as "clean." When the reference count reaches zero, the remote object is free of any live client references. It is then placed on the weak reference list and subject to periodic garbage collection.
![Page 62: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/62.jpg)
RMI Limitations
• RMI is not full featured middleware• No mechanism for object description• No server events• Java only, not language independent• Firewalls are not supported• Naming service is not persistent• No load balancing
![Page 63: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/63.jpg)
Example
• Users fill in the fields of an expense report. Clients communicate with the server using RMI. The server stores the expense reports in a database using JDBC.
![Page 64: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/64.jpg)
Remote Interface
import java.rmi.*;public interface ExpenseServer extends
Remote {Policy getPolicy() throws RemoteException;
void submitReport(ExpenseReport report) throws RemoteException, InvalidReportException;}
![Page 65: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/65.jpg)
The Policy Interface
public interface Policy {void checkValid(ExpenseEntry entry)
throws PolicyViolationException;}
![Page 66: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/66.jpg)
Client Code
Policy curPolicy = server.getPolicy(); while (user keeps adding entries) { try { curPolicy.checkValid(entry);
// throws exception if not OK add the entry to the expense report } catch (PolicyViolationException e) { show the error to the user } } server.submitReport(report);
![Page 67: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/67.jpg)
Server Code (1)
import java.rmi.*;import java.rmi.server.*;class ExpenseServerImpl extends UnicastRemoteObject implements ExpenseServer
![Page 68: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/68.jpg)
Server Code (2)
{ExpenseServerImpl() throws RemoteException {
// ...set up server state... } public Policy getPolicy() { return new TodaysPolicy(); } public void submitReport(ExpenseReport report) { // ...write the report into the db... } }
![Page 69: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/69.jpg)
Implementing a Policy
public class TodaysPolicy implements Policy {public void checkValid(ExpenseEntry entry) throws PolicyViolationException
{ if (entry.dollars() < 20) return; // no receipt required throw new PolicyViolationException(
"Need a receipt"); }}
![Page 70: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/70.jpg)
New Policy Implementation
public class TomorrowsPolicy implements Policy {public void checkValid(ExpenseEntry entry)
throws PolicyViolationException { if (entry.isMeal() && entry.dollars() < 20) return; // no receipt required throw new PolicyViolationException(
"Need a receipt"); }}
![Page 71: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/71.jpg)
Summary
• Java RMI is a distributed object model for the Java platform.
• RMI extends the Java object model beyond a single virtual machine address space.
• RMI uses object serialization to convert object graphs to byte streams for transport.
![Page 72: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/72.jpg)
Defining Interfaces
• All methods that can be run remotely must be declared as part of an interface that extends Remote .
public interface RemOp extends Remote {public void call() throws RemoteException;
}
![Page 73: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/73.jpg)
Creating Classes that Implement the Interfaces• Classes that implement the remote
interfaces must be subclasses of the RemoteObject class.
• RemoteObject provides support for the hashCode(), equals(), and toString().
![Page 74: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/74.jpg)
package rmi1;
import java.rmi.*;import java.rmi.server.*;
Interface Class Example (1)
![Page 75: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/75.jpg)
public class RemImpl extends UnicastRemoteServerimplements RemoOp {
public static void main(String[] args) { System.setSecurityManager(
new StubSecurityManager()); try {
Naming.rebind(“operator”, new RemImpl()); } catch (Exception x) {
x.printStackTrace(); return; } }
Interface Class Example (2)
![Page 76: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/76.jpg)
public RemImpl() throws RemoteException { }
public void call() throws RemoteException { System.out.println(getName()); System.out.println(“Location: “ +
System.getProperty(“LOCATION”));}
Interface Class Example (3)
![Page 77: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/77.jpg)
public String getName() {return “Remote operation: “ + hashCode();
}}
Interface Class Example (4)
![Page 78: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/78.jpg)
package rmi1;
import java.rmi.*;import java.rmi.server.*;
Interface Class Example (5)
![Page 79: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/79.jpg)
public class OpTest { public static void main(String[] args) { System.setSecurityManager(
new StubSecurityManager()): try { RemOp ro = (RemOp) Naming.lookup(“operator”); ro.call(); } catch (Exception x) { x.printStackTrace();
System.exit(-1); } } }
Interface Class Example (6)
![Page 80: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/80.jpg)
Running the rmi1 Example
• java java.rmi.registry.RegistryImpl• java -DLOCATION=server rmi1.RemImpl• java -DLOCATION=client rmi1.OpTest
Output:Remote operation: 841549921Location: server
![Page 81: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/81.jpg)
RMI Security
• One of the most common problems encountered with RMI is a failure due to security constraints.
• The security policy can be set by constructing a SecurityManager object and calling the setSecurityManager method of the System class.
The SecurityManager class has a large number of methods whose name begins with check. For example, checkConnect (String host, int port).
![Page 82: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/82.jpg)
Security Continued
• If the current security policy does not allow connection to this host and port, then the call throws an exception. This usually causes the program to terminate with a message such as:
java.security.AccessControlException: access denied
(java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
![Page 83: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/83.jpg)
Java-RMI vs. CORBA and DCOM
• Java-RMI is a Java-specific middleware spec that allows client Java programs to invoke server Java objects as if they were local.
• Java-RMI is tightly coupled with the Java language.
![Page 84: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/84.jpg)
Comparison Continued
• Since Java-RMI is tightly coupled with The Java Language, Java-RMI can work with true sub-classes.
• Because of this, parameters passed during method calls between machines can be true Java Objects. This is impossible in DCOM or CORBA at present.
![Page 85: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/85.jpg)
Comparison Continued
• If a process in an RMI system receives an object of a class that it has never seen before, it can request that its class information be sent over the network.
• Over and above all this, Java-RMI supports Distributed Garbage Collection that ties into the local Garbage Collectors in each JVM.
![Page 86: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/86.jpg)
Trouble Shooting
• If a JVM that is exporting remote objects does not have the java.rmi.server.codebase property set correctly, you will get a class not found error.
• Default port for RMI RegistryImpl is 1099. If it is already in use, you will get a
java.net.SocketException: Address already in use.• Do not try to use the loopback address, 127.0.0.1
improperly. It does not work on a network.• RMI is subject to Connection Exceptions that are
idiosyncratic and difficult to overcome. This is one of the most common topics in RMI discussions.
![Page 87: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/87.jpg)
Problems Encountered
• A java.rmi.NotBoundException was overcome by bypassing the start command and starting the server directly. This is not recommended, as the program will not exit normally. NOTE: A probable cause is another version of Java on the system that is referenced by the start command.
• Several java.rmi.UnmarshallException were caused by classes not in the class path. Some students just had to add the current directory:
set classpath=.’%classpath%
![Page 88: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/88.jpg)
Problems Encountered
• An Incompatible Type Exception error occurred when a group failed to cast a returned reference to the <ClassName>_stub type.
• A java.security.AccessControlException required creating a policy file and referencing it as
java -Djava.security.policy=c:\rmi.policy <YourClient./<YourServer>
• Don’t overlook standard programming errors like syntax errors, missing try blocks, and naming and typing mistakes.
![Page 89: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/89.jpg)
Hints• When you have a problem, you may have to kill
all your processes and restart everything.• You must start the registry (Windows command) start rmiregistrystart• All remote interfaces must extend
java.rmi.RemoteInterface, and all remote objects must extend java.rmi.UnicastRemoteObject.
• Security management and implementing class policies are complex and difficult in RMI. The client policy file permits connection to a specific referenced port number.
![Page 90: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/90.jpg)
Lessons Learned• You can get a trace of the server activity using java -Djava.rmi.server.logCalls=true <YourServer>• Properties beginning java.rmi.* are part of
published RMI spec and docs, while sun.rmi.* indicates things that may vary.
• You can find the host name of a caller of a remote method with
java.rmi.server.RemoteServer.getClientHost• On a server with multiple host names, you need
to force a fully qualified host name with java.rmi.server.useLocalHostname=true
![Page 91: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/91.jpg)
Environment Conflicts• If you may have multiple versions of Java on your
system, try java -version to see which version your environment defaults to. Remember that different versions seldom work together, and that your Windows autoexec.bat file may set some environment values that you might not think about.
![Page 92: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/92.jpg)
Good RMI explanation
• The example from the Sun Web site is difficult to get working, because it includes a lot of material that goes beyond the basics. You may wish to find a simpler example. One excellent one was at– http://www.ccs.neu.edu/home/kenb/com3337/
rmi_tut.html
![Page 93: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/93.jpg)
RMI Versions• Java 2 SDK, version 1.4 adds server-side stack trace
retention, Service Provider Interface for RMIClassLoader and Dynamic Server Host Name.
• Java 2 SDK, version 1.3 adds enhancements to serialization
• Java 2 SDK, version 1.2 adds activation, the use of custom socket protocols and security policies.
• Java 1.1 added core RMI support, but usually requires running applets in the applet viewer.
• Java 1.0.2 was an interim release, and should not be used for RMI.
![Page 94: JavaRMI](https://reader035.fdocuments.us/reader035/viewer/2022062618/5514fa44497959161e8b49bf/html5/thumbnails/94.jpg)
References
• http://java.sun.com/j2se/1.4.2/docs/guide/rmi/getstart.doc.html
• http://developer.java.sun.com/developer/onlineTraining/rmi/RMI.html
• http://java.sun.com/j2se/1.4/docs/relnotes/features.html
• http://java.sun.com/marketing/collateral/javarmi.html