使用自定义侦听器(接口)更新 ProgressBar

Updating ProgressBar using custom listener (interfaces)

在我的应用程序中,我有一个持久的进程,它在服务器中处理一些微积分(一些车辆的最佳路线等)。我想向用户显示一个进度条,指示进程的当前状态。

理论上,一切似乎都很容易。我的视图中有一个 ProgressBar 对象。这个进度条将通过一个监听器更新,监听器将被写成一个普通的接口,比如 IProgressListener,其中一个方法称为 notifyProgress(float pValue)。当在服务器中生成新值时(运行 calculate())将调用 notifyProgress()。在我看来,它将定义一个 IProgressListener 的实例来覆盖 notifyProggress(float pValue) 方法,它将执行 progressBar.setValue(pValue);东西。

不幸的是,当 运行 我的 webapp 时,进度条一直卡住,没有值更新,直到耗时的过程完成。这是进度条快速前进到 100%

结束的时候

有人知道为什么会这样吗?

提前致谢。

编辑:

我终于成功了。我已经替换了 IProgressListener 的用法并且我已经实现了观察者模式。看起来很奇怪,我没有将 UI 更新包装在 ui.access( () -> { //code here }); 之间。代码段(否则,UI 更改不会在此处引入)。

vaadin 论坛上有一个帖子值得一读,总结得很好 - Updating UI from another thread:

Vaadin consists of a server side that runs in a servlet container and a client side that runs in a web browser. The application state is kept on server side but communication is initiated by the client side whenever needed e.g. when a button is clicked.

由于HTTP是request-response,一般情况下server是没有办法随意向client发送数据的,除非直接向server请求。有两种主要技术可以克服这个问题:

  1. 客户端轮询 - 客户端定期向服务器请求更新 - 例如,进度条的当前值是多少。这可以通过 vaadin refresher add-on.
  2. 来实现
  3. Server Push - Server maintains connection with client (for example using WebSockets) 及时联系客户

请注意,上面提到的论坛线程提到了使用推送的特殊 vaadin 附加组件,但从那时起,在所有现代浏览器中基于 Atmosphere Framework became part of the Vaadin Framework. While websockets are supported 的常规服务器推送,代理或旧浏览器可能会出现问题。如果 websockets 不可用,大气框架提供了各种替代的自动回退。