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