Event Queues

From Documentation

Overview

An event queue is an event-based publish-subscribe solution for application information delivery and messaging. It provides asynchronous communications for different modules/roles in a loosely-coupled and autonomous fashion.

By publishing, a module (publisher) sends out messages without explicitly specifying or having knowledge of intended recipients. By subscribing, a receiving module (subscriber) receives messages that the subscriber has registered an interest in, without explicitly specifying or knowing the publisher. Eventqueue-concept.jpg

The purpose of event queues are two folds:

  1. Simplify the many-to-many communication.
  2. Make the application independent of the underlining communication mechanism. The application remains the same, while the event queue can be implemented by use of Ajax, server push and even message queue.

Identification of an Event Queue

An event queue is identified by a name and a scope. The scope represents the visibility of an event queue. For example, a desktop-scoped event queue is visible in the same desktop, while application-scoped event queue is visible in the whole application.

Locate an Event Queue

You could locate an event queue by invoking one of the lookup method of EventQueues. For example,

EventQueues.lookup("myQueue"); //assumes the desktop scope
EventQueues.lookup("anotherQueue", EventQueues.SESSION);
EventQueues.lookup("anotherQueue", session);

Notice that if you want to locate an event queue in a working thread (rather than an event listener), you have to use EventQueues.lookup(String, Session) or EventQueues.lookup(String, Application), depending on your requirement.

The Scope of an Event Queue

There are currently four different scopes: desktop, group, session and application. In additions, you add your own scope, such as message queue to communicate among several servers.

Name API Description
desktop EventQueues.lookup(String, String)

EventQueues.lookup(String, String, boolean)

The event queue is visible only in the same desktop.
group EventQueues.lookup(String, String)

EventQueues.lookup(String, String, boolean)

[since 5.0.4][ZK EE]

The event queue is visible only in a group of desktops that belongs to the same browser. It is formed if iframe or frameset is used. Some portal container might cause a group of desktops to be formed too. Unlike the session and application scope, the group scope doesn't require the server push, so the communication is more efficient.

session EventQueues.lookup(String, String)

EventQueues.lookup(String, String, boolean)
EventQueues.lookup(String, Session)
EventQueues.lookup(String, Session, boolean)

The event queue is visible only in the same session. The server push will be enabled automatically if it subscribes a session-scoped event queue.

Notice that the server push is disabled automatically if the current desktop doesn't subscribe to any session- or application-scoped event queue. Also notice that the locating and creating of an event queue and publishing an event won't start the server push.

When a server push is enabled, a working thread is instantiated and started. It means this feature cannot be used in the environment that doesn't allow working threads, such Google App Engine.

application EventQueues.lookup(String, String)

EventQueues.lookup(String, String, boolean)
EventQueues.lookup(String, Application)
EventQueues.lookup(String, Application, boolean)

The event queue is visible only in the whole application. The server push will be enabled automatically.

Notice that the server push is disabled automatically if the current desktop doesn't subscribe to any session- or application-scoped event queue. Also notice that the locating and creating of an event queue and publishing an event won't start the server push.

When a server push is enabled, a working thread is instantiated and started. It means this feature cannot be used in the environment that doesn't allow working threads, such Google App Engine.

Differences:

desktop group session application
visibility desktop group session application
publish only in an event listener, or the current execution is available. only in an event listener, or the current execution is available. no limitation no limitation
subscribe only in an event listener, or the current execution is available. only in an event listener, or the current execution is available. only in an event listener, or the current execution is available. only in an event listener, or the current execution is available.
multi-thread Not used Not used Used (transparent) Used (transparent)
server-push Not used Not used Used (transparent) Used (transparent)

Publish and Subscribe

Publish an Event

To publish, just invoke one of the publish methods of EventQueue (returned by lookup). For example,

EventQueues.lookup("my super queue", EventQueues.APPLICATION, true)
  .publish(new Event("onSomethingHapping", null, new SomeAdditionInfo()));

The message used to communicate among publishers and subscribes are the event instances, so you can use any event you prefer.

Subscribe to an Event Queue

The event being published will be sent to each subscriber by calling the event listener the subscriber subscribe. To subscribe, just invoke one of the subscribe methods of EventQueue (returned by lookup). For example,

EventQueues.lookup("my super queue", EventQueues.APPLICATION, true).subscribe(
  new EventListener() {
    public void onEvent(Event evt) {
       //handle the event just like any other event listener
    }
  });

The event listener is invoked just like a normal event. You can manipulate the component or do whatever as you want.

An Example: Chat

Extend Event Queue: Add a Custom Scope

The location and creation of an event queue is actually done by a so-called event queue provider. An event-queue provider must implement the EventQueueProvider interface.

To customize it, just provide an implementation, and then specify the class name in the property called org.zkoss.zk.ui.event.EventQueueProvider.class.

For example, let us say we want to introduce the JMS scope, then we can implement as follows (only pseudo-code):

public class MyEventQueueProvider extends org.zkoss.zk.ui.event.impl.EventQueueProviderImpl {
  public EventQueue lookup(String name, String scope, boolean autocreate) {
    if ("jms".equals(scope)) {
      //create an event queue based on JMS's name
    } else
      return super.lookup(name, scope, autocreate);
  }
  public boolean remove(String name, String scope) {
    if ("jms".equals(scope)) {
      //remove the event queue based on JMS's name
    } else
      return super.removename, scope);
  }
}

Then, specify the property in WEB-INF/zk.xml

<library-property>
	<name>org.zkoss.zk.ui.event.EventQueueProvider.class</name>
	<value>MyEventQueueProvider</value>
</library-property>

Version History

Version Date Content
5.0.4 August 2010 The group scope was introduced to allow the communication among inline frames without Server Push (minimizing the network bandwidth consumption).



Last Update : 2010/08/25

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.