Directory

← Back

UITask for Vaadin

UITask is a server-side add-on for Vaadin 7 that provides a simple framework for executing background tasks and updating the UI safely.

Author

Rating

Popularity

<100

The add-on is primarily composed of a task that can be run on a background thread via an Executor and then complete the work safely in the UI thread/lock via a UIAccessor. Vaadin is not thread-safe, therefore all UI modifications must be done after obtaining the UI lock. This add-on attempts to simplify that process by providing an implementation of Future and exposing methods that are safely called in the UI thread.

The add-on was inspired by by Swing's SwingWorker and JavaFX's Task.

View the README for more details and examples.

Sample code

class MyTask extends UITask<Integer> {

  protected Integer runInBackground() {
    // Some long running calculation that doesn't use the UI.
    return 42;
  }

  protected done() {
    if (!isCancelled()) {
      int result = get();

      someUiComponent.setValue(result);  
    }
  }
}

Future<Integer> task = new MyTask(new UIAccessor.Current());
executor.execute(task);
class MyTask extends ProgressUITask<File> {

  protected Integer runInBackground() {
    // Some long running calculation that doesn't use the UI.
    updateTotal(100);

    for (int i = 0; i < 100; i++) {
      // Do some work.
      updateProgress(i);
      updateMessage(format("Downloading segment %d of 100", i));
    }

    return new File("/var/tmp/download.tmp");
  }

  protected done() {
    if (!isCancelled()) {
      File result = get();

      someUiComponent.setValue("Download complete: " + result.getAbsolutePath());
    }
    else {
      someUiComponent.setValue("Download cancelled.");
    }
  }
}

ProgressUITask<Integer> task = new MyTask(injectedUiAccessor);
totalLabel.setPropertyDataSource(task.getTotal());
progressLabel.setPropertyDataSource(task.getProgress());;
messageLabel.setPropertyDataSource(task.getMessage());
executor.execute(task);

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

  • Added test case for canceling before run.
  • Updated documentation.
  • Fixed a packaging bug where the parent POM was referenced from the module. This is not supported in the Vaadin Directory.
Released
2016-07-11
Maturity
BETA
License
Apache License 2.0

Compatibility

Framework
Vaadin 7.3+
Browser
N/A

UITask for Vaadin - Vaadin Add-on Directory

UITask is a server-side add-on for Vaadin 7 that provides a simple framework for executing background tasks and updating the UI safely. UITask for Vaadin - Vaadin Add-on Directory
The add-on is primarily composed of a task that can be run on a background thread via an Executor and then complete the work safely in the UI thread/lock via a UIAccessor. Vaadin is not thread-safe, therefore all UI modifications must be done after obtaining the UI lock. This add-on attempts to simplify that process by providing an implementation of Future and exposing methods that are safely called in the UI thread. The add-on was inspired by by Swing's SwingWorker and JavaFX's Task. View the README for more details and examples.
Online