Docs

Documentation versions (currently viewingVaadin 24)

Implementing Callbacks Flow

How to use server push with callbacks.

When building the user interface with Vaadin Flow, the easiest way of allowing a background job to update the user interface is through callbacks. This is explained in more detail in the Callbacks documentation page.

Whenever you implement a callback, you have remember that the callback is called by the background thread. This means that any updates to the user interface must happen inside a call to UI.access().

Note
The examples on this page only work with push enabled. For information about how to do that, see the Server Push documentation page.

For every callback, you should create a private method in your user interface. The method is called inside UI.access(), so you can safely update the user interface inside it.

For example, a method for handling successful completion could look like this:

private void onJobCompleted(String result) {
    Notification.show("Job completed: " + result);
}

Likewise, a method for handling errors might be done like this:

private void onJobFailed(Exception error) {
    Notification.show("Job failed: " + error.getMessage());
}

For reporting progress, you can use a progress bar. If the background jobs reports the progress as a floating point value between 0.0 and 1.0, you can pass it directly to the setValue method of the progress bar.

Here is an example of a button click listener that starts a background job, and uses the private methods, and the progress bar, to update the user interface:

button.addClickListener(clickEvent -> {
    var ui = UI.getCurrent();
    service.startBackgroundJob(
        ui.accessLater(this::onJobCompleted, null), // (1)
        ui.accessLater(progressBar::setValue, null),
        ui.accessLater(this::onJobFailed, null)
    );
});
  1. The UI.accessLater() method is explained on the Pushing UI Updates documentation page.