0

Using EventQueue in different threads?

asked 2011-04-27 05:15:21 +0800

Lesstra gravatar image Lesstra
193 1

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.

delete flag offensive retag edit

1 Reply

Sort by ยป oldest newest

answered 2011-04-27 06:14:36 +0800

Lesstra gravatar image Lesstra
193 1

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.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-04-27 05:15:21 +0800

Seen: 745 times

Last updated: Apr 27 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More