Background tasks for long running queries on main window?

Hi:

I have a “dashboard” page I developed using charts and KeyValueContainers on my application’s main window. Some of my queries can take a few minutes to load.

I am looking at various options to speed up the queries, but another solution is to bring the main window up immediately and put the query refresh and subsequent chart refresh in one or more background tasks.

Has anybody ever done this? Are there any examples out there I can look at?

Is there a better way to handle this?

Hello @ericraskin

I suggest you to use CUBA Background Tasks API. It seems that it’s what you need.

Regards,
Daniil.

Thanks. Are there any examples I can see? The documentation doesn’t show a complete interaction with a screen, and the Live Demo button doesn’t link to any demos of BackgroundTasks that I can find.

Hello @ericraskin

Sorry for waiting. Please take a look at this example:

@Inject
private BackgroundWorker backgroundWorker;
@Inject
private Notifications notifications;

@Subscribe
private void onAfterShow(AfterShowEvent event) {
    // instantiate a task
    BackgroundTask<Long, String> longRunningTask =
            new BackgroundTask<Long, String>(30L, TimeUnit.SECONDS, this) {

                @Override
                public String run(TaskLifeCycle<Long> taskLifeCycle) throws Exception {
                    // "hard working"
                    Thread.sleep(5000L);
                    return "Task finished!";
                }

                @Override
                public void done(String result) {
                    notifications.create(Notifications.NotificationType.HUMANIZED)
                        .withCaption(result)
                        .show();
                }
            };
    // run the task
    backgroundWorker.handle(longRunningTask)
            .execute();
}

Regards,
Daniil

Thank you very much. :grinning: