Channels ▼
RSS

Tools

A Remote Java RMI Registry

Source Code Accompanies This Article. Download It Now.


Usage

Listings Three and Four illustrate the server and client side usage of our remote RMI registry (also see Figure 2).

Listing Three is a complete example for the definition of a simple RMI server object. It comprises the remote interface Hello, the implementation class HelloImpl, as well as the class HelloSetup. The latter class contains the code for startup—setting the code base from where RMI clients can download server object's code, instantiating and exporting the RMI server object, locating the remote registry, and (re)binding the server object with the remote RMI registry. The use of our remote RMI registry is completely transparent to the RMI server; no modifications have been made to the standard RMI code. With the standard RMI registry, however, the call registry.rebind("hello", stub); would have thrown an AccessException. With our custom RMI Registry, RMI servers are allowed to invoke bind, rebind, and unbind methods of a remote RMI Registry.

Listing Four shows a sample RMI client that uses the hello RMI server object. Exactly as with the standard RMI architecture, the client locates the RMI Registry, looks up the hello server object, and then invokes methods at the server object.

// Remote interface
public interface Hello extends Remote {
  String getHello() throws RemoteException;
}
// Implementation class
public class HelloImplimplements Hello {
  public String getHello() throws RemoteException {
    return "hello, world" ;
  }
}
// Startup of RMI serverobject, including registration of 
// the instantiated server object with remote RMI registry
public class HelloSetup {
  public static void main (String[] args) throws
    UnknownHostException,RemoteException,
    MalformedURLException,NotBoundException,
    InterruptedException,AlreadyBoundException {
  System.setProperty ("java.rmi.server.codebase",
         "http://192.168.2.32/hello.jar");
  Hello stub = (Hello) UnicastRemoteObject.
            exportObject (new HelloImpl(),0);
  //registration with remote RMI-Registry is now possible
  Registry registry = LocateRegistry.getRegistry ("192.168.2.31");
  registry.rebind ("hello",stub);
  System.out.println ("Successfully registered.");
  }
}
Listing Three


public class HelloClient {
  public static void main (String[] args) throws
    RemoteException,NotBoundException {
  Registry registry = LocateRegistry.getRegistry ("192.168.2.31");
  Hello server = (Hello) registry.lookup ("hello");
  System.out.println (server.getHello ());
  }
}
Listing Four

Conclusion

A remote RMI registry is useful in many distribution scenarios. A completely transparent custom registry implementation needs to resolve several interesting technical obstacles, such as how to export a remote object with a given ObjID, or how to check the validity of an existing binding. The complete source code as well as the executable jar file can be found at the RRR project homepage (www-home.htwg-konstanz.de/~haase/hp/RRR.html).


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video