⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverpush.java

📁 ZK是一个Ajax Java Web框架
💻 JAVA
字号:
/* ServerPushWindow.java{{IS_NOTE	Purpose:			Description:			History:		Mon Aug  6 12:37:13     2007, Created by tomyeh}}IS_NOTECopyright (C) 2007 Potix Corporation. All Rights Reserved.{{IS_RIGHT	This program is distributed under GPL Version 2.0 in the hope that	it will be useful, but WITHOUT ANY WARRANTY.}}IS_RIGHT*/package org.zkoss.zkdemo.test2;import org.zkoss.lang.Threads;import org.zkoss.util.logging.Log;import org.zkoss.zk.ui.Executions;import org.zkoss.zk.ui.Desktop;import org.zkoss.zk.ui.Component;import org.zkoss.zul.*;/** * Used to test the server-push feature. * * @author tomyeh */public class ServerPush {	private static final Log log = Log.lookup(ServerPush.class);	public static void start(Component info) throws InterruptedException {		final Desktop desktop = Executions.getCurrent().getDesktop();		if (desktop.isServerPushEnabled()) {			Messagebox.show("Already started");		} else {			desktop.removeAttribute("sp.ceased");			desktop.enableServerPush(true);			new WorkingThread(info).start();		}	}	public static void stop() throws InterruptedException {		final Desktop desktop = Executions.getCurrent().getDesktop();		if (desktop.isServerPushEnabled()) {			desktop.enableServerPush(false);			desktop.setAttribute("sp.ceased", Boolean.TRUE);		} else {			Messagebox.show("Already stopped");		}	}	public static void updateInfo(Component info, String postfix) {		Integer i = (Integer)info.getAttribute("count");		int v = i == null ? 0: i.intValue() + 1;		if (info.getChildren().size() >= 10) {			((Component)info.getChildren().get(0)).detach();			((Component)info.getChildren().get(0)).detach();		}		info.setAttribute("count", new Integer(v));		info.appendChild(new Label(" " + v + " " + postfix));		info.appendChild(new Separator());			//create a new component that is easier to detect bugs	}	private static class WorkingThread extends Thread {		private final Desktop _desktop;		private final Component _info;		private WorkingThread(Component info) {			_desktop = info.getDesktop();			_info = info;		}		public void run() {			try {				while (_desktop.getAttribute("sp.ceased") == null) {					Executions.activate(_desktop);					try {						updateInfo(_info, "comet");					} catch (RuntimeException ex) {						log.error(ex);						throw ex;					} catch (Error ex) {						log.error(ex);						throw ex;					} finally {						Executions.deactivate(_desktop);					}					Threads.sleep(2000); //Update every two seconds				}				log.info("The server push thread ceased");			} catch (InterruptedException ex) {				log.info("The server push thread interrupted", ex);			}		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -