| TWiki . Communications . TransparentProxy |
public interface Duck {
boolean looks();
boolean walks();
boolean talks();
}
Next let's create a simple server POJO, implementing this interface, which we would like to be remotely callable:
DuckServer.java
import gnu.cajo.invoke.Remote;
import gnu.cajo.utils.ItemServer;
public class DuckServer implements Duck {
public boolean looks() {
System.out.println("hi there!");
return true;
}
public boolean walks() {
System.out.println("waddle waddle");
return true;
}
public boolean talks() {
System.out.println("quack quack!");
return true;
}
public static void main(String args[]) throws Exception { // simple unit test
Remote.config(null, 1198, null, 0); // use cajo port 1198
ItemServer.bind(new DuckServer(), "Donald");
System.out.println("duck server running");
}
}
Now let's create a test server directory; and in it place the common interface Duck, the server object DuckServer, and of course, the tiny 40 kB cajo.jar library.
The server files can be compiled as follows:
Now let's start up the server, it will require a command line input like the following:javac -classpath cajo.jar;. DuckServer.java
OK. Now let's create a simple client unit test; to connect to, and use the duck server object: DuckClient.javajava -classpath cajo.jar;. DuckServer
import gnu.cajo.utils.extra.TransparentItemProxy;
public class DuckClient { // try out DuckServer
public static void main(String args[]) throws Exception {
Duck duck = (Duck)TransparentItemProxy.getItem(
"//serverHost:1198/Donald",
new Class[] { Duck.class }
);
System.out.println("looks like = " + duck.looks());
System.out.println("walks like = " + duck.walks());
System.out.println("talks like = " + duck.talks());
}
}
Note: the serverHost portion of the string in the getItem call, must be replaced, with the acutal host name, or IP address of the server.
Now let's create a test client directory; on either the same, or a different physical machine, and in it place the common interface Duck, the client object DuckClient, and likewise, the cajo library.
Similarly, compile the client files:
Now let's start up the client, it will require a command line input like the following:javac -classpath cajo.jar;. DuckClient.java
This will result in the following client console output:java -classpath cajo.jar;. DuckClient
and the following server console output:looks like = true walks like = true talks like = true
as the client invokes each method of the server object, with the exact syntactic transparency, as if it were local. Note: As mentioned previously, a server object is free to create and implement as many service interfaces as it wishes, to group its functional concerns; however the client also gets the unique ability to define its own interfaces to the service, by picking and chosing from any of the methods defined by the service object. This is an extremely powerful concept, known as dynamic client-side subtyping. It can be quite a head-spinner, but it is well worth taking a minute to ponder the significance of it. Also Note: Whilst all classes in this example are in the unnamed package; much unlike all-local Java: Both the client and the server are free to place their classes and interfaces into whatever packages they wish, without affecting the other, in any way. This page was an example of using a remote object locally by reference; there is also a way to use a remote object locally by value.hi there! waddle waddle quack quack!
----- Revision r3 - 04 Aug 2007 - 22:46:57 - JohnCatherino
|