zk event thread model

来源:互联网 发布:淘宝上的上海大药房 编辑:程序博客网 时间:2024/05/02 05:04

原文地址:http://books.zkoss.org/wiki/ZK_Configuration_Reference/zk.xml/The_system-config_Element/The_disable-event-thread_Element


使用到zk的 自定义事件功能。发现sendEvent.postEvent 两种方式。不过postEvent 只是通知,不能对流程实现控制。因此,不符合要求,而sendEvent方式


需要event process 也就是说,不是在任何时刻都可以触发的。看了下文档,原来如此。。。。


ps 我的zk是3.6.3的。

=========================================================



By default, ZK processes an event in the same Servlet thread that receives the HTTP request. It is the suggested approach because the performance is better and it is easy to integrate with other frameworks[1].

However, it also implies the developer cannot suspend the execution. Otherwise, the end user won't see any updates from the server. To solve it, ZK provides an alternative approach: processes the event in an independent thread called the event processing thread. Therefore, the developer can suspend and resume the execution at any time, without blocking the Servlet thread from sending back the responses to the browser. To turn it on[2], you have to specify the following in WEB-INF/zk.xml (ZK Configuration Guide: disable-event-thread).


1
2
3
<system-config>
    <disable-event-thread>false</disable-event-thread>
</system-config>

In short, it is recommended to disable the event thread. Enable the event thread only if the project does not need to integrate other frameworks (such as Spring), depends on Messagebox and modal windows a lot, and does not have a lot of concurrent users.

Here is the advantages and limitations about using the Servlet thread to process events. In the following sections we will talk more about the limitations and workarounds when using the Servlet thread.

 
Using Servlet Thread
Using Event Processing Thread
IntegrationLess integration issues.

Many containers assume the HTTP request is handled in the Servlet thread, and many frameworks store per-request information in the thread local storage.

You may have to implement EventThreadInit and/orEventThreadCleanup to solve the integration issue, such as copying the per-request information from the Servlet thread to the event processing thread.

Threre are several implementations to solve the integration issue, such asHibernateSessionContextListener (they can be found under the org.zkoss.zkplus package).

SuspendResumeNo way to suspend the execution of the event listener.

For example, you cannot create a modal window.

No limitation at all.PerformanceNo extra costIt executes a bit slower to switch from one thread to another, and it might consume a lot more memory if there are a lot of suspended event processing threads.


Subsections:

Modal Windows
Message Box
File Upload



  1. ↑ Many frameworks store per-request information in the thread-local storage, so we have to copy them from Servlet thread to the Event Processing Thread.
  2. ↑ For ZK 1.x, 2.x and 3.x, the event processing thread is enabled by default.



==================================================================================================================

Deferrable EventListeners

Bydefault, events are sent the server when it is fired at the client.However, many event listeners are just used to maintain the status atthe server, rather than providing visual response to the user. Inother words, the events for these listeners have no need to be sentimmediately. Rather, they shall be sent at once to minimize the traffic between theclient and the server, and then to improve the server's performance.For the sake of the description convenience, we call them thedeferrable event listeners.

Tomake an event listener deferrable, you have to implement theorg.zkoss.zk.ui.event.Deferrableinterface (withEventListener)and return true for theisDeferrablemethod as follows.

public classDeferrableListener implements EventListener, Deferrable {private boolean _modified;public void onEvent(Eventevent) {_modified = true;}public booleanisDeferrable() {return true;}}

Whenan event is fired at the client (e.g., the user selects a list item),ZK won't send the event if no event listener is registered for it oronly deferrable listeners are registered. instead, the event isqueued at the client.

Onthe hand, if at least one non-deferrable listener is registered, theevent are sent immediately with all queued events to the server at once. No event is lost and the arriving order is preserved.


很多时候这里的 at once 翻译为 立刻马上。这个和 Deferrable Event 的本身的内涵违背了。这里应该翻译为 一次性 更好一点。后文也有解释,当此类事件触发的时候,zk并不会立刻向服务器发送事件,二是放在一个队列中,当遇到一个非Deferrable 的Event的时候,一起捎带过去。当然这里肯定会先执行Deferrable 的Event。(No event is lost and the arriving order is preserved.