2008/9/5 Baptiste Boussemart <[EMAIL PROTECTED]>
>
> Hello,
>
> I am trying to run a worker with the latest api.
>
> Now many examples are now old and I am having problems with my test.
>
> package gears.worker.jsni.client;
>
> import com.google.gwt.core.client.EntryPoint;
> import com.google.gwt.gears.client.Factory;
> import com.google.gwt.gears.client.workerpool.WorkerPool;
> import
> com.google.gwt.gears.client.workerpool.WorkerPoolMessageHandler;
> import com.google.gwt.user.client.ui.Button;
> import com.google.gwt.user.client.ui.ClickListener;
> import com.google.gwt.user.client.ui.Label;
> import com.google.gwt.user.client.ui.RootPanel;
> import com.google.gwt.user.client.ui.VerticalPanel;
> import com.google.gwt.user.client.ui.Widget;
>
> public class Jsni implements EntryPoint {
>
> private final VerticalPanel panel = new VerticalPanel();
>
> private WorkerPool workerPool;
>
> public void start() {
> // create a new worker thread to start generating primes
> final int workerId = workerPool.createWorker(worker());
> panel.add(new Label(String.valueOf(workerId)));
> workerPool.sendMessage("Test", workerId);
> }
>
> private native String worker() /*-{
> function workerMessageHandler(a, b, msg) {
> $wnd.alert(msg.body);
> google.gears.workerPool.sendMessage('Echo ' +
> msg.body,
> msg.sender);
> }
> var workerCode = String(workerMessageHandler) +
> 'google.gears.workerPool.onmessage = workerMessageHandler';
> return workerCode;
> }-*/;
>
> public void onModuleLoad() {
>
> // create a new worker pool instance
> workerPool = Factory.getInstance().createWorkerPool();
> workerPool.setMessageHandler(new WorkerPoolMessageHandler()
> {
> public void onMessageReceived(final MessageEvent
> message) {
> panel.add(new Label("[" +
> message.getSender() + "] "
> + message.getBody()));
> }
> });
> // workerPool.setErrorHandler(new
> WorkerPoolErrorHandler() {
> //
> // public boolean onError(final ErrorEvent errorEvent) {
> // panel.add(new Label(errorEvent.getLineNumber() + " : "
> // + errorEvent.getMessage()));
> // return false;
> // }
> // });
Here is the javadoc for WorkerPool.setErrorHandler():
/**
* Set the [EMAIL PROTECTED] ErrorHandler}. This provides functionality in
workers
* similar to the window.onerror property. If set, it will be called for
any
* unhandled errors that occur inside a worker.
*
* You can use this callback to implement "last-chance" error handling for
* your workers. For example, you could log all unhandled errors into the
* Database module.
*
* NOTE: This callback can only be set from child workers.
*
* @param handler handler to be notified of error events
*/
For now, you'll have to define this handler in JavaScript.
> RootPanel.get().add(panel);
> panel.add(new Button("Invoke", new ClickListener() {
>
> public void onClick(final Widget arg0) {
> panel.add(new Label("started"));
> start();
> }
>
> }));
> }
> }
>
> I got the invokation of start() but the worker does not respond.
>
> I want to find a worker that works with the latest API.
>
> // my code, based on Gears API (non GWT) Worker example
> private native String worker() /*-{
> function workerMessageHandler(a, b, msg) {
> $wnd.alert(msg.body);
> google.gears.workerPool.sendMessage('Echo ' +
> msg.body,
> msg.sender);
> }
> var workerCode = String(workerMessageHandler) +
> 'google.gears.workerPool.onmessage = workerMessageHandler';
> return workerCode;
> }-*/;
>
> // some code a year ago
> 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;
> }-*/;
>
> Is "gearsWorkerPool" correct?
I don't see the part of the code that assigns a value to 'gearsWorkerPool'.
I think you mean to use google.gears.workerPool, or assign gearsWorkerPool
to google.gears.workerPool when the worker is started. See
http://code.google.com/apis/gears/api_workerpool.html
>
> Is "workerMessageHandler(msg, sender)" correct?
>
>
> I also have a problem with setErrorHandler
>
> Uncaught Error: The onerror property cannot be set on a parent worker
> (from Google Chrome Javascript console)
> The page stops completely.
(see above - setErrorHandler() can only be called from a worker)
>
>
>
> References
> http://groups.google.com/group/Google-Web-Toolkit/msg/4aec89cb494e56ce
>
> Thank you.
>
> >
>
--
Eric Z. Ayers - GWT Team - Atlanta, GA USA
http://code.google.com/webtoolkit/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---