zkoss 的 线程 以及 push
来源:互联网 发布:彩票万能计算器软件 编辑:程序博客网 时间:2024/05/16 10:38
package de.valuegrids;
import org.zkoss.lang.Threads;
public class ServerPushText {
public static void start()
throws InterruptedException {
}
public static void stop() throws InterruptedException {
}
private static class WorkingThread extends Thread {
private WorkingThread() {
}
public void run() {
try {
while (true) {
Threads.sleep(1000);
}
} catch (Exception ex) {
System.out.println("The server push thread interrupted");
}
}
public void init() {
}
}
}
服务器推动
服务器推动即所谓的反向Ajax(reverse-Ajax),允许服务器将内容动态的发至客户端。
通过使用服务器推动技术,当你预先定义的条件满足时,则可以在工作线程内将内容发
至客户端或更新客户端的内容。使用服务器推动很简单,仅需要如下的三步,
1. 使用 Desktop.enableServerPush(boolean bool)为桌面调用启用
服务器推动。
2. 将需要更新的组传递至工作线程。
3. 在桌面内调用工作线程。
[注]:你需要安装zkex.jar 或zkmax.jar 来使用服务器推动,除非你有自己
org.zkoss.zk.ui.sys.ServerPush 的实现。
现在让我们来看一个实际的例子。若你想使用服务器推动更新客户端的数字,首先要为
桌面启用服务器推动,然后调用线程,如下。
<window title="Demo of Server Push">
<zscript>
import test.WorkingThread;
WorkingThread thread;
void startServerpush(){
//enable server push
desktop.enableServerPush(true);
//invoke working thread and passing required component as
parameter
thread= new WorkingThread(info);
thread.start();
}
void stopServerpush(){
//stop the thread
thread.setDone();
//disable server push
desktop.enableServerPush(false);
}
</zscript>
<vbox>
<button label="Start Server Push"
onClick="startServerpush()"/>
<button label="Stop Server Push"
onClick="stopServerpush()"/>
<label id="info"/>
</vbox>
</window>
例子:
package de.valuegrids;
import org.zkoss.lang.Threads;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
public class ServerPushText {
static WorkingThread thread;
public static void start(Component infotext)throws InterruptedException {
// create desktop for the form which uses this bean.
final Desktop desktop = Executions.getCurrent().getDesktop();
if (desktop.isServerPushEnabled()) {
Messagebox.show("Already started");
} else {
//enable server push
desktop.enableServerPush(true);
//invoke working thread and passing required component as parameter
thread= new WorkingThread(infotext);
thread.start();
}
}
public static void stop() throws InterruptedException {
// create desktop for the form which uses this bean.
final Desktop desktop = Executions.getCurrent().getDesktop();
if (desktop.isServerPushEnabled()) {
//stop the thread
//thread.setDone();?
//disable server push
desktop.enableServerPush(false);
} else {
Messagebox.show("Already stopped");
}
}
private static class WorkingThread extends Thread {
Desktop desk;
Component infotext;
int counter;
private WorkingThread(Component infotext) {
this.desk = infotext.getDesktop();
this.infotext = infotext;
}
public void run() {
try {
while (true) {
// if exception, then stop push.
if (this.infotext.getDesktop() == null || !this.desk.isServerPushEnabled()) {
this.desk.enableServerPush(false);
return;
}
// here to activate desktop
Executions.activate(this.desk);
try {
((Label) infotext).setValue(String.valueOf(this.counter+1));
} finally {
// here to deactivate desktop
// deactivate the desktop, in order to avoid the safety issues
Executions.deactivate(this.desk);
}
Threads.sleep(1000);
}
} catch (Exception ex) {
System.out.println("The server push thread interrupted");
}
}
}
}
- zkoss 的 线程 以及 push
- Zkoss Push V2
- ZKoss树的一个例子
- ZKoss表格的一个例子
- zkoss显示各种类型的值
- 最近接触ZKoss的学习感受
- struts2+zkoss框架中,zk表格排序的实现
- Zkoss Sessions
- Git push 出错以及server端没有显示push后的文件的解决方法
- Git push 出错以及server端没有显示push后的文件的解决方法
- Git push 出错以及server端没有显示push后的文件的解决方法
- 基于dwr2.0的Push推送技术详解以及实例
- 线程以及一些线程相关的事物
- 线程的生命周期以及控制线程
- 线程的生命周期以及控制线程
- Java线程的使用以及中断线程
- 线程的阻塞以及优化
- git push 原因以及问题!
- 100个最佳Linux站点(三):相关新闻
- 集群和分布式
- 英语考试是中国人的毒瘤
- Windows XP Embedded (XPE)开发工具、升级包、中文包等微软官方下载地址
- 还是用回fedora11…………
- zkoss 的 线程 以及 push
- 关闭linux下pc喇叭的嘟嘟声音(system beep)
- 2010.3.11 晴天
- 毕业设计开始了!
- Eucalyptus弹性计算云平台配置文件和xen配置文件(centos)
- 高性能程序设计(性能优化参考点)
- lucene 简介
- 2010-3-11 linux 读写过程(1)
- 配置 yum 为 Fedora 11 快速更新软件