8.1.1 Actions and Threads - Reference Documentation
Authors: Andres Almiray
Version: 1.2.0
8.1.1 Actions and Threads
A key aspect that you must always keep in mind is proper threading. Often times controller actions will be bound in response to an event driven by the UI. Those actions will usually be invoked in the same thread that triggered the event, which would be the UI thread. When that happens you must make sure that the executed code is short and that it quickly returns control to the UI thread. Failure to do so may result in unresponsive applications.The following example is the typical use case that must be avoidedclass BadController {
def badAction = {
def sql = Sql.newInstance(
app.config.datasource.url,
model.username,
model.password,
app.config.datasource.driver
)
model.products.clear()
sql.eachRow("select * from products") { product ->
model.products << [product.id, product.name, product.price]
}
sql.close()
}
}
class GoodController { def goodAction = { execOutsideUI { def sql = null try { sql = Sql.newInstance( app.config.datasource.url, model.username, model.password, app.config.datasource.driver ) List results = [] sql.eachRow("select * from products") { product -> results << [product.id, product.name, product.price] } execInsideUIAsync { model.products.clear() model.addAll(results) } } finally { sql?.close() } } } }
execOutsideUI
as the compiler will do it for you. This feature breaks backward compatibility with previous releases so it's possible to disable it altogether. Please refer to the Disable Threading Injection section. This feature can be partially enabled/disabled too. You can specify with absolute precision which actions should have this feature enabled or disabled, by adding the following settings to griffon-app/conf/BuildConfig.groovy
compiler { threading { sample { SampleController { action1 = false action2 = true } FooController = false } bar = false } }
- the action identified by
sample.SampleController.action1
will not have automatic threading injected into its code, whilesample.SampleController.action2
(and any other found in the same controller) will have it. - all actions belonging to
sample.FooController
will not have automatic threading injected. - all actions belonging to all controllers in the
bar
package will not have threading injected.
Automatic threading injection only works for Groovy based controllers. You must add appropriate threading code to controller actions that are written in languages other than Groovy.