zkoss 的 线程 以及 push

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");
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值