Vaadin Table 使用线程仅以一种方式工作
Vaadin Table using threads only working one way
我有一个名为 HomeView 的 class,用于扩展 Vaadin Designer HTML class。这个 class 有一个 Vaadin table 从上传的文件中获取输入。到目前为止,文件上传正常,我可以将文件分成多行进行测试。我试图使用 Vaadin 线程锁定会话并转到 UploadFile class,我将在其中拆分文件并添加到 table 中的一行。然后我会解锁会话,退回到后台线程,UI 应该用新行更新 table。下面的代码不会发生这种情况。
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();
}
});
上传文件class
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();
}
}
如果我采用上面的代码并将其放在方法调用的位置,那么 table 会正常更新。 table 正在后台更新行数,只是不刷新视图。我缺少什么来使 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();
}
});
UI.getCurrent() helper 使用 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 访问之外,您还需要正确工作的推送连接或轮询才能获得对客户端的更改。如果你想使用 "real push" 你需要添加注释并添加 vaadin-push 模块到你的应用程序。更简单的方法(通常同样好)只是启用轮询:
ui.setPollInterval(1000); // 1000ms polling interval for client
我有一个名为 HomeView 的 class,用于扩展 Vaadin Designer HTML class。这个 class 有一个 Vaadin table 从上传的文件中获取输入。到目前为止,文件上传正常,我可以将文件分成多行进行测试。我试图使用 Vaadin 线程锁定会话并转到 UploadFile class,我将在其中拆分文件并添加到 table 中的一行。然后我会解锁会话,退回到后台线程,UI 应该用新行更新 table。下面的代码不会发生这种情况。
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();
}
});
上传文件class
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();
}
}
如果我采用上面的代码并将其放在方法调用的位置,那么 table 会正常更新。 table 正在后台更新行数,只是不刷新视图。我缺少什么来使 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();
}
});
UI.getCurrent() helper 使用 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 访问之外,您还需要正确工作的推送连接或轮询才能获得对客户端的更改。如果你想使用 "real push" 你需要添加注释并添加 vaadin-push 模块到你的应用程序。更简单的方法(通常同样好)只是启用轮询:
ui.setPollInterval(1000); // 1000ms polling interval for client