The Source for Java Technology Collaboration
User: Password:



John Catherino's Blog

September 2007 Archives


Simple Inter-JVM communication... The Grail!

Posted by cajo on September 03, 2007 at 04:19 PM | Permalink | Comments (18)

I am very pleased to announce a most significant breakthrough from the the cajo project, in the ease with which distributed computing can be accomplished in Java; and in only 20 kilobytes. It works with all JREs, 1.3 and later. (And before you Rocket Scientists out there ask; yes, it's also 64-bit clean ;)

Just three methods: (click the link, for greater detail)

void export(Object object);
This makes any object's public methods remotely callable

Object[] lookup(Class methodSet);
This finds remote objects matching the given interface

Object proxy(Object reference, Class methodSet);
This creates a local proxy for using the remote object

That's it. These allow for complete interoperability between distributed JVMs. It just can't get any easier than this.

Any object can be exported for remote object invocation, irrespective of the classes it implements, or interfaces it extends. An exported object can be used by a client, simply if it is compatible with the signature requirements specified by the client. Argument and return type coërcion is applied, in compliance with the language specification.

Comfortably, the exported methods can be used with the same syntax and language conventions as local methods. However, it is always well to keep in mind the Eight Fallacies of Distributed Computing. [pdf warning]

Strictly speaking the simple 3 method Grail interface could be implemented by other distributed frameworks; Jini, EJB, Spring, etc. So I've placed the interface specification into the public domain: To leave that as a bit of an open challenge! ;-)

OK, time for an example: Server.java
import gnu.cajo.Cajo; // The cajo implementation of the Grail

public class Server {
   public static class Test { // remotely callable classes must be public
      public String foo(Object bar, int count) {
         System.out.println("foo called w/ " + bar + ' ' + count + " count");
         return "Thanks";
      }
      public Boolean bar(int count) {
         System.out.println("bar called w/ " + count + " count");
         return Boolean.TRUE;
      }
      public boolean baz() {
         System.out.println("baz called");
         return false;
      }
   } // but needn't be defined in the same class

   public static void main(String args[]) throws Exception { // unit test
      Cajo cajo = new Cajo(1198, null, null);
      cajo.export(new Test());
      System.out.println("Server running");
   }
}
Compile via: javac -cp grail.jar;. Server.java
Execute via: java -cp grail.jar;. Server

Now for a client: Client.java
import gnu.cajo.Cajo;

interface ClientSet { // client interfaces need not be public
   void baz();
   boolean bar(Integer quantum);
   Object foo(String barbaz, int foobar);
} // the order of the client method set does not matter

public class Client {
   public static void main(String args[]) throws Exception { // unit test
      Cajo cajo = new Cajo(0, null, null); // port does not matter for clients
      Thread.currentThread().sleep(100); // allow a little discovery time
      Object refs[] = cajo.lookup(ClientSet.class);
      if (refs.length > 0) {
         System.out.println("Found " + refs.length);
         ClientSet cs = (ClientSet)cajo.proxy(refs[0], ClientSet.class);
         cs.baz();
         System.out.println(cs.bar(new Integer(77)));
         System.out.println(cs.foo(null, 99));
      } else System.out.println("No servers found");
      System.exit(0);
   }
}
Compile via: javac -cp grail.jar;. Client.java
Execute via: java -cp grail.jar;. Client

That's all there is to it. Detailed information, including source code, can be found here, also the main project site is a very helpful resource.

Technically it's still beta, and will be officially added to the cajo project soon, however there are no known issues at this time. So please feel free to kick the tyres, and take her out for a spin!

Enjoy, and as always, your comments and feedback are encouraged.

-John



Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds