java 异步更新 vaadin 组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11553576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:32:29  来源:igfitidea点击:

Asynchronously update vaadin components

javamultithreadingbuttonvaadin

提问by Sergey

I have this code for updating vaadin button's caption every 3 seconds.

我有这段代码每 3 秒更新一次 vaadin 按钮的标题。

TimerTask tt = new TimerTask() {

    @Override
    public void run() {
        try {
            logger.debug("adding l to button's caption");
            btn.setCaption(eventsButton.getCaption() + "l");
        } catch (Exception ex) {
            logger.error(ex.getMessage());
        }
    }
};
Timer t = new Timer(true);
t.scheduleAtFixedRate(tt, 0, 3000);

However, it can't change button's caption although it is executed every 3 seconds(judging by the log file). How can I access vaadin's GUI components from another thread?

但是,尽管每 3 秒执行一次(根据日志文件判断),但它无法更改按钮的标题。如何从另一个线程访问 vaadin 的 GUI 组件?

采纳答案by Sergey

There's an addon named ICEPush which does exactly what I needed.

有一个名为 ICEPush 的插件,它完全符合我的需要。

https://vaadin.com/directory#addon/icepush

https://vaadin.com/directory#addon/icepush

回答by Charles Anthony

A reasonably comprehensive discussion of the problem, and the various solutions can be found here; Redux: 'vanilla' Vaadin simply follows a user initiated request-response paradigm.

对问题的合理全面讨论,以及各种解决方案可以在这里找到;Redux:'vanilla' Vaadin 简单地遵循用户发起的请求-响应范式。

You'll need to use an add-on to initiate changes in the browser from the server.

您需要使用附加组件从服务器启动浏览器中的更改。

Aside : you should synchronize on the application object when updating components from your own threads (as opposed to the normal request thread) - as you may get 'Sync' errors.

旁白:当从您自己的线程(而不是普通请求线程)更新组件时,您应该在应用程序对象上进行同步 - 因为您可能会收到“同步”错误。

回答by Tomasz

Because of the way Vaadin works, asynchronous UI changes made on the server side are not reflected on the client. The refresher addonmakes it possible to make UI changes, even if the user does not start a transaction.

由于 Vaadin 的工作方式,在服务器端进行的异步 UI 更改不会反映在客户端上。在复习插件能够使用户界面的变化,即使用户不启动事务。

final Refresher refresher = new Refresher();
refresher.setRefreshInterval(3000);
addComponent(refresher);

refresher.addListener(new RefreshListener() {    
    @Override
    public void refresh(final Refresher source) {
        try {
            logger.debug("adding l to button's caption");
            btn.setCaption(eventsButton.getCaption() + "l");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }
}