Best practice that long running service updates a progress bar

Hello,
first I want to thank you for this great platform and support. As I come from the .Net world I dont have so much knowledge in implementing Java applications yet.

I have a long running service method and the progress should be indicated with a progressbar in the GUI. What is the best practice to communicate the progress from the service to the GUI? I could transfer the TaskLifeCycle to the service as input parameter (so the service can call taskLifeCycle.publish) , but I think its not a good solution since TaskLifeCycle belongs to CUBA GUI package.

Hi Stefan,

There is no way to propagate service progress from middleware to web client since it requires separate connection from core to web (in case of distributed application).

You have two options to show something like progress bar for a long running service method:

  1. Show indeterminate progress using BackgroundWorkWindow
  2. Extract service method that will operate only with one piece of data and call it multiple times from BackgroundTask.

We can implement the second option using BackgroundWorkProgressWindow:


List<Integer> data = Arrays.asList(10, 20, 30, 40, 50);

BackgroundWorkProgressWindow.show(new BackgroundTask<Integer, Void>(30, this) {
    @Override
    public Void run(TaskLifeCycle<Integer> taskLifeCycle) throws Exception {
        for (Integer d : data) {
            longRunningService.incrementalLongCall(d);

            // report progress to progress bar of BackgroundWorkProgressWindow
            taskLifeCycle.publish(d);
        }
        return null;
    }
}, 50); // max value of progress = 50

If a service method should be transactional then the second is not a suitable option, so in this case you can use the first option.

I’ve attached sample project that illustrates these two approaches so you can try it in action.

service-progress.zip (21.3K)