Directory

← Back

Refresher

A non-visible poller component

Author

Contributors

Rating

Deprecated. Use Vaadin 7 polling instead.

Because of the way Vaadin works, asynchronous UI changes made on the server side (e.g. by a long-duration data processing Thread) aren't reflected to the client side. This component makes it possible to make UI changes, even if the user doesn't start a transaction.

Sample code

public class RefresherUI extends UI {
	
	public class DatabaseListener implements RefreshListener {
		private static final long serialVersionUID = -8765221895426102605L;
		
		@Override
		public void refresh(final Refresher source) {
			if (databaseResult != null) {
				// stop polling
				removeExtension(source);
				
				// replace the "loading" with the actual fetched result
				content.setValue("Database result was: " + databaseResult);
			}
		}
	}
	
	public class DatabaseQueryProcess extends Thread {
		@Override
		public void run() {
			databaseResult = veryHugeDatabaseCalculation();
		}
		
		private String veryHugeDatabaseCalculation() {
			// faux long lasting database query
			try {
				Thread.sleep(6000);
			} catch (final InterruptedException ignore) {
				return "interrupted!";
			}
			return "huge!";
		}
	}
	
	private static final long serialVersionUID = -1744455941100836080L;
	
	private String databaseResult = null;
	private Label content;
	
	@Override
	public void init(final VaadinRequest request) {
		// present with a loading contents.
		content = new Label("please wait while the database is queried");
		addComponent(content);
		
		// the Refresher polls automatically
		final Refresher refresher = new Refresher();
		refresher.addListener(new DatabaseListener());
		addExtension(refresher);
		
		new DatabaseQueryProcess().start();
	}
}

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

No new code, just recompiled in Java 6

Released
2014-03-14
Maturity
STABLE
License
Apache License 2.0

Compatibility

Framework
Vaadin 7.1+
Vaadin 7.0+ in 1.2.1.7
Vaadin 6.2+ in 1.1.1
Browser
Internet Explorer
Internet Explorer
Internet Explorer
Firefox
Opera
Safari
Google Chrome
Internet Explorer
iOS Browser
Android Browser
Internet Explorer
Online