Synchronous Tasks"

From Documentation
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{ZKDevelopersReferencePageHeader}}
 
{{ZKDevelopersReferencePageHeader}}
 +
 +
 +
__TOC__
 +
  
 
Server push is a technology to actively ''push'' data to the client. For ZK, the data is usually the UI updates or its variants. Thus, for sake of understanding, we could consider the task is about updating UI in parallel with regular Ajax requests (poll-type request). For example, in a chat application, once a message is entered by a participant, the server has to ''push'' it to all clients that involve in the conversation.
 
Server push is a technology to actively ''push'' data to the client. For ZK, the data is usually the UI updates or its variants. Thus, for sake of understanding, we could consider the task is about updating UI in parallel with regular Ajax requests (poll-type request). For example, in a chat application, once a message is entered by a participant, the server has to ''push'' it to all clients that involve in the conversation.
Line 31: Line 35:
 
#Invoke <javadoc method="deactivate(org.zkoss.zk.ui.Desktop)">org.zkoss.zk.ui.Executions</javadoc> to return the control, such that other thread could have a chance to update UI.
 
#Invoke <javadoc method="deactivate(org.zkoss.zk.ui.Desktop)">org.zkoss.zk.ui.Executions</javadoc> to return the control, such that other thread could have a chance to update UI.
  
Here is the pseudo code that illustrates the flow<ref>For a real example, please refer to [[Small Talks/2008/May/Server Push with a Stock Chart Example|Server Push with a Stock Chart Example]]:
+
Here is the pseudo code that illustrates the flow<ref>For a real example, please refer to small talks: [[Small Talks/2007/August/Simple and Intuitive Server Push with a Chat Room Example|Simple and Intuitive Server Push with a Chat Room Example]] and [[Small Talks/2008/May/Server Push with a Stock Chart Example|Server Push with a Stock Chart Example]].</ref>:
  
 
<source lang="java" high="9,15">
 
<source lang="java" high="9,15">
Line 37: Line 41:
 
     public void run() {
 
     public void run() {
 
         try {
 
         try {
             while (anyDataToShow()) { //whatever you like
+
             while (anyDataToShow()) {
 
                 //Step 1. Prepare the data that will be updated to UI
 
                 //Step 1. Prepare the data that will be updated to UI
                 collectData(); //whatever you like
+
                 collectData(); //prepare the data to set to components
  
                 //Step 2. Activate to grant the access of the give desktop
+
                 //Step 2. Activate to grant the access of the given desktop
 
                 Executions.activate(desktop);
 
                 Executions.activate(desktop);
 
                 try {
 
                 try {
 
                     //Step 3. Update UI
 
                     //Step 3. Update UI
                     updateUI(); //whatever you like
+
                     updateUI(); //impelment the logic to change UI
 
                 } finally {
 
                 } finally {
 
                     //Step 4. Deactivate to return the control of UI back
 
                     //Step 4. Deactivate to return the control of UI back
                     Executions.deactivate(_desktop);
+
                     Executions.deactivate(desktop);
 
                 }
 
                 }
 
             }
 
             }
Line 58: Line 62:
 
</source>
 
</source>
  
'''Notice''' that the task between <javadoc method="activate(org.zkoss.zk.ui.Desktop)">org.zkoss.zk.ui.Executions</javadoc> and <javadoc method="deactivate(org.zkoss.zk.ui.Desktop)">org.zkoss.zk.ui.Executions</javadoc> has to be efficient, since it blocks others, including the user (of the desktop), from access the UI. It is suggested to prepare the data before <javadoc method="activate(org.zkoss.zk.ui.Desktop)">org.zkoss.zk.ui.Executions</javadoc>, such that it can be done in parallel with other threads.
+
'''Notice''' that the task between <javadoc method="activate(org.zkoss.zk.ui.Desktop)">org.zkoss.zk.ui.Executions</javadoc> and <javadoc method="deactivate(org.zkoss.zk.ui.Desktop)">org.zkoss.zk.ui.Executions</javadoc> has to take less time, since it blocks others, including the user (of the desktop), from accessing the UI. It is suggested to prepare the data before <javadoc method="activate(org.zkoss.zk.ui.Desktop)">org.zkoss.zk.ui.Executions</javadoc>, such that it can be done in parallel with other threads.
  
 
<blockquote>
 
<blockquote>

Revision as of 01:29, 9 May 2019


Synchronous Tasks




Server push is a technology to actively push data to the client. For ZK, the data is usually the UI updates or its variants. Thus, for sake of understanding, we could consider the task is about updating UI in parallel with regular Ajax requests (poll-type request). For example, in a chat application, once a message is entered by a participant, the server has to push it to all clients that involve in the conversation.

If the task of updating UI takes place in a working thread, it is generally more convenient to execute it synchronously as described later. On the other hand, if the task can be encapsulated as an event listener (EventListener), you could execute it asynchronously -- please refer to the Asynchronous Tasks section for more information.

Enable Server Push

By default, the server push is disabled (for better performance). Before pushing data for a given desktop, you have to enable the server push for it.

It can be done by use of Desktop.enableServerPush(boolean):

desktop.enableServerPush(true);

After the server push of a given desktop is enabled, you could use any number of working thread to update the desktop concurrently as described in the following section[1].


  1. For better performance, it is suggested to disable the server push if it is no longer used in the give desktop.

Update UI in a Working Thread

To updating the UI synchronously in a working thread, we have to do as follows.

  1. Invoke Executions.activate(Desktop). It has two purposes:
    1. It grants the right to access the UI of the given desktop to the caller's thread[1].
    2. It establishes a connection with the client (the browser window displaying the desktop), such that the update will be sent to the client after finished
  2. Update UI any way you want, just like any regular event listener.
  3. Invoke Executions.deactivate(Desktop) to return the control, such that other thread could have a chance to update UI.

Here is the pseudo code that illustrates the flow[2]:

public class WorkingThread extends Thread {
    public void run() {
        try {
            while (anyDataToShow()) {
                //Step 1. Prepare the data that will be updated to UI
                collectData(); //prepare the data to set to components

                //Step 2. Activate to grant the access of the given desktop
                Executions.activate(desktop);
                try {
                     //Step 3. Update UI
                     updateUI(); //impelment the logic to change UI
                } finally {
                    //Step 4. Deactivate to return the control of UI back
                     Executions.deactivate(desktop);
                }
            }
        } catch (InterruptedException ex) {
            //Interrupted. You might want to handle it
        }
    }
}

Notice that the task between Executions.activate(Desktop) and Executions.deactivate(Desktop) has to take less time, since it blocks others, including the user (of the desktop), from accessing the UI. It is suggested to prepare the data before Executions.activate(Desktop), such that it can be done in parallel with other threads.


  1. Notice that, for each desktop, there is at most one thread is allowed to access at the same time.
  2. For a real example, please refer to small talks: Simple and Intuitive Server Push with a Chat Room Example and Server Push with a Stock Chart Example.

Version History

Last Update : 2019/05/09


Version Date Content
     



Last Update : 2019/05/09

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