ZK UI更新数据库数据更改

时间:2013-10-21 09:41:39

标签: zk

如果对数据库进行某些更改,如何更新UI上的数据? 我希望我的UI在我更改我的数据库(使用常见的数据库查看器)上的记录时自动更新数据,就像有人朋友在剪贴板上发布内容(不刷新)时那样做了

1 个答案:

答案 0 :(得分:0)

您可以使用轮询或推送。

对于推送,您可以扩展SelectorComposer 并适用于你的zul

public class ComposerPush extends SelectorComposer<Component>  {

    private static final long serialVersionUID = 1L;
// wire components
@Wire
ZHighCharts chartComp;

// Basic Line
private SimpleExtXYModel dataChartModel = new SimpleExtXYModel();


 public void startWorker() {
            final Desktop desktop = Executions.getCurrent().getDesktop();
            if (desktop.isServerPushEnabled()) {
            } else {
                desktop.enableServerPush(true);
                if(timer != null)timer.cancel();
                timer = new Timer();
                timer.schedule(databaseCheck(), 0, 5000);
            }
        }

    private TimerTask databaseCheck() { 
            return new TimerTask() {
                @Override
                public void run() {
                    updateInfo();
                }
            };
        }

        private void updateInfo() {
            try {
                 Desktop desktop = chartComp.getDesktop();
                if(desktop == null) {
                    timer.cancel();
                    return;
                }

                Executions.activate(desktop);
                try {

                          **<your code in here>**

                } finally {
                    Executions.deactivate(desktop);
                }
            } catch (DesktopUnavailableException ex) {
                System.out.println("Desktop currently unavailable");
                timer.cancel();
            } catch (InterruptedException e) {
                System.out.println("The server push thread interrupted");
                timer.cancel();
            }


        }