Subscribe to EventQueues"

From Documentation
Line 39: Line 39:
 
Available scopes are: Desktop, Group, Session, Application. Note that Group scope requires ZK EE. See also <javadoc>org.zkoss.zk.ui.event.EventQueues</javadoc>.
 
Available scopes are: Desktop, Group, Session, Application. Note that Group scope requires ZK EE. See also <javadoc>org.zkoss.zk.ui.event.EventQueues</javadoc>.
  
 +
&nbsp;
 +
==Event Name==
 +
Since 7.0.3, you can also specify the event name to listen.
 +
<source lang="java">
 +
@Subscribe(value = "queue2", eventName = "event1")
 +
public void method2(Event event) {
 +
// this method will be called when EventQueue "queue2" of Session scope is published
 +
}
 +
public void publish() {
 +
EventQueue<Event> eq = EventQueues.lookup("queue2", EventQueues.DESKTOP, true);
 +
eq.publish(new Event("event1", component, data));
 +
}
 +
</source>
  
 
&nbsp;
 
&nbsp;
Line 50: Line 63:
 
}
 
}
 
</source>
 
</source>
 +
 +
Since 7.0.3, ZK will automatically map the event data into the method parameters in order.
 +
 +
<source lang="java">
 +
@Subscribe("queue3")
 +
public void method3(int i, String s) {
 +
// i will be 100, s will be "eventData"
 +
// ...
 +
}
 +
 +
public void publish() {
 +
EventQueue<Event> eq = EventQueues.lookup("queue3", EventQueues.DESKTOP, true);
 +
eq.publish(new Event("event1", component, new Object[]{100, "eventData"}));
 +
}
 +
</source>
 +
 +
If you put the event at the first one, it also works well.
 +
 +
<source lang="java">
 +
@Subscribe("queue3")
 +
public void method3(Event event, int i, String s) {
 +
// ...
 +
}
 +
</source>
 +
 +
so now, we have four situations to the parameter:
 +
 +
* method()
 +
* method(Event event)
 +
* method(Event event, int d1, String d2, ....)
 +
* method(int d1, String d2, ...)
  
 
=Version History=
 
=Version History=
Line 59: Line 103:
 
| April 2012
 
| April 2012
 
| @Subscribe was introduced.
 
| @Subscribe was introduced.
 +
|-
 +
| 7.0.3
 +
| June 2014
 +
| New feature: ZK-2076[http://tracker.zkoss.org/browse/ZK-2076]
 
|}
 
|}
  
 
{{ZKDevelopersReferencePageFooter}}
 
{{ZKDevelopersReferencePageFooter}}

Revision as of 02:12, 25 June 2014


Subscribe to EventQueues


Subscribe to EventQueues

  • Available for ZK:
  • http://www.zkoss.org/product/zkhttp://www.zkoss.org/whyzk/zkeeVersion ee.png

You can subscribe a method (as if in an EventListener) to an EventQueue by annotate it with Subscribe. For example,

@Subscribe("queue1")
public void method1(Event event) {
	// this method will be called when EventQueue "queue1" of Desktop scope is published
	Object data = event.getData();
	Component target = event.getTarget();
}
public void publish() {
	EventQueue<Event> eq = EventQueues.lookup("queue1", EventQueues.DESKTOP, true);
	eq.publish(new Event("onMyEvent", component, data));
}

In the example above, when you publish an event in the EventQueue, the subscribed method will be called. This is a useful mechanism to communicate with other composers. See also EventQueue.


 

EventQueue Scope

You can subscribe to EventQueue of different scope.

@Subscribe(value = "queue2", scope = EventQueues.SESSION)
public void method2(Event event) {
	// this method will be called when EventQueue "queue2" of Session scope is published
}
public void publish() {
	EventQueue<Event> eq = EventQueues.lookup("queue2", EventQueues.SESSION, true);
	eq.publish(new Event("onMyEvent", component, data));
}

Available scopes are: Desktop, Group, Session, Application. Note that Group scope requires ZK EE. See also EventQueues.

 

Event Name

Since 7.0.3, you can also specify the event name to listen.

@Subscribe(value = "queue2", eventName = "event1")
public void method2(Event event) {
	// this method will be called when EventQueue "queue2" of Session scope is published
}
public void publish() {
	EventQueue<Event> eq = EventQueues.lookup("queue2", EventQueues.DESKTOP, true);
	eq.publish(new Event("event1", component, data));
}

 

Subscriber Method Parameter

The method which subscribes to the EventQueue takes either no parameter, or one parameter of type Event.

@Subscribe("queue3")
public void method3() { // the event parameter can be omitted
	// ...
}

Since 7.0.3, ZK will automatically map the event data into the method parameters in order.

@Subscribe("queue3")
public void method3(int i, String s) { 
	// i will be 100, s will be "eventData"
	// ...
}

public void publish() {
	EventQueue<Event> eq = EventQueues.lookup("queue3", EventQueues.DESKTOP, true);
	eq.publish(new Event("event1", component, new Object[]{100, "eventData"}));
}

If you put the event at the first one, it also works well.

@Subscribe("queue3")
public void method3(Event event, int i, String s) { 
	// ...
}

so now, we have four situations to the parameter:

  • method()
  • method(Event event)
  • method(Event event, int d1, String d2, ....)
  • method(int d1, String d2, ...)

Version History

Last Update : 2014/06/25


Version Date Content
6.0.1 April 2012 @Subscribe was introduced.
7.0.3 June 2014 New feature: ZK-2076[1]



Last Update : 2014/06/25

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