Hi darkAngel,
Glad to see that you're interested in using the Gears for GWT API! Unfortunately, there isn't as much sample code demonstrating the use of the WorkerPool module for GWT/Gears out there as we would have liked there to be. But have no fear, I'll post an example down below that you can reference for your application.
Before you begin, however, could you post up some details about what you're planning on using the WorkerPool module for? I just want to be sure it will be able to handle what you're planning to throw at it (namely, it doesn't have access to the DOM).
Now then, there are a few things to consider before you go ahead and use the WorkerPool module. For one, currently it can indeed only create worker threads out of raw JavaScript code. That means, you have one of three options to start creating worker threads in your GWT module:
1) Use JSNI (demonstrated below)
2) Create a js file containing the JavaScript worker code and in your application, GET the file and create your worker threads with its contents (somewhat trivial)
3) Write it in Java code using the method that Jason is referring to (which in his own words, is a PITA).
*4*) Write it in Java code and have it "just work" as you would expect without any setup tricks (currently planned for a future release of gwt-google-apis)
Out of the three options, I would recommend you go with option 2). The reason I wouldn't suggest option 1) is because there are currently issues with this approach in the gwt-google-apis library with the WorkerPool module. The problem is that once GWT obfuscates the JSNI code that you use to create the worker threads, the Gears WorkerPool module blows up, and inevitably crashes your browser. On the other hand, option 2) would work if you compile your GWT module using the -style DETAILED option, but this defeats the purpose of one of GWT's major benefits: smaller script sizes.
For option 3), check out the thread below that Jason was referring to:
http://groups.google.com/group/Google-Web-Toolkit-Contributors/browse_thread/thread/d70bdbaeffa597b4/5d96b22d807f58e7?lnk=gst&q=workerpool&rnum=1#5d96b22d807f58e7Option 2) should be fairly simple. The example below is actually using the JSNI method, but it should be enough to give you an overall idea of how the WorkerPool module works with GWT and how you would write the JavaScript code that you would need to feed to your worker threads.
Let's suppose you have an application that needs to compute all primes less than and equal to some given number. Handling this computation on the server-side would present extra strain on your server, as well as mean that your application will have another HTTP roundtrip to get the prime number results. Handling this computation on the client-side without the use of the Gears WorkerPool module means that your client application will be busy crunching numbers rather than process all those fancy Ajax-y features we've come to love. The solution Gears is offering is to have this happen in the worker threads that can handle all the heavy computation while allowing your browser client to keep its focus on your application.
Using Gears for GWT to have prime number computation happen in Gears worker threads, your entry point class would contain something similar to the code snippet below:
public class MyClient implements EntryPoint {
...
WorkerPool workerPool;
MyMessageHandler messageHandler = new MyMessageHandler();
int workerId = 0;
private String primes = "";
/**
* Message handler for the workerPool class. It stores the sequence
* of computed primes in the primes String upon receiving messages
* from worker threads.
*/
private class MyMessageHandler implements MessageHandler {
public void onMessageReceived(String message, int srcWorker) {
primes = message;
}
}
/**
* Instantiates the Gears workerpool instance and creates prime number
* generating worker threads.
*/
public void startGenerator() throws GearsException {
//create a new worker pool instance
workerPool = new WorkerPool(messageHandler);
//create a new worker thread to start generating primes
workerId = workerPool.createWorkerFromString
(getPrimeGenerationFunction());
}
/**
* Stringifies and returns JavaScript code that will be run by the worker pool's
* thread. The JavaScript code defines a worker thread message handler function
* which will generate the sequence of primes less than or equal to a given number
* and return this message back to the workerpool. This method Stringifies this
* code and appends a last snippet that sets the worker thread's onmessage handler,
* and then returns this code as a string.
*/
private native String getSudokuGenerationFunction() /*-{
function workerMessageHandler(msg, sender) {
try {
//computePrimes computes all primes for the number in msg
var primes = computePrimes(msg);
gearsWorkerPool.sendMessage(primes, sender);
} catch(e) {
//take corrective action
}
}
var workerCode = String(workerMessageHandler) + '
gearsWorkerPool.onmessage = workerMessageHandler';
return workerCode;
}-*/;
}
That should help get you on your way :-) Also, the relationship between the Gears for GWT API and the Gears API itself is fairly one-to-one, so you may find it useful to check out the Gears home page for a better idea on using Gears modules like the WorkerPool:
http://code.google.com/apis/gears/Hope that helps,
-Sumit Chandel