Class PollingServerPush
- java.lang.Object
-
- org.zkoss.zk.ui.impl.PollingServerPush
-
- All Implemented Interfaces:
ServerPush
public class PollingServerPush extends java.lang.Object implements ServerPush
A server-push implementation that is based on client-polling.Developer can control the frequency of the polling by setting the preferences as follows:
PollingServerPush.delay.min- The minimal delay to send the second polling request (unit: milliseconds). Default: 1000.
PollingServerPush.delay.max- The maximal delay to send the second polling request (unit: milliseconds). Default: 15000.
PollingServerPush.delay.factor- the delay factor. The real delay is the processing time multiplies the delay
factor. For example, if the last request
took 1 second to process, then the client polling will be delayed
for
1 x factorseconds. Default: 5. The larger the factor is, the longer delay it tends to be.
Another way to control the frequency is to instantiate an instance with
PollingServerPush(int, int, int), and then assign it withDesktopCtrl.enableServerPush(ServerPush).desktop.enableServerPush(new PollingServerPush(1000, 5000, -1));- Since:
- 5.0.0
- Author:
- tomyeh
-
-
Constructor Summary
Constructors Constructor Description PollingServerPush()PollingServerPush(int min, int max, int factor)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanactivate(long timeout)Activate the current thread (which must be a server-push thread).booleandeactivate(boolean stop)Deactivates the current thread (which must be a server-push thread).protected java.lang.StringgetStartScript()Returns the JavaScript codes to enable (a.k.a., start) the server push.protected java.lang.StringgetStopScript()Returns the JavaScript codes to disable (a.k.a., stop) the server push.booleanisActive()Returns if the working thread of the server push is active.voidonPiggyback()Called whenEvents.ON_PIGGYBACKis received.voidresume()ZK-1777 resume serverpush after DesktopRecycling<T extends Event>
voidschedule(EventListener<T> listener, T event, Scheduler<T> scheduler)Called by the associated desktop to schedule a task to execute asynchronously.voidstart(Desktop desktop)Starts and initializes the server-push controller.protected voidstartClientPush()Sends an AU response to the client to start the server push.voidstop()Stops and cleans up the server-push controller.protected voidstopClientPush()Sends an AU response the client to stop the server push.
-
-
-
Constructor Detail
-
PollingServerPush
public PollingServerPush()
-
PollingServerPush
public PollingServerPush(int min, int max, int factor)- Parameters:
min- the minimal delay before sending the second polling request (unit: milliseconds). If negative, the default is used (seePollingServerPush).max- the maximal delay before sending the second polling request (unit: milliseconds). If negative, the default is used (seePollingServerPush).factor- the delay factor. If negative, the default is used (seePollingServerPush).- Since:
- 5.0.4
-
-
Method Detail
-
startClientPush
protected void startClientPush()
Sends an AU response to the client to start the server push. It is called bystart(org.zkoss.zk.ui.Desktop).The derived class usually overrides this method to support different devices, such as ZK Mobile.
The default implementation is to send
AuScriptcontaining the script returned bygetStartScript(). Devices that don't support scripts could override this method to send a custom AU response (AuResponse).
-
stopClientPush
protected void stopClientPush()
Sends an AU response the client to stop the server push.The derived class usually overrides this method to support different devices, such as ZK Mobile.
The default implementation is to send
AuScriptcontaining the script returned bygetStopScript(). Devices that don't support scripts could override this method to send a custom AU response (AuResponse).
-
getStartScript
protected java.lang.String getStartScript()
Returns the JavaScript codes to enable (a.k.a., start) the server push. It is called bystartClientPush()to prepare the script ofAuScriptthat will be sent to the client.
-
getStopScript
protected java.lang.String getStopScript()
Returns the JavaScript codes to disable (a.k.a., stop) the server push. It is called bystopClientPush()to prepare the script ofAuScriptthat will be sent to the client.
-
isActive
public boolean isActive()
Description copied from interface:ServerPushReturns if the working thread of the server push is active. In other words, it is true if it is betweenExecutions.activate(org.zkoss.zk.ui.Desktop)andExecutions.deactivate(org.zkoss.zk.ui.Desktop).- Specified by:
isActivein interfaceServerPush
-
start
public void start(Desktop desktop)
Description copied from interface:ServerPushStarts and initializes the server-push controller. One server-push controller is associated with exactly one desktop.ServerPush.start(org.zkoss.zk.ui.Desktop)is called whenDesktop.enableServerPush(boolean)is called to enable the server-push feature for the specified desktop.- Specified by:
startin interfaceServerPush
-
resume
public void resume()
ZK-1777 resume serverpush after DesktopRecycling- Specified by:
resumein interfaceServerPush
-
stop
public void stop()
Description copied from interface:ServerPushStops and cleans up the server-push controller.ServerPush.stop()is called whenDesktop.enableServerPush(boolean)is called to disable the server-push feature, or when the desktop is being removed.- Specified by:
stopin interfaceServerPush
-
onPiggyback
public void onPiggyback()
Description copied from interface:ServerPushCalled whenEvents.ON_PIGGYBACKis received. The invocation is passive (i.e., triggered by other events, rather than spontaneous).This method is called in the context of an event listener. In other words, the execution is activated and you can retrieve it by
Executions.getCurrent().- Specified by:
onPiggybackin interfaceServerPush
-
schedule
public <T extends Event> void schedule(EventListener<T> listener, T event, Scheduler<T> scheduler)
Description copied from interface:ServerPushCalled by the associated desktop to schedule a task to execute asynchronously.The implementation usually delegates the scheduling to the scheduler passed as the third argument, which is controlled by the desktop. Of course, it could schedule it by itself.
- Specified by:
schedulein interfaceServerPush- Parameters:
listener- the task to executeevent- the event to be passed to the task (i.e., the event listener). It could null or any instance as long as the task recognizes it.scheduler- the default scheduler to schedule the task. The implementation usually delegates the scheduling back to it. If you prefer to handle it by yourself, you could ignore it.
-
activate
public boolean activate(long timeout) throws java.lang.InterruptedException, DesktopUnavailableExceptionDescription copied from interface:ServerPushActivate the current thread (which must be a server-push thread). It causes the current thread to wait until the desktop is available to access, the desktop no longer exists, some other thread interrupts this thread, or a certain amount of real time has elapsed.The invoker of this method must invoke
ServerPush.deactivate(boolean)in this finally clause.Note: the activation is applied to the desktop that was assigned by
ServerPush.start(org.zkoss.zk.ui.Desktop).Unlike
ServerPush.onPiggyback(), this method is NOT called in the context of an event listener. Rather, it is called in the thread of a server-push thread.- Specified by:
activatein interfaceServerPush- Parameters:
timeout- the maximum time to wait in milliseconds. Ignored (i.e., never timeout) if non-positive.- Returns:
- whether it is activated or it is timeout. The only reason it returns false is timeout.
- Throws:
java.lang.InterruptedException- if it is interrupted by other threadDesktopUnavailableException- if the desktop is removed (when activating).
-
deactivate
public boolean deactivate(boolean stop)
Description copied from interface:ServerPushDeactivates the current thread (which must be a server-push thread).- Specified by:
deactivatein interfaceServerPush- Parameters:
stop- whether to stop after deactivate- Returns:
- true if it is stopped
- See Also:
ServerPush.activate(long)
-
-