Event Threads"

From Documentation
Line 3: Line 3:
 
__TOC__
 
__TOC__
  
By default, ZK processes an event in an independent thread called the event processing thread. Thus, the developer can suspend and resume the execution at any time, without blocking the servlet thread from sending back the responses to the browser.
+
By default, ZK processes an event in the same Servlet thread that receives the HTTP request. It is the suggested approach because it is efficient and easy to integrate with other frameworks<ref>Many frameworks store per-request information in the thread-local storage.</ref>.
  
However, it consumes more memory, especially if there are a lot suspended threads, and it may cause some challenge to integrate with other systems that storing information at the Servlet thread's local storage.
+
However, it also implies the developer cannot suspend the execution. Otherwise, the end user won't see any update 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<ref> For ZK 1.x, 2.x and 3.x, the event processing thread is enabled by default.</ref>, you have to specify the following in <tt>WEB-INF/zk.xml</tt>.
 
 
ZK provides an option to let you disable the use of the event processing threads. In other words, you can force ZK to process events all in the Servlet threads like other conventional frameworks. Of course, you cannot suspend the execution if the Servlet thread is used.
 
 
 
To disable the use of the event processing threads, you have to specify the following content in <tt>WEB-INF/zk.xml</tt>.
 
  
 
<source lang="xml" >
 
<source lang="xml" >
 
<system-config>
 
<system-config>
     <disable-event-thread/>
+
     <disable-event-thread>false</disable-event-thread>
 
</system-config>
 
</system-config>
 
</source>
 
</source>
Line 42: Line 38:
  
 
|}
 
|}
 +
 +
<blockquote>
 +
----
 +
<references/>
 +
</blockquote>
 +
 
=== Modal Windows ===
 
=== Modal Windows ===
 
You can not use the modal window anymore. You can create the same visual effect with the highlighted mode. However, at the server side, it works just like the overlapped mode – it returns immediately without waiting for user's response.
 
You can not use the modal window anymore. You can create the same visual effect with the highlighted mode. However, at the server side, it works just like the overlapped mode – it returns immediately without waiting for user's response.

Revision as of 03:45, 3 September 2010

Stop.png This documentation is for an older version of ZK. For the latest one, please click here.


By default, ZK processes an event in the same Servlet thread that receives the HTTP request. It is the suggested approach because it is efficient and 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 update 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.

<system-config>
    <disable-event-thread>false</disable-event-thread>
</system-config>

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
Integration Less integration issues.

Many containers assume the HTTP request is handled in the servlet thread.

You may have to implement EventThreadInit and/or EventThreadCleanup to solve the integration issue.

ZK and the community keep providing versatile implementations to solve the integration issue.

SuspendResume No way to suspend the execution of the event listener.

For example, you cannot create a modal window.

No limitation at all.

  1. Many frameworks store per-request information in the thread-local storage.
  2. For ZK 1.x, 2.x and 3.x, the event processing thread is enabled by default.

Modal Windows

You can not use the modal window anymore. You can create the same visual effect with the highlighted mode. However, at the server side, it works just like the overlapped mode – it returns immediately without waiting for user's response.

 win.doHighlighted(); //returns once the mode is changed; not suspended

Message Boxes

The message boxes returns immediately so it always returns Messagebox.OK. Thus, it is meaningless to show buttons other than the OK button. For example, the if clause in the following example is never true.

 if (Messagebox.show("Delete?", "Prompt", Messagebox.YES|Messagebox.NO,
     Messagebox.QUESTION) == Messagebox.YES) {
     this_never_executes();
 }

Rather, you have to provide an event listener as follows.

 Messagebox.show("Delete?", "Prompt", Messagebox.YES|Messagebox.NO,
     Messagebox.QUESTION,
     new EventListener() {
         public void onEvent(Event evt) {
             switch (((Integer)evt.getData()).intValue()) {
             case Messagebox.YES: doYes(); break; //the Yes button is pressed
             case Messagebox.NO: doNo(); break; //the No button is pressed
             }
         }
     }
 );

The event listener you provided is invoked when the user clicks one of the buttons. Then, you can identify which button is clicked by examining the data (Event's getData). The data is an integer whose value is the button's identifier, such as Messagebox.YES.

Alternatively, you can examine the event name:

 public void onEvent(Event evt) {
     if ("onYes".equals(evt.getName())) {
         doYes(); //the Yes button is pressed
     } else if ("onNo".equals(evt.getName())) {
         doNo(); //the No button is pressed
     }
 }

Note: The event name for the OK button is onOK, not onOk.

File Upload

The file upload dialog is no longer applicable. Rather, you shall use Button or Toolbarbutton with upload="true" instead. For example,

<zk>
	<zscript>
	void upload(Event event) {
		org.zkoss.util.media.Media media = event.getMedia();
		if (media instanceof org.zkoss.image.Image) {
			org.zkoss.zul.Image image = new org.zkoss.zul.Image();
			image.setContent(media);
			image.setParent(pics);
		} else {
			Messagebox.show("Not an image: "+media, "Error", Messagebox.OK, Messagebox.ERROR);
			break; //not to show too many errors
		}
	}
	</zscript>
	<button label="Upload" upload="true" onUpload="upload(event)"/>
	<toolbarbutton label="Upload" upload="true" onUpload="upload(event)"/>
	<vbox id="pics" />
</zk>

If you prefer to use a dialog (Fileupload.get()), please take a look at ZK Component Reference: Fileupload for more inormation.




Last Update : 2010/09/03

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