【发布时间】: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