|
 |
|
 |
This short walkthrough will demonstrate both the ease of use of the
framework, as well as provide a practical template for building applications.
We will start with a server first, then move on to the construction of a
client. Once you understand this material, it will be possible for you to
successfully distribute any application that you can make work in a
single Virtual Machine. In fact, this is a highly recommended approach for
application development; first get the whole thing working in a single JVM,
then add cajo to distribute it.
Note: if you do not have it already, you will need the
cajo.jar library.
Let's extend the simple object of which we learnt, in the
overview: (feel free to cut and paste)
import gnu.cajo.invoke.Remote;
import gnu.cajo.utils.ItemServer;
public class SomeObject {
protected String string = "this thing";
public String foo() {
System.out.println("foo method called!");
return string;
}
public String baz(String string) {
System.out.println("baz method called!");
try { return this.string; }
finally { this.string = string; }
}
public static void main(String args[]) throws Exception {
Remote.config(null, 1198, null, 0);
ItemServer.bind(new SomeObject(), "someName");
System.out.println("The server is running!");
}
}
Here we have a basic server. Quite intuitive already isn't it? The static
main method will launch the server, operating on the IANA assigned cajo
TCP port of
1198.
The next step would be to compile the server, it is done thus:
javac -classpath cajo.jar SomeObject.java
Now it is ready to be run, to do this type:
java -classpath cajo.jar;. SomeObject
This will start up the server. Believe it or not, we are finished with this
phase.
|
|
 |
|
 |
A client is a remote application, that uses one or more served objects as
if they were local resources. Let's see just how easily this is done:
import gnu.cajo.invoke.Remote;
public class SomeClient {
public static void main(String args[]) throws Exception {
Object object = Remote.getItem("//serverHost:1198/someName");
String s = (String)Remote.invoke(object, "baz", "hello");
System.out.println("first string = " + s);
s = (String)Remote.invoke(object, "foo", null);
System.out.println("second string = " + s);
}
}
Note: The serverHost part in the getItem
call must be replaced, with the actual IP address, or machine name, on
which your server is running. Both the client, and the server, can be
run on the same physical machine.
Let's compile the client:
javac -classpath cajo.jar SomeClient.java
It is now ready to run, simply type:
java -classpath cajo.jar;. SomeClient
The results should come out exactly as you would expect.
|
 |
|
 |
Now feel free to expand upon, and test, with this simple template. If it hasn't
become apparent already; an application can be both a client and
a server.
Next, when you are ready to learn some more sophisticated techniques; the
proxy,
scripting,
transparency, and
multicast turorials, await you.
|
If your design can work within a single JVM, cajo can scale it transparently!
|
|