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.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

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
8 years ago

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

Mallesh
Mallesh
8 years ago

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

Back to top button