AsyncMethod < Communications < TWiki
|
TWiki . Communications . AsyncMethod
|
-- Main.piman31415 - 02 Dec 2006
To use the asynchMethod, first create your remote object (as normal...nothing new here, except let's assume oure remote calls take a while to complete ). Example:
/*
* Created on Nov 29, 2006
*
*/
package duckServer;
import gnu.cajo.invoke.Remote;
import gnu.cajo.utils.ItemServer;
public class DuckServer {
protected String string = "The duck server's initial string.";
public String itWalks(String string) {
// Take a 5-second walk...
try
{
Thread.sleep(5000);
}
catch (InterruptedException ex)
{
}
System.out.println("Waddle, waddle!");
return string;
} // itWalks
public String itTalks(String string) {
// It's time for a long talk...
try
{
Thread.sleep(8000);
}
catch (InterruptedException ex)
{
}
System.out.println("Quack, quack!");
try { return this.string; }
finally { this.string = string; }
} // itTalks
public static void main(String args[]) throws Exception {
Remote.config(null, 1198, null, 0);
ItemServer.bind(new DuckServer(), "DuckServer");
System.out.println("The duck server is running!");
}
}
Now, for the real fun, let's add a client that calls our server asynchronously.
First, we need to create a callback object whose methods will be called upon completion of each remote object invocation. The callback object requires methods to be defined whose prototypes match exactly to the remotely callable objects in the server:
/*
* Created on Dec 1, 2006
*
*/
package duckClient;
/**
* @author Dave Calloway
*
*/
public class ClientCallBack {
public void itWalks(String string)
{
System.out.println("itWalks was called, and has completed.");
}
public void itTalks(String string )
{
System.out.println("itTalks was called, has completed, and returned: " + string + ".");
}
} // ClientCallBack
Then, our client can make use of the remote objects and rely on the callbacks to process the results:
/*
* Created on Nov 29, 2006
*
*/
package duckClient;
import gnu.cajo.invoke.Remote;
import gnu.cajo.utils.extra.AsyncMethod;
public class DuckClient {
public static void main(String args[]) throws Exception {
Object remoteDuck = Remote.getItem("//206.59.254.67:1198/DuckServer");
AsyncMethod asyncDuck = new AsyncMethod(remoteDuck, new ClientCallBack());
Remote.invoke(asyncDuck, "itWalks", "Walk!");
Remote.invoke(asyncDuck, "itTalks", "Speak!");
// Let's just print some numbers, one each second, to gauge when things
// happen on the server and in our callbacks.
for (int i=0; i<20; i++)
{
System.out.println(i);
try
{
Thread.sleep(1000);
}
catch (InterruptedException ex)
{
}
} // for
} // main
} // DuckCLient
Start the server, and you should see:
The duck server is running!
Start the client, and it's console will report:
0
1
2
3
4
itWalks was called, and has completed.
5
6
7
8
itTalks was called, has completed, and returned: The duck server's initial string..
9
10
11
12
13
14
15
16
17
18
19
Note that once the client makes its calls to the server, it proceeds on with it's application. At some later time, the server completes each requested task and the corresponding callback is then called. Very cool! (And very easy/clean. Thanks, cajo!!!
----- Revision r2 - 30 Apr 2008 - 19:44:14 - Main.chesspoker
|