Grails Generate Asynchronous Controller
Since version 2.3, Grails supports asynchronous parallel programming to support modern multiple core hardware. Therefore a new Grails command is added to generate asynchronous controllers for domain classes. The generated controller contains CRUD actions for a given domain class. In the example below, we will generate a default asynchronous implementation of a Grails controller. First we create a domain object:
$ grails create-domain-class grails.data.Movie
Second we will generate the asynchronous controller using the new generate-async-controller
command:
$ grails generate-async-controller grails.data.Movie
Grails now generates an asynchronous controller with the name MovieController
. Below you can see the default implementation of the index
method:
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
Movie.async.task {
[movieInstanceList: list(params), count: count() ]
}.then { result ->
respond result.movieInstanceList, model:[movieInstanceCount: result.count]
}
}
The async
namespace makes sure GORM methods in the task
method will be performed in a different thread and therefore is asynchronous. The task
method which is used, returns a [Promises](http://grails.org/doc/latest/api/grails/async/Promises.html)
object which you can use to perform callback operations like onError
and onComplete
.