Groovy

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 object which you can use to perform callback operations like onError and onComplete.

Reference: Grails Generate Asynchronous Controller from our JCG partner Albert van Veen at the JDriven blog.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mallesh
Mallesh
9 years ago

Hi, How can we configure thread pool and Queue config?

Mallesh
Mallesh
9 years ago

Hi, How can we configure thread pool and Queue config?

Back to top button