-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I have the following problem.
I 'm starting new Thread in one of my controller classes (extends GenericForwardComposer) on a button click.
This thread runs some checking on a remote FTP server, and is basically background operation, because I don't want to block the main thread waiting half an hour for some response.
I have a class StatusChecker which implements Runnable, and I start the thread like this:
public void onClick$someButton() { ... this.runner = new Thread(new StatusChecker(ip, user, pass, configPath, log, 12)); this.runner.start(); }
I need some mechanism to notify back when this thread finishes. In the run() method I have something lke this:
public void run() { connect(); .... for (int i = 0; i < this.tries; i++) { Thread.sleep(5000); int res = remoteFileStatus(this.name); if(res == -1) { status = true; break; } else if(res == 0) { status = true; String msg = "Status: OK [Config successfully applied]"; logger.info(msg); <b >doNotify(msg);</b> break; } else if(res == 1) { status = true; String msg = "Status: WARNING [Config may contain errors]"; logger.warning(msg); <b >doNotify(msg);</b> break; } else { status = false; continue; } } disconnect(); }
The method doNotify() should contain some logic for notifying back in the GUI (some message window, or popup window).
What i tried is the following: I registered eventListener like this in my controller class
// NOTIFICATIONS EventQueues.lookup("notificationEventQueue", EventQueues.SESSION, true).subscribe(new EventListener() { @Override public void onEvent(Event event) throws Exception { String msg = (String) event.getData(); setNotification(msg); // show msg window Window win = createNotificationWindow(); ((Textbox) win.getFellow("notificationTextbox")).setValue(getNotification()); } });
and in doNotify() I tried to publish event, like this
private void doNotify(String message) { EventQueues.lookup("notificationEventQueue", EventQueues.SESSION, true).publish(new Event("onNotificationEventQueue", null, message)); }
But, of course, I get IllegalStateException ("Not in an execution") in this method, because Executions.getCurrent() always returns null.
I'd really appreciate some suggestions on this matter., or some possible solution.
Cheers.
Ok, I think I solved it with using lookup(String, WebApp, autocreate) method.
Just passed Desktop.getWebApp() as parameter to StatusChecker class (which runs in separate thread), and all the code logic above stays the same.
Asked: 2011-04-27 05:15:21 +0800
Seen: 745 times
Last updated: Apr 27 '11