Для того, чтобы форма UI обновлялась автоматически, нужно в init просписать интервал обновления UI.getCurrent().setPollInterval(500);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
@Theme("mytheme") @SuppressWarnings("serial") public class MyVaadinUI extends UI implements Runnable{ private ApplicationContext context; private MeasurementRepository measurementRepository; private VerticalLayout layout; @WebServlet(value = "/*", asyncSupported = true) @VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class, widgetset = "ru.test.AppWidgetSet") public static class Servlet extends VaadinServlet { } @Override protected void init(VaadinRequest request) { context = new AnnotationConfigApplicationContext(Config.class); measurementRepository = context.getBean(MeasurementRepository.class); layout = new VerticalLayout(); layout.setMargin(true); setContent(layout); UI.getCurrent().setPollInterval(500); new Thread(this).start(); } @Override public void run() { while(true){ ArrayList<measurement> measurements = Lists.newArrayList(measurementRepository.findAll()); for (Measurement measurement : measurements) { layout.addComponent(new Label(measurement.toString())); } try { Thread.sleep(1000); } catch (final InterruptedException e) { e.printStackTrace(); } } } } |
Vaadin. Обновление UI в потоке