Web browser closed

Hi CUBA team,

Normally, when user click a button in a browser and webapp start doing some processing. If the client connection dropped or the web browser accidentally closed. The processing will be stopped half way. This happened occasionally if the processing is heavy like will be taking about more than 10 minutes (or up to 30 minutes) or the connected users does not have good connection or the users want to move to other form and do other things.

How to do this in CUBA framework? Are there any documentation for this?

The best is when user click on a button, a dialog pop up

Caption : Are you sure?
Check Box : [ ] Run as task
List Box : Notification : Notification, Email
Button : OK, Cancel

There will be a place to list out all task invoked by the current user.

Objective : User can close browser or logout or move to other form to do other things or invoke a task again in the same form.

Hi Adam,

for Background Tasks there are generally multiple options:

  • [url=https://doc.cuba-platform.com/manual-6.2/background_tasks.html]Background Tasks[/url]
  • [url=https://doc.cuba-platform.com/manual-6.2/scheduled_tasks_cuba_reg.html]Scheduled Tasks[/url]
  • Java Threads

With Background Tasks you can execute long running stuff and push it away from UI thread. Nevertheless, what you described - that the browser can be closed seems not be the use case for background tasks in CUBA. As you can read in the docs:
"Closing the screen will interrupt the tasks associated with it."
Here’s an example of BackgroundTasks.

Then there is scheduled tasks. The main problem according to your requirement is that a scheduled task is scheduled - meaning it runs every now and then, not directly on user request.

A possible solution to get something going like you described could be like this:

  • create an Entity that represents metadata for execution of your stuff (e.g. PriceCalculationExecution) with attributes like status, username, etc.
  • create a scheduled task that looks into the PriceCalculationExecution table every few seconds and starts an execution (as a plain Java Thread e.g.) for the instance with state NOT_PROCESSED

So it would be an approach that does only use rare features of the platform.
Perhaps there is even a more framework based approach, but the two options seems to have the above described limitations regarding your requirements. But the docs should give you a starting point to look at.


Thanks Mario David,

Option 2 might be a good idea as I can record and control the tasks easily.

I have 1 concern. Said there are 5 middleware server in the cluster. How do CUBA manage the scheduler? Said there will be only 1 scheduler doing a thing at interval of 1 hour. Will I need to specify which server to run? How to prevent other server run the tasks several instances?

In a clustered middleware scenario you have multiple options:
You can either use the Locking mechanism in order to manually lock your PriceCalculationExecution entities on the different servers (the ScheduledTask itself uses pessimistic locking to prevent the problems that arise with optimistic locking in a high frequent requested environment like task execution table). The locks are synchronised (as well as any other middleware information) via a peer to peer approach through JGroups.

Additionally ScheduledTask has the boolean toggle “singleton”, which will prevent parallel execution across two middleware servers.

The more i think about, i’m coming to the conclusion that using the SchduledTask mechanism is exactly what you want, because it has everything built in: distributed execution, logging, user references etc. The only thing that i don’t see currently is that a task can’t be executed only once (like it is possible in Quartz).
Perhaps a CUBA member can step in here and evaluate the possibility to allow one time executions of tasks. Because then you would not need to create a structure besides the scheduled task which is a scheduled task as well.

Alternatively, a fairly bigger solution for something like this is to use an external message queue. Here’s an example of a task queue in java with a RabbitMQ server. You might consider this, when the workers do not need to be the same instances as the middleware backend servers (e.g. when you cluster the middleware to 5 only because this execution is so heavyweight, but the rest of the CUBA middleware server is rarely used. This might be the case when you have very little people using the software, but the calucation task is the one that uses most of the CPU cycles.


I would go with the approach suggested by Mario: create an “ExecutionQueue” entity and start executions from a scheduled task. The scheduled task should be marked as “singleton” which prevents simultaneous execution in a cluster.
This approach should be fine if the rate of executions is not high and users can accept a small delay before starting an execution (when the scheduled task retrieves the execution from the queue). Otherwise, consider using a message queueing software that can be in-process or standalone.

As for implementing “one-off” scheduled tasks, we need to think it over.

Also please note that CUBA doesn’t detect browser page closing and related background tasks will be terminated only on HTTP session expiration. Background tasks those are related to some application Window(screen in CUBA terminology) will be cancelled on Window close.