Get smart with proxies and
RMI - JavaWorld
November 2000
Tutorial Details:
Get smart with proxies and RMI
Get smart with proxies and RMI
By: By M. Jeff Wilson
Use dynamic class loading to implement smart proxies in RMI
ava Remote Method Invocation (RMI) gives clients access to objects in the server virtual machine (VM) in one of two ways: by reference or by value. To access a remote object by reference, the object must be an instance of a class that:
Implements an interface that extends java.rmi.Remote
Has a properly generated RMI stub class that implements the same interface
Is properly exported to allow incoming RMI calls
The interface implemented both by that class and its stub is referred to as the object's remote interface, and the methods declared in that interface can be invoked remotely. Clients only need to know about the remote interface. When they request a reference to a remote object that implements that interface, RMI substitutes an instance of the stub class and returns a copy of that stub to the client. Method calls on the RMI stub are forwarded to the remote object, which is still in the server VM. The substitution of a stub for the remote object happens automatically -- you just need to ensure that the server object implements the remote interface and is properly exported.
With pass-by-reference, all methods on the object are remote calls. That lets the remote object have access to the server's resources and services and, because multiple clients can talk to the same object on the server, changes made to that object's state by one client are visible to all clients. However, all the problems of network communications -- latency, disconnects, and time-outs, for example -- still apply.
Clients access an object by value when a remote call results in a return value of an object that implements java.io.Serializable instead of java.rmi.Remote . In that case, the returned object is serialized, sent to the client's VM, and deserialized, instantiating a copy of the object in the client's VM. Methods invoked on this copy are just like any other Java method -- they execute locally, without communicating with the server.
For pass-by-value, none of the network communication problems occur; however, the copy does not have easy access to server-side resources and services. Further, each time a client makes the remote method call to get the object, a new copy is created in the client VM. Since each copy maintains its own state, the changes made in one object cannot be seen by any other object.
Situations may arise that require a blend of those two strategies: an object in which some methods execute locally, without network latencies and other problems, and some methods that execute on the server. Ideally, some parts of the object's state could be shared, so that all clients could see changes, and other parts of the state kept private. In the CORBA world, that problem is solved with a construct called a smart proxy.
Smart proxies
A smart proxy is a class, instantiated in the client VM, that holds onto a remote object reference. It implements the object's remote interface and typically forwards most of the calls on the interface to the remote object, just like an RMI stub. However, a smart proxy is more useful than the RMI stub in that you can change the behavior of the remote interface to do more than forward calls to the remote object. For instance, a smart proxy can locally cache state from the remote object to avoid the network overhead on every method call.
CORBA implementations typically use a client-side object factory to instantiate smart proxies. The application calls a method of the factory to request a remote object reference. The factory gets the remote object reference, instantiates a smart proxy object that implements the same interface, and stores the remote reference in the proxy. The proxy is then returned to the caller.
That approach works in pure Java applications as well. It has a few drawbacks, however. Client-side code typically has to know about the implementation of the server's objects, for instance, in order to know which attributes are safe to cache and which need to be read from the server each time. Another problem is that, as the server application changes, some remote objects that were good candidates for smart proxies might no longer be appropriate; other classes that didn't need a smart proxy on the client might now need one. Each of those changes requires changes on the client code that may already be distributed.
A better solution would be to take advantage of RMI's ability to dynamically download classes that the client doesn't know about at runtime and implement the use of smart proxies in the server's code. That way, the client only knows that it is getting an object that implements the remote interface -- it doesn't know whether the object is a remote reference, a copy of a remote object, or a smart proxy. The server developer can, in fact, change the implementation from one of those to the other, and the client will continue to work without change.
To implement smart proxies in the server, it helps to understand how RMI passes object references:
If the object returned from a remote method call implements an interface that extends java.rmi.Remote , the JVM believes that object should be a remote object and tries to construct an instance of the RMI stub for it. The stub is returned in place of the original object's reference. That is the pass-by-reference case as explained above; calls on the stub are forwarded to the server object.
Otherwise, if the object implements java.io.Serializable , the object itself is serialized and sent to the client VM, which then creates a copy of the object. (Primitive types, such as byte , char , boolean , int , and float , are always serialized and passed by value.) That is the pass-by-value case.
Finally, if neither of the above cases hold, an exception is thrown.
When a serializable object is sent to the client VM, each of the object's non-transient fields goes through the same scrutiny. That is, if the field's type is a class that implements an interface that extends java.rmi.Remote (and it's not declared transient ), an RMI stub is generated, sent to the client, and substituted for the field in the client's copy of the remote object. If the field is a reference to a serializable object, a copy of the field's data is serialized and sent to the client. In that case, the field in the client's VM will refer to that deserialized copy. That occurs recursively until all of the object graph's nontransient fields have been examined and sent appropriately.
A smart proxy must, therefore, implement java.io.Serializable , while not implementing an interface that extends java.rmi.Remote . At the same time, the proxy must implement the same interface as the server object. That allows the client to use the proxy as if it was the server object. Achieving all three of those goals might appear a little tricky, however. How does the proxy implement the server object's interface and not implement java.rmi.Remote ? The answer lies in refactoring the way remote objects are typically implemented.
Conventional remote objects
"Beg your pardon, sir, but your excuse, 'We've always done it this way,' is the most damaging phrase in the language."
-Rear Admiral Grace Hopper, Ret.
Suppose that you want remote access to a Door object that contains methods, which return the door location and detect if the door is open. To implement that in Java RMI, you need to define an interface that extends java.rmi.Remote . That interface would also declare the methods that comprise the object's remote interface. Likewise, you need to define a class that implements that interface and can be exported as a remote object. The easiest way to define that class is to extend java.rmi.server.UnicastRemoteObject . That leads to the design shown in the UML class diagram below.
Figure 1. Door and DoorImpl class diagram
The remote interface, Door , extends java.rmi.Remote and declares the interface you need for Door objects. DoorImpl is the class that actually implements the Door interface. DoorImpl also extends java.rmi.server.UnicastRemoteObject so that instances of it can be accessed remotely.
Below is the code that you could use to implement that design:
/**
* Define the remote interface of a Door.
* @author M. Jeff Wilson
* @version 1.0
*/
public interface Door extends java.rmi.Remote
{
String getLocation() throws java.rmi.RemoteException;
boolean isOpen() throws java.rmi.RemoteException;
}
/**
* Define the remote object that implements the Door interface.
* @author M. Jeff Wilson
* @version 1.0
*/
public class DoorImpl extends java.rmi.server.UnicastRemoteObject
implements Door
{
private final String name;
private boolean open = false;
public DoorImpl(String name) throws java.rmi.RemoteException
{
super();
this.name = name;
}
// in this implementation, each Door's name is the same as its
// location.
// we're also assuming the name will be unique.
public String getLocation() { return name; }
public boolean isOpen() { return open; }
// assume the server side can call this method to set the
// state of this door at any time
void setOpen(boolean open) { this.open = open; }
// convenience method for server code
String getName() { return name; }
// override various Object utility methods
public String toString() { return "DoorImpl:["+ name +"]"; }
// DoorImpls are equivalent if they are in the same location
public boolean equals(Object obj)
{
if (obj instanceof DoorImpl)
{
DoorImpl other = (DoorImpl)obj;
return name.equals(other.name);
}
return false;
}
public int hashCode() { return toString().hashCode(); }
}
Now that you've defined and implemented the Door interface, the next step is to allow remote clients to access Door 's various instances. One way to do that is to bind each instance of DoorImpl to the RMI registry. The client would then have to construct a URL that contained the nam
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Get smart with proxies and
RMI - JavaWorld
November 2000
View Tutorial: Get smart with proxies and
RMI - JavaWorld
November 2000
Related
Tutorials:
|
Displaying 1 - 50 of about 1604 Related Tutorials.
|
J2EE Tutorial - Running RMI Example
; and submit.
19) We will get the greeting :
" SUN'S RMI...
J2EE Tutorial - Running RMI Example
J2EE Tutorial - Running RMI Example
  |
