私は Vaadin を初めて使用し、機能をテストするためのプロトタイプを実装しただけです。現在、私は Vaadin Server-Push 機能をいじっています。ボタンで増加できる単純なカウンターを行うのが好きです。その後、別のブラウザからこの面を開くのが好きで、カウントアップすると、すべてのブラウザが更新されるのが好きです.
今のところ、現在のブラウザは別のスレッドによって更新されていることがわかりました。
public class PushTestView extends VerticalLayout implements View {
private static final long serialVersionUID = -8056849522760207972L;
private Label counter;
public void enter(ViewChangeEvent event) {
removeAllComponents();
PushTestController controller = PushTestController.getInstance();
counter = new Label("" + controller.getCounter());
Button button = new Button("Count up");
button.addClickListener(new ClickListener() {
private static final long serialVersionUID = -1437136914245802675L;
@Override
public void buttonClick(ClickEvent event) {
InitializerThread thread = new InitializerThread();
thread.run();
}
});
addComponent(counter);
addComponent(button);
}
class InitializerThread extends Thread {
@Override
public void run() {
// Init done, update the UI after doing locking
UI.getCurrent().access(new Runnable() {
@Override
public void run() {
// Here the UI is locked and can be updated
PushTestController.getInstance().countUp();
counter.setValue("" + PushTestController.getInstance().getCounter());
UI.getCurrent().notifyAll();
}
});
}
}
}
で注釈が付けられた UI に埋め込んだこのビュー@Push
。そして、ここに私のコントローラーがあります:
public class PushTestController {
private static PushTestController instance = new PushTestController();
private int counter = 0;
private PushTestController() {
//Private Constructor for Singleton
}
public static PushTestController getInstance() {
return instance;
}
public int getCounter() {
return counter;
}
public String getCounterString() {
return "" + counter;
}
public void countUp() {
counter++;
}
}
この例を拡張して、さまざまなブラウザーで UI を更新するにはどうすればよいですか?