zkoss 基于 ajax/xul/java 的框架
最近搞了一个后台推送消息到前台.一开始使用 java 的 timer 和 timertask 不好使. 为什么呢.因为 这个是异步的调用这个页面的线程, 代码上是能调用到, 但是运行时是调用不到的. 他都是在内部封装好的. 所以 还是要调用zk 自己的东西. 在官网上找到了.
经过提炼
checkPicNum.setVisible(true);
final Desktop desktop = Executions.getCurrent().getDesktop(); //应该是获取桌面映射
if (desktop.isServerPushEnabled()) { //检查是不是不可用
try {
Messagebox.show("Already started");
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
desktop.enableServerPush(true); //我也不知道这步是设置的什么
if(timer != null)timer.cancel();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
SysSceneryPicExample picExample = new SysSceneryPicExample();
picExample.createCriteria().andIsUsableEqualTo("0").andInTimeGreaterThan(date);
int num = sceneryPicService.countByExample(picExample);
Desktop desktop1 = checkPicNum.getDesktop(); //拿到操作组件的对象桌面映射
if(desktop1 == null) {
timer.cancel();
}
try {
Executions.activate(desktop1); //对映射进行操作
} catch (DesktopUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
checkPicNum.setValue(String.valueOf(num)); //操作组件
Executions.deactivate(desktop1); //在把这个映射还回去
}
}, 30000L);
}