J2EE Tutorial - RMI Example
J2EE Tutorial - RMI Example
J2EE Tutorial - RMI... to the enquiry desk and
if we want to contact him, we have to get his... the journey with RMI.
Remote Method Invocation is a corba |
Free VoIP Proxies
Free VoIP Proxies
Free VoIP Proxies
 ... of RFC 3489 except the ability to get OTPs from the server.
  |
GPS Smart Shoes
GPS Smart Shoes
GPS Smart Shoes... Smart GPS Shoes:
The one is a California based company GTX has lunched a range of Smart GPS shoes. The GTX Corporation has announced this development |
CORBA and RMI Books
CORBA and RMI
Books
CORBA and RMI
Books... involved in choosing among sockets, HTTP/CGI, remote method invocation (RMI...-by-Value, IDL-to-Java, and RMI-to-IIOP Uses tutorials and client/server |
RMI Plugin for Eclipse
Eclipse Plugin-Language
RMI Plugin for Eclipse...;
The RMI Plug-in for Eclipse is the most comprehensive
solution for developing Java
RMI systems using the Eclipse
platform. Besides generation |
Eclipse plugin-Network
;
RMI Plugin for Eclipse
Genady's RMI Plug-in for Eclipse is the comprehensive solution for developing RMI applications. Apart from the automatic generation of RMI stubs, the RMI Plugin makes it simple |
Displaying Hello using RMI
Displaying Hello using RMI, RMI Tutorial
Displaying Hello using RMI
 ... to display Hello message using
RMI. By RMI we mean Remote Method Invocation. RMI |
Get Month Name Example
Get Month Name Example
Get Month Name Example... in understanding
a how to get a 'Get Month Name Example'.For this we have...
with a specific time zone and locale.
2)get(Calendar.Month) - This get  |
JFreeChart - An Introduction
java chart library.
David Gilbert founded the JFreeChart project in February 2000..., Gantt Charts. Developers
get a better choice to add professional quality charts |
Java Get Month
Java Get Month
Java Get Month... and how
to get the current date and time. But in this example, we are going to show the
current month of the existing year. This Java get month example is simply |
Get Property by Name
Get Property by Name
Get Property by Name... in
understanding to get Property by Name. For this we have a class name "Get... that
help you to get the Properties of System-
1)System.getProperties ( ) -  |
EBPP
of Electronic Bill Presentment and Payment is a smart way of bill collection and its... for downloading so the users can get a hard copy or can save it into theirs’ own PC |
Get Image
Get Image
Get Image
 ...;
