Java 使用仅单向工作的线程的Vaadin表
我有一个名为HomeView的类,用于扩展Vaadin Designer HTML类。这个类有一个Vaadin表,它从上传的文件中获取输入。到目前为止,文件上传很好,我可以将文件分成几行进行测试。我试图使用Vaadin线程来锁定会话并转到UploadFile类,在该类中我将拆分文件并添加到表中的一行。然后我将解锁会话,退出后台线程,UI将用新行更新表。下面的代码不会发生这种情况Java 使用仅单向工作的线程的Vaadin表,java,multithreading,session,vaadin,Java,Multithreading,Session,Vaadin,我有一个名为HomeView的类,用于扩展Vaadin Designer HTML类。这个类有一个Vaadin表,它从上传的文件中获取输入。到目前为止,文件上传很好,我可以将文件分成几行进行测试。我试图使用Vaadin线程来锁定会话并转到UploadFile类,在该类中我将拆分文件并添加到表中的一行。然后我将解锁会话,退出后台线程,UI将用新行更新表。下面的代码不会发生这种情况 public void uploadSucceeded(Upload.SucceededEvent succe
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();
}
});
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推送模块添加到你的应用程序中。更简单的方法(通常也同样好)就是启用轮询:
ui.setPollInterval(1000); // 1000ms polling interval for client
您是否尝试将线程提交到
ui.access()
方法作为?您很可能需要启用轮询或推送。否则,客户端只会在下次访问服务器时才注意到状态更改。我已尝试在类中设置@Push,并确保asynchronous为true。我还尝试在线程代码中设置极点间隔。两者都没有做任何事情;UI.getCurrent().access(new Runnable(){public void run(){try{uf.parseFile();}这也没用,我厌倦了设置UI.getCurrent().访问新线程。谢谢,我将尝试使用此方法实现。在您的帮助下,我刚刚返回此方法来解决此问题,很清楚我的错误所在。使其正常工作,谢谢您的帮助。