Skip to content

Instantly share code, notes, and snippets.

@Artur-
Created July 29, 2015 11:28
Show Gist options
  • Save Artur-/f808d18235da07c9bfda to your computer and use it in GitHub Desktop.
Save Artur-/f808d18235da07c9bfda to your computer and use it in GitHub Desktop.
@Push(transport = Transport.LONG_POLLING)
public class PushWithoutThreads extends DelayedUI {
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = Vaadin7helloworldUI.class)
public static class Servlet extends VaadinServlet {
}
private VerticalLayout layout;
@Override
protected void init(VaadinRequest request) {
layout = new VerticalLayout();
layout.addComponent(new Label("Please wait while doing heavy lifting on the server..."));
setContent(layout);
}
@Override
protected void delayedInit() {
sleep(5000);
layout.removeAllComponents();
layout.addComponent(new Label("Thank you for waiting, we are now ready to rock"));
Button button = new Button("Do heavy lifting");
button.addClickListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
ProgressBar progressBar = new ProgressBar();
progressBar.setValue(0f);
layout.addComponent(progressBar);
push(); // Force changes to be sent to the client
sleep(3000); // Heavy lifting part 1
progressBar.setValue(0.5f);
push();
sleep(2000); // Heavy lifting part 2
layout.removeComponent(progressBar);
layout.addComponent(new Label("Heavy lifting done, thanks for waiting patiently"));
}
});
layout.addComponent(button);
}
protected void sleep(int i) {
try {
Thread.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment