cajo

client server communication example

The 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

// though not necessarily declared in the same class

private final String greeting;

// no silly requirement to have no-arg constructors

public Test(String greeting) { this.greeting = greeting; }

// all public methods, instance or static, will be remotely callable

public String foo(Object bar, int count) {

   System.out.println("foo called w/ " + bar + ' ' + count + " count");

   return greeting;

}

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 true;

}

public String other() { // functionality not needed by the test client

   return "This is extra stuff";

}
   } // arguments and return objects can be custom or common to server and client
 
   public static void main(String args[]) throws Exception { // unit test

Cajo cajo = new Cajo(0);

System.out.println("Server running");

cajo.export(new Test("Thanks"));
   }
}

Compile via:

javac -cp cajo.jar;. Server.java

Execute via:

java -cp cajo.jar;. Server

As you can see with just 2 commands :

Cajo cajo = new Cajo(0);
cajo.export(new Test("Thanks"));

we can expose any POJO (Plain Old Java Object) as a distributed service!

And now the Client.java

import gnu.cajo.Cajo;
 
import java.rmi.RemoteException; // caused by network related errors
 
interface SuperSet {  // client method sets need not be public
   void baz() throws RemoteException;
} // declaring RemoteException is optional, but a nice reminder
 
interface ClientSet extends SuperSet {
   boolean bar(Integer quantum) throws RemoteException;
   Object foo(String barbaz, int foobar) throws RemoteException;
} // 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);

if (args.length > 0) { // either approach must work...

   int port = args.length > 1 ? Integer.parseInt(args[1]) : 1198;

   cajo.register(args[0], port);

   // find server by registry address & port, or...

} else Thread.currentThread().sleep(100); // allow some discovery time
 

Object refs[] = cajo.lookup(ClientSet.class);

if (refs.length > 0) { // compatible server objects found

   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 server objects found");

System.exit(0); // nothing else left to do, so we can shut down
   }
}

Compile via:

javac -cp cajo.jar;. Client.java

Execute via:

java -cp cajo.jar;. Client

Related Article:

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button