This Example shows you get the image.
Description of the code... to true.getDefaultToolkit is used for get the default toolkit.
Toolkit.getImage |
JSF Static Navigation Example
, you will find the code of the example and can get
more about the static |
Java get available Font
Java get available Font
Java get available Font
 ...;
In this section, you will learn how to get the available font |
Get Image Size Example
Get Image Size Example
Get Image Size Example...;
This Example shows you get the image size.
Description...;java.awt.headless" is set to true.getDefaultToolkit is used for get the default |
Java example program to get the operating system name of the system
java example program to get the operating system name
of the system
Java example program to get the operating system name...;
java get Operating System name
Many times we need |
HTML Get Radio Button
HTML Get Radio Button
HTML Get Radio Button...;
HTML Get method in HTML is used to send the data as part of the URL.
The get |
Mysql Date Get Year
Mysql Date Get Year
Mysql Date Get Year...;
Mysql Date Get Year is the way of retrieving the current year.
Understand with Example
The Tutorial illustrate an example from 'Mysql Date Get Year |
Get Method of the Form In JSP
Get Method of the Form In JSP
Get Method... of the get
method of the form in JSP. The HTTP get method sends data to the server.
In your JSP application, the get method of the form carries the form data |
Java Get Example
Java Get Example
Java Get Example... Get
examples like how to get the date and time, IP Address or memory size etc... to get the things fast and practice these
code at your end to learn java easily |
Java Get Method
Java Get Method
Java Get Method...;
In this example you will learn how to use the get method in Java.
Java... to use get method in Java.
The example is going to show the date details |
JDBC Get Metadata
Jdbc Get Metadata
JDBC Get Metadata...;
The
Tutorial illustrate a program that helps you to understand JDBC Get... the number, type
and properties of the result set object.6) get column count |
Java get Folder Size
Java get Folder Size
Java get Folder Size
 ...;
