Synchronous Tasks"
(Created page with '{{ZKDevelopersReferencePageHeader}} =Version History= {{LastUpdated}} {| border='1px' | width="100%" ! Version !! Date !! Content |- | | | |} {{ZKDeveloper…') |
|||
Line 1: | Line 1: | ||
{{ZKDevelopersReferencePageHeader}} | {{ZKDevelopersReferencePageHeader}} | ||
+ | |||
+ | Server push is a technology to actively ''push'' data to the client. For ZK, the data is usually UI updates or its variants. Thus, for sake of understanding, we could consider the task is about updating UI concurrently with regular Ajax requests (poll-type request). For example, in a chat application, we have to update UI when the people you talk to have entered a message. Since the entering of a message takes place in another session, the server has to ''push'' to all clients that involve in the conversation. | ||
+ | |||
+ | If the task of updating UI takes place in a working thread, it generally more convenient to execute it synchronously. On the other hand, if the task can be encapsulated as an event listener (<javadoc type="interface">org.zkoss.zk.ui.event.EventListener</javadoc>), you could refer to the [[ZK Developer's Reference/Server Push/Asynchronous Tasks|Asynchronous Tasks]] section. | ||
+ | |||
+ | =Enable Server Push= | ||
+ | |||
+ | =How to Update UI in a Working Thread= | ||
+ | To updating the UI synchronously in a working thread, we have to do as follows. | ||
+ | |||
+ | #Invoke <javadoc method="activate(org.zkoss.zk.ui.Desktop)">org.zkoss.zk.ui.Executions</javadoc>. It has two purposes: | ||
+ | ##It grants the right to access the UI of the given desktop to the thread<ref>Notice that, for each desktop, there is at most one thread is allowed to access.</ref>. | ||
+ | ##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 | ||
+ | #Update UI any way you want, just like any regular event listener. | ||
+ | #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: | ||
+ | |||
+ | <source lang="java"> | ||
+ | public class WorkingThread extends Thread { | ||
+ | public void run() { | ||
+ | try { | ||
+ | while (anyDataToShow()) { //whatever you like | ||
+ | //Step 1. Prepare the data that will be updated to UI | ||
+ | collectData(); //whatever you like | ||
+ | |||
+ | //Step 2. Activate to grant the access of the give desktop | ||
+ | Executions.activate(desktop); | ||
+ | try { | ||
+ | //Step 3. Update UI | ||
+ | updateUI(); //whatever you like | ||
+ | } finally { | ||
+ | //Step 4. Deactivate to return the control of UI back | ||
+ | Executions.deactivate(_desktop); | ||
+ | } | ||
+ | } | ||
+ | } catch (InterruptedException ex) { | ||
+ | //Interrupted. You might want to handle it | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | <blockquote> | ||
+ | ---- | ||
+ | <references/> | ||
+ | </blockquote> | ||
=Version History= | =Version History= |
Revision as of 11:40, 22 November 2010
Server push is a technology to actively push data to the client. For ZK, the data is usually UI updates or its variants. Thus, for sake of understanding, we could consider the task is about updating UI concurrently with regular Ajax requests (poll-type request). For example, in a chat application, we have to update UI when the people you talk to have entered a message. Since the entering of a message takes place in another session, the server has to push to all clients that involve in the conversation.
If the task of updating UI takes place in a working thread, it generally more convenient to execute it synchronously. On the other hand, if the task can be encapsulated as an event listener (EventListener), you could refer to the Asynchronous Tasks section.
Enable Server Push
How to Update UI in a Working Thread
To updating the UI synchronously in a working thread, we have to do as follows.
- Invoke Executions.activate(Desktop). It has two purposes:
- It grants the right to access the UI of the given desktop to the thread[1].
- 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
- Update UI any way you want, just like any regular event listener.
- 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:
public class WorkingThread extends Thread {
public void run() {
try {
while (anyDataToShow()) { //whatever you like
//Step 1. Prepare the data that will be updated to UI
collectData(); //whatever you like
//Step 2. Activate to grant the access of the give desktop
Executions.activate(desktop);
try {
//Step 3. Update UI
updateUI(); //whatever you like
} 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, for each desktop, there is at most one thread is allowed to access.
Version History
Version | Date | Content |
---|---|---|