我有一个网格,我试图从传入的websocket数据自动更新。我正在尝试将新消息添加到网格数据持有者,但我在堆栈跟踪中遇到了多个错误。
@Push
public class MyUI extends UI implements Broadcaster.BroadcastListener {
VerticalLayout vertmain;
List<Trade> trades;
@Override
protected void init(VaadinRequest request) {
vertmain = new VerticalLayout();
vertmain.addComponent(new Label("start"));
trades = Arrays.asList(Trade.newTrade(1, "buy"), Trade.newTrade(2, "sell"));
Grid<Trade> grid = new Grid<>();
grid.setItems(trades);
grid.addColumn(Trade::getSide);
grid.addColumn(Trade::getSize);
vertmain.addComponent(grid);
//start weboscket stream in new thread
access(() -> {
try {
WebsocketSetup.startStream();
} catch (Exception e) { e.printStackTrace(); }
});
// Register to receive broadcasts
Broadcaster.register(this);
setContent(vertmain);
}
@Override
public void receiveBroadcast(final String message) {
// Must lock the session to execute logic safely
access(new Runnable() {
@Override
public void run() {
trades.add(Trade.newTrade(100, message));
}
});
}
我是vaadin的新手,可能会忽视某些东西,但是一直在弄乱网格而且无法让它工作......
栈跟踪
Apr 03, 2018 12:35:57 AM com.vaadin.server.DefaultErrorHandler doDefault
SEVERE:
java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException
at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:191)
at com.vaadin.server.VaadinSession$FutureAccess.get(VaadinSession.java:114)
at com.vaadin.server.VaadinService.runPendingAccessTasks(VaadinService.java:2049)
at com.vaadin.server.VaadinSession.unlock(VaadinSession.java:1019)
at com.vaadin.server.VaadinService.ensureAccessQueuePurged(VaadinService.java:2011)
at com.vaadin.server.VaadinService.accessSession(VaadinService.java:1977)
at com.vaadin.server.VaadinSession.access(VaadinSession.java:1418)
at com.vaadin.ui.UI.access(UI.java:1562)
at my.vaadin.MyUI.receiveBroadcast(MyUI.java:101)
at my.vaadin.utils.Broadcaster$1.run(Broadcaster.java:35)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.UnsupportedOperationException
at java.base/java.util.AbstractList.add(AbstractList.java:153)
at my.vaadin.MyUI.addTrade(MyUI.java:120)
at my.vaadin.MyUI.access$000(MyUI.java:23)
at my.vaadin.MyUI$2.run(MyUI.java:105)
at com.vaadin.ui.UI.accessSynchronously(UI.java:1501)
at com.vaadin.ui.UI$3.run(UI.java:1565)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:514)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at com.vaadin.server.VaadinService.runPendingAccessTasks(VaadinService.java:2046)
... 10 more
答案 0 :(得分:7)
Arrays.asList
返回一个不可变的List,这意味着它无法修改。这与Vaadin无关。
相反,尝试以经典方式创建列表:
trades = new LinkedList<>();
trades.add(...);
添加新项目后,您应该刷新网格:
grid.getDataProvider().refreshAll();