views:

380

answers:

1

In order to use the WorkerPool (Javascript Threads) class and functionality in GWT/Gears, do I need to write my code in both Java and Javascript? Upon inspection of WorkerPool - Getting Started and the sample WorkerPool application in the GWT-Gears-1.1 download package it seems that I need to. It seems that the Java code is used to demonstrate the synchronous nature of Javascript and the same algorithm in Javascript is used for the asynchronous calls.

Am I missing something? Wasn't GWT created so that you don't have to write Javascript code? Why can't you write your WorkerThread code in Java and have the compiler compile it to Javascript?

Here is the relevant Java code from that url:

 public void syncWorker(String message) {
// Busy wait for 5 seconds
long end = System.currentTimeMillis() + 5000;
while (true) {
  int busyCounter = 1000;
  String busyVar;
  while (busyCounter-- > 0) {
    busyVar = "garbage" + busyCounter;
  }
  long now = System.currentTimeMillis();
  if (now > end) {
    break;
  }
}
logMessage("Approved: " + message);
}

Here is the relevant Javascript code in worker.js (notice how it is the same algorithm written in Javascript):

function doWork(message) {
// Busy wait for 5 seconds
var start = new Date();
while (true) {
  var busyCounter = 10000;
  var busyVar;
  while (busyCounter-- > 0) {
    busyVar = "garbage" + busyCounter;
  }
  var now = new Date();
  if (now.getTime() - start.getTime() > 5000) {
    break;
  }
 }

 return "Approved: " + message;
 };

Here is the call in Java:

wp = Factory.getInstance().createWorkerPool();
wp.setMessageHandler(GearsWorkerPoolTutorial.this);
workerId = wp.createWorkerFromUrl("worker.js");

Can anyone with experience in GWT and Gears shed some light on this?

Thanks!

Edit:

It seems according to this http://markmail.org/message/gxipzth2jheccpha#query:+page:1+mid:sabv4llpymbavt7t+state:results my suspicions are true. I'm not sure I understand why this limitation exists. I would love to read any insight on the matter.

Edit 2:

This also seems to confirm it: http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/658f6665e5b09bed

+1  A: 

I have never used Gears for GWT, but after reading your evidence, I believe you are correct in stating that the WorkerPool class can run only JavaScript files and not Java-compiled-to-JavaScript. Here is a quote from the GWT-Gears v1.1.1 WorkerPool API.

WorkerPool module allows web applications to run JavaScript code in the background, without blocking the main page's script execution. Currently this class can only create worker threads out of raw JavaScript code. That is, user code cannot currently create worker bodies from Java code.

The WorkerPool class manages the threads and communication, which is no small task.

Bill