In this section, you will learn how to get the size of specified |
Java Get Mime Type
Java Get Mime Type
Java Get Mime Type...;
To get the mime type, we have defined FileNameMap interface... in
Java.
Go through the given example to find out, how to get mime type |
JSP bean get property
JSP bean get property
JSP bean get property... get property. In this example we
define a package bean include a class Employees... and address. The get Address, first Name, last
Name return you the value from a bean |
Java get Next Day
Java get Next Day
Java get Next Day...;
In this section, you will study how to get the next day in java using... in
order to get the names of the days of the week. The method getWeekdays()
provide |
Java Get Class path
Java Get Class path
Java Get Class path
 ...;
In this section, you will learn how to get the class path. The method |
Get Environment Variable Java
Get Environment Variable,Java Get Environment Variable,Get Environment Variable Example
Get Environment Variable Java...;
In this example we are getting environment variable. To
get the environment |
Get Tomorrow Date
Get Tomorrow Date
Get Tomorrow Date...; in
understanding Get Tomorrow Date. For this we have a class name...
formatter. The Calendar class has a method get Instance method() returns you |
Get Local Host Name
Get Local Host Name
Get Local Host Name... in
understanding a code how to 'Get Local Host Name'. For this we have a class name....
The System.outprintln print the interface name by calling the
get Display Name ( ) method.
2 |
JDBC Get Row Count
JDBC Get Row Count
JDBC Get Row Count
 ...;
In
this Tutorial we want to describe a code that make you to understand in JDBC Get Row... be used further to get
information about the types and properties of the columns |
JDBC Get Int
JDBC Get Int
JDBC Get Int
 ...;
In this tutorial we want to describe you a code that help in
understanding JDBC Get Int. The code include a class Jdbc Get Int.Inside this
class we have a main method |
Get computer name in java
Get computer name in java
Get computer name in java
 ...;
We can get the computer name by the java code program |
Java Get File Name
Java Get File Name
Java Get File Name
 ...;
In order to get the file name, we have provided the path of file 'Hello.txt" |
Get Usage Memory Example
Get Usage Memory Example
Get Usage Memory Example
 ... in understanding Get Usage
Memory. The class Memory Usage include the main method |
JavaScript Array get key
JavaScript Array get key
JavaScript Array get key... you in
understanding JavaScript Array get key. For this we implement JavaScript..._Array_get_key.html
<html>
<head> |
Get Session Id
Get Session Id
Get Session Id... Get Session Id .In this example we import a package... the information of the user. The servlet include the
class Get Session Id,Inside |
Get Length of Array
Get Length of Array
Get Length of Array
 ... in
get a length of array. For this we are using JavaScript language |
Java program to get the desktop Path
Java program to get the desktop path
Java program to get the desktop
Path
 ... have to get the desktop path
of the system. In the java environment we can get |
Get File Size
Get File Size
Get File Size
 ... in
understanding Get File Size. For this we have a class name" Get File Size" Inside |
Java get Stack Trace as String
Java get Stack Trace as String
Java get Stack Trace as String
 ...;
In this section, you will learn how to get |
Get XML Elements
Java XML Parsing,Java XML Parser Sax,Get XML Element,XML Elements
Get XML Elements
 ... to develop a simple java program
to get the names of all elements contained |
Get Length of Array
Get Length of Array
Get Length of Array...;
In this Tutorial we make a simple example to understand Get Length...; -This method get you the calendar current time.
5)monthname.length ( )-This method return |
Java Get Time in MilliSeconds
Java Get Time in MilliSeconds
Java Get Time... how to get the time in
milliseconds. Please do not forget to import.....
Java Syntax to get time in milliseconds |
Basic Components & Functionality of LBS
: Consists of basic electronic instruments like mobile phone, smart phone, laptop... phones, smart phone, pocket PC, Laptop or other PDA devices that can be used |
GET and POST Method of HTTP
;
GET
The Get is one the simplest Http method... may be a HTML
page, a sound file, a picture file (JPEG) etc. We can say that get... send
parameters to the server. But the total amount of characters in a GET |
Java get Free Memory
Java get Free Memory
Java get Free Memory
 ... to get the
memory left of being allocated, the method getruntime() returns |
Java get File Type
Java get File Type
Java get File Type
 ...;
This section illustrates you how to get the file type. In the given |
|
|
|