0

Publish ZK Events from a quartz thread.

asked 2012-02-23 13:00:00 +0800

cyberdeth gravatar image cyberdeth
15
www.prododgenous.com

Hi.

I've looked for a solution on the forum, and found this solution : http://www.zkoss.org/forum/listComment/11923, but it doesn't really feel right. Plus I couln't really find any examples of how to use Observables in ZK. I'm using ZK 5.0.9 PE at the moment. At the moment this is what I've got :

In the zul page :

EventQueues.lookup("systemMessagesQueue", true).subscribe(new EventListener() {
                     public void onEvent(Event event) throws Exception {
                                 Messagebox.show("Called");
                     }
}

And in my quartz thread:

EventQueues.lookup("systemMessagesQueue").publish(new Event("onAddItem", null, null));

However, when the thread is executed I get the following error :

java.lang.IllegalStateException: Not in an execution

Now I don't know if this was fixed in ZK 6.0 to work like this out of the box, however, what can I do to get this working.

Your help will be greatly appreciated.

Francois Hensley

delete flag offensive retag edit

6 Replies

Sort by ยป oldest newest

answered 2012-02-23 14:31:24 +0800

gekkio gravatar image gekkio flag of Finland
899 1
http://gekkio.fi/blog

Your event queue lookups don't include a scope, so the default Desktop(= "browser tab") scope is used. However, in your quartz thread there's no ZK request going on, so it's impossible to know which desktop you want to use. You'll also need to have a WebApp object in order to lookup event queue scopes without a ZK request.

Use Application scope instead:

WebApp webApp = // ...
EventQueues.lookup("systemMessagesQueue", webApp, EventQueues.APPLICATION)...

You'll need to somehow gain access to the WebApp object in your Quartz thread. If you have access to ServletContext, you can use this:

ServletContext ctx = // ...
WebApp webApp = WebManager.getWebManager(ctx).getWebApp();

If you do not have access to the ServletContext, you'll need to configure your app somehow so that you have access to it or directly to the WebApp instance.

When you use a scope with such a long lifecycle, you will absolutely need to manage event listener unsubscribing too or you will leak memory. See my recent forum post for more details about this subject.

link publish delete flag offensive edit

answered 2012-02-23 20:51:14 +0800

cyberdeth gravatar image cyberdeth
15
www.prododgenous.com

Hi Gekkio.

Thank you very much for the response. I did try to add the Application scope, but it still gave me the same exception. As for the servletcontext, that sounds like a good plan. I'll try that out and let you know how that goes. I did try and use WebApp but again, unfortunately, not a lot of examples of how to effectively use it in code on the forum :(, and thus I couldn't get that to work. However, your example above does make sense. I'll try that for sure.

The memory management article is very well done. Thanks for that.

One other solution, that I just thought of, is on page load, I could somehow register the desktop with the quartz bean. That would probably fix the execution exception and the scope would be a lot finer, but I will still have to implement an unsubscribe implementation, ala your forum post.

I'll let you know how that goes.

link publish delete flag offensive edit

answered 2012-02-23 22:14:12 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2012-02-23 22:14:59 +0800

@Francois, what is the usecase to interact to a frontend with quartz? why not use a timer on client side?

@Gekkio, your input is all time very useful and with high quality and deep understanding. Thanks. I'm interested in your opinon on this thread, if you have time for it.

thx
Stephan

link publish delete flag offensive edit

answered 2012-02-24 08:10:47 +0800

gekkio gravatar image gekkio flag of Finland
899 1
http://gekkio.fi/blog

+1 to Stephan's request:
Could you describe your use case so we can make a more detailed recommendation?

I'm specifically interested in the quartz job you are running. Is it something that is user-specific, like "inform this particular user that something happened"? Or is it something global, like "inform all users that a job finished"?

If you really want to use desktop scope and inform a specific user without informing everyone, a better choice might be to use plain server push and Executions.schedule without using any event queues at all. I can explain this further if you can describe what you are trying to do.

link publish delete flag offensive edit

answered 2012-02-26 22:56:50 +0800

cyberdeth gravatar image cyberdeth
15
www.prododgenous.com

updated 2012-02-26 22:57:07 +0800

Well the quartz job will read data from a stream and push the streamed data to the users desktop. The UI would then filter out the appropriate information from the the streamed data and only display the relevant information for the user. The assumption is that there would be a lot of different users logged in but we only have 1 stream of data from the 3rd party solution. So it would be easier, and less resource intensive to have the quartz job run, grab the stream of data and push it to the appropriate queue than to spawn, a timer for each logged in user to call a method on the bean. I hope that makes sense?

link publish delete flag offensive edit

answered 2012-03-05 12:48:04 +0800

cyberdeth gravatar image cyberdeth
15
www.prododgenous.com

updated 2012-03-05 19:43:48 +0800

Ok so all the above recommendations didn't work. I did however get it working with comet server push, as per the example on the live demo site. Thanks for pointing me in the right direction though.

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: 2012-02-23 13:00:00 +0800

Seen: 333 times

Last updated: Mar 05 '12

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