Asynchronous Tasks"

From Documentation
Line 1: Line 1:
 
{{ZKDevelopersReferencePageHeader}}
 
{{ZKDevelopersReferencePageHeader}}
  
If the task of updating UI can be represented as a method, the push can be done easily. All you need to do is
+
If you run an application logic in a task thread (not in a servlet thread), and you don't want to update UI in the same thread. All you need to do is:
#Implement the UI updates in an event listener (implementing <javadoc type="interface">org.zkoss.zk.ui.event.EventListener</javadoc> or <javadoc type="interface">org.zkoss.zk.ui.event.SerializableEventListener</javadoc>).
+
 
#Then, schedule it for executed asynchronously by the use of <javadoc method="schedule(org.zkoss.zk.ui.Desktop, org.zkoss.zk.ui.event.EventListener, org.zkoss.zk.ui.event.Event)">org.zkoss.zk.ui.Executions</javadoc>.
+
# enable server push
 +
# Implement the UI updates in an event listener (implement <javadoc type="interface">org.zkoss.zk.ui.event.EventListener</javadoc> or <javadoc type="interface">org.zkoss.zk.ui.event.SerializableEventListener</javadoc>).
 +
# Execute the listener asynchronously by <javadoc method="schedule(org.zkoss.zk.ui.Desktop, org.zkoss.zk.ui.event.EventListener, org.zkoss.zk.ui.event.Event)">org.zkoss.zk.ui.Executions</javadoc>.
  
 
Here is the pseudo code:
 
Here is the pseudo code:
Line 14: Line 16:
 
         }, event);
 
         }, event);
 
</source>
 
</source>
 +
* Line 2: You can manipulate UI whatever you want in <javadoc type="interface" method="onEvent(org.zkoss.zk.ui.Event)">org.zkoss.zk.ui.event.EventListener</javadoc>. It is no different from any other event listener.
  
You could manipulate UI whatever you want in <javadoc type="interface" method="onEvent(org.zkoss.zk.ui.Event)">org.zkoss.zk.ui.event.EventListener</javadoc>. It is no different from any other event listener.
+
Notice that <javadoc method="schedule(org.zkoss.zk.ui.Desktop, org.zkoss.zk.ui.event.EventListener, org.zkoss.zk.ui.event.Event)">org.zkoss.zk.ui.Executions</javadoc> can be called anywhere, including another event listener or a task thread. In other words, you don't have to fork a new thread to use this feature.
  
Notice that <javadoc method="schedule(org.zkoss.zk.ui.Desktop, org.zkoss.zk.ui.event.EventListener, org.zkoss.zk.ui.event.Event)">org.zkoss.zk.ui.Executions</javadoc> can be called anywhere, including another event listener or a working thread. In other words, you don't have to fork a working thread to use this feature.
+
Notice that, since there is at most one thread to access the UI of a given desktop, the event listener must NOT be time-consuming. Otherwise, it will block other event listeners from execution. Thus, if you have a long operation to do, you could use [[ZK_Developer's_Reference/Event_Handling/Event_Queues#Asynchronous_Event_Listener|event queue's asynchronous event listener]], or implement it as [[ZK Developer's Reference/Server Push/Synchronous Tasks|a synchronous task]] and handle lengthy operation outside of the activation block.
  
Notice that, since there is at most one thread to access the UI of a given desktop, the event listener's performance must be good. Otherwise, it will block other event listeners from execution. Thus, if you have a long operation to do, you could use [[ZK_Developer's_Reference/Event_Handling/Event_Queues#Asynchronous_Event_Listener|event queue's asynchronous event listener]], or implement it as [[ZK Developer's Reference/Server Push/Synchronous Tasks|a synchronous task]] and handle lengthy operation outside of the activation block.
 
  
 
=Version History=
 
=Version History=

Revision as of 08:42, 28 June 2019


Asynchronous Tasks


If you run an application logic in a task thread (not in a servlet thread), and you don't want to update UI in the same thread. All you need to do is:

  1. enable server push
  2. Implement the UI updates in an event listener (implement EventListener or SerializableEventListener).
  3. Execute the listener asynchronously by Executions.schedule(Desktop, EventListener, Event).

Here is the pseudo code:

    Executions.schedule(desktop,
        new EventListener() {
            public void onEvent(Event event) {
                updateUI();
            }
        }, event);

Notice that Executions.schedule(Desktop, EventListener, Event) can be called anywhere, including another event listener or a task thread. In other words, you don't have to fork a new thread to use this feature.

Notice that, since there is at most one thread to access the UI of a given desktop, the event listener must NOT be time-consuming. Otherwise, it will block other event listeners from execution. Thus, if you have a long operation to do, you could use event queue's asynchronous event listener, or implement it as a synchronous task and handle lengthy operation outside of the activation block.


Version History

Last Update : 2019/06/28


Version Date Content
5.0.6 November 2010 This feature was introduced. With 5.0.5 or prior, you have to use Event Queues or Synchronous Tasks.



Last Update : 2019/06/28

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