【问题标题】:Vaadin Table using threads only working one wayVaadin Table 使用线程仅以一种方式工作
【发布时间】:2016-04-23 18:12:13
【问题描述】:

我有一个名为 HomeView 的类,用于扩展 Vaadin Designer HTML 类。这个类有一个 Vaadin 表,它从上传的文件中获取输入。到目前为止,文件上传正常,我可以将文件分成几行进行测试。我试图使用 Vaadin 线程来锁定会话并转到 UploadFile 类,在该类中我将拆分文件并添加到表中的一行。然后我会解锁会话,退出到后台线程,UI 应该用新行更新表。下面的代码不会发生这种情况。

    public void uploadSucceeded(Upload.SucceededEvent succeededEvent) {
            //upload notification for upload
            new Notification("File Uploaded Successfully",
                    Notification.Type.HUMANIZED_MESSAGE)
            .show(Page.getCurrent());
            //create new class for parsing logic
            uf = new UploadFile();

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        getSession().lock();
                        uf.parseFile();
                        getSession().unlock();
                    } catch (IOException e) {
                        new Notification("Could not parse file type",
                                e.getMessage(),
                                Notification.Type.ERROR_MESSAGE)
                        .show(Page.getCurrent());
                    }
                    catch (UnsupportedOperationException e) {
                        e.printStackTrace();
                    } catch (ReadOnlyException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            //outputFile.delete();
        }
    });

上传文件类

public class UploadFile extends HomeView {

/**
 * 
 */
private static final long serialVersionUID = 839096232794540854L;

public void parseFile() throws IOException {

    //container.removeAllItems();
    BufferedReader reader = null;

    reader = new BufferedReader(new InputStreamReader(new FileInputStream(outputFile.getAbsolutePath()), StandardCharsets.UTF_8));
    String line;
    while ((line = reader.readLine()) != null)
    {
        System.out.println("before add:" + uploadTable.size());
        container = uploadTable.getContainerDataSource();
        container.addItem("row3");
        Item item2 = container.getItem("row3");
        Property property2 = item2.getItemProperty("name");
        property2.setValue("hello");
        uploadTable.setContainerDataSource(container);
        System.out.println("after add:" + uploadTable.size());

    }
    reader.close();
}
}

如果我使用上面的代码并把它放在方法调用的位置,那么表格会更新得很好。该表正在后台更新行数,它只是不刷新视图。我缺少什么来刷新 UI?

@Override
        public void uploadSucceeded(Upload.SucceededEvent succeededEvent) {
            //upload notification for upload
            new Notification("File Uploaded Successfully",
                    Notification.Type.HUMANIZED_MESSAGE)
            .show(Page.getCurrent());
            //create new class for parsing logic
            uf = new UploadFile();

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        getSession().lock();

                        BufferedReader reader = null;

                        reader = new BufferedReader(new InputStreamReader(new FileInputStream(outputFile.getAbsolutePath()), StandardCharsets.UTF_8));
                        String line;
                        while ((line = reader.readLine()) != null)
                        {
                            System.out.println("before add:" + uploadTable.size());
                            container = uploadTable.getContainerDataSource();
                            container.addItem("row3");
                            Item item2 = container.getItem("row3");
                            Property property2 = item2.getItemProperty("name");
                            property2.setValue("hello");
                            uploadTable.setContainerDataSource(container);
                            System.out.println("after add:" + uploadTable.size());

                        }
                        reader.close();


                        getSession().unlock();
                    } catch (IOException e) {
                        new Notification("Could not parse file type",
                                e.getMessage(),
                                Notification.Type.ERROR_MESSAGE)
                        .show(Page.getCurrent());
                    }
                    catch (UnsupportedOperationException e) {
                        e.printStackTrace();
                    } catch (ReadOnlyException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            //outputFile.delete();
        }
    });

【问题讨论】:

  • 您是否尝试将线程作为suggested by the vaadin book 提交给ui.access() 方法?
  • 您很可能需要启用轮询或推送。否则客户端只有在下次访问服务器时才会注意到状态变化。
  • 我已经尝试在我的课程中设置@Push 并确保异步为真。我还尝试在线程代码中设置极点间隔。两者都没有做任何事情。
  • getSession().lock(); UI.getCurrent().access(new Runnable() { public void run() { try { uf.parseFile(); }
  • 这也没有用,我也厌倦了在新线程上设置 UI.getCurrent().access。

标签: java multithreading session vaadin


【解决方案1】:

UI.getCurrent() 帮助器使用 ThreadLocal 变量来获取活动 UI,并且它仅适用于在 UI 线程中执行的代码(例如 init 方法或按钮单击侦听器)。 在构造线程之前获取 UI 参考,并在修改 UI 的代码周围使用访问方法。不要使用 getSession().lock() 或类似的,你很可能会做错什么。这是一个简单的使用示例,应该也可以帮助您解决您的用例。

            // Get the reference to UI to be modified
        final UI ui = getUI();

        new Thread() {
            @Override
            public void run() {
                // Do stuff that don't affect UI state here, e.g. potentially
                // slow calculation or rest call
                final double d = 1*1;

                ui.access(new Runnable() {
                    @Override
                    public void run() {
                        // This code here is safe to modify ui
                        Notification.show("The result of calculation is " + d);
                    }
                });
            }
       }.start();

除了正确同步的 UI 访问之外,您还需要正常工作的推送连接或轮询以获取对客户端的更改。如果您想使用“真正的推送”,您需要添加注释并将 vaadin-push 模块添加到您的应用程序。更简单的方法(通常同样好)就是启用轮询:

ui.setPollInterval(1000); // 1000ms polling interval for client

【讨论】:

  • 谢谢,我会尝试用这个方法实现
  • 刚回来在您的帮助下解决了这个问题,很清楚我哪里出错了。得到它的工作,感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 2020-07-30
相关资源
最近更新 更多