WorkerPool

3 views
Skip to first unread message

darkAngel

unread,
Sep 14, 2007, 5:12:27 AM9/14/07
to Google Web Toolkit, WorkerPool
Hi:

I've started using Google Gears in my GWT application. In the Google
Gears I did not found any sample that explains how to use the
WorkerPool class.

The WorkerPool class relies on JavaScript to execute JavaScript
threads.

I'm looking for a sample that explains how to deals with this class,
in Java programming language.

Thank you ...

Les Harris

unread,
Sep 14, 2007, 5:26:31 AM9/14/07
to Google-We...@googlegroups.com
Are you using the GWT API for Gears?

Its located here: http://code.google.com/p/gwt-google-apis/

If you are using that then I would direct you to the WorkerPool source
which states:

* 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.

So you are out of luck if you want to create this javascript thread
groups from the java side of things. You need to create them in the
javascript.

darkAngel

unread,
Sep 14, 2007, 5:35:54 AM9/14/07
to Google Web Toolkit
Hi Les Harris:

Yes, I'm talking about GWT API for Gears.

I can not find any sample/information that explain how to use the
WorkerPool class using the Java programming language.
I'm not a JavaScript developper, I just know some basics of the
JavaScript programming language. This why I face many difficulties.

I'm doing my utmost to uses this class in my application since it
interacts with databases.

Thank you ...

Les Harris

unread,
Sep 14, 2007, 5:46:37 AM9/14/07
to Google-We...@googlegroups.com
Hi again,

The WorkerPool class is a way to create threads in *javascript* using
*javascript*.

You cannot create these workerpool thread bodies in Java with the
current API. In fact, if you look at the source of the class you will
see that it is mostly methods wrapping JSNI code.

Again:


* 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.

You need to pass Javascript in a string to the worker creation methods
to create the WorkerPool threads. Use something like the following
method to create the thread:
public int createWorkerFromString(String javaScript)

In summary: You can't use Java to create WorkerPool thread bodies. You
need to use Javascript.

On 9/14/07, darkAngel <ab.dar...@gmail.com> wrote:
>

darkAngel

unread,
Sep 14, 2007, 6:06:19 AM9/14/07
to Google Web Toolkit
Hi:

I know that you must pass a JavaScript code to the
createWorkerFromString() method.

Is there any concrete example/sample that demonstrates how to put in
practice this class?

Bye ...

Jason Essington

unread,
Sep 14, 2007, 11:13:57 AM9/14/07
to Google-We...@googlegroups.com
Actually you can! Do a search of the contrib list, I did it right after Google Gears came out.

That said, it is a huge PITA to get everything set up to make it work. What GWT really needs is some core level modifications to create "WorkerEntryPoints"

-jason

Sumit Chandel

unread,
Sep 19, 2007, 6:41:34 PM9/19/07
to Google-We...@googlegroups.com
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#5d96b22d807f58e7

Option 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
Reply all
Reply to author
Forward
0 new messages