SlideShare a Scribd company logo
Async and event-driven
Grails applications
Álvaro Sánchez-Mariscal
@alvaro_sanchez
About me
— Coming from Madrid
!
— Developer since 2001 (Java / Spring stack).
— Grails fanboy since v0.4.
— Working @ OCI since 2015: Groovy, Grails & Micronaut!
— Father since 2017!
"
@alvaro_sanchez
Introduction
@alvaro_sanchez
Grails Async Framework
— https://async.grails.org
— Introduced in Grails 2.3 with the Promise API. Spun-
off in Grails 3.3
— Supports different async libraries: GPars, RxJava and
Reactor.
— Application events, Spring events, GORM events.
— Async GORM.
@alvaro_sanchez
Grails Async Framework
— Server Sent Events.
— RxGORM.
— Async request/response processing.
— Servlet 3.0 async support.
@alvaro_sanchez
 Ge"ing started
— Add the dependency in build.gradle:
runtime "org.grails.plugins:async"
— Check the Grails plugin page for more information:
http://plugins.grails.org/plugin/grails/async
@alvaro_sanchez
Servlet 3.0 API
@alvaro_sanchez
 Servlet 3.0
— Spec'ed in 2009!
!
— Allows offloading of blocking operations to a different
thread.
— Implement grails.async.web.AsyncController to get
started.
— Call startAsync() to get the javax.servlet.AsyncContext
— When done, call complete() or dispatch()
@alvaro_sanchez
Demo
@alvaro_sanchez
 Advanced usage
ctx.addListener(new AsyncListener() {
void onStartAsync(AsyncEvent event) throws IOException { }
void onComplete(AsyncEvent event) throws IOException { }
void onTimeout(AsyncEvent event) throws IOException { }
void onError(AsyncEvent event) throws IOException { }
})
ctx.timeout = 5_000
@alvaro_sanchez
Promise API
@alvaro_sanchez
Grails Promise API
— Builds on top of java.util.concurrent.Future.
— grails.async.Promises helps you to create Promise<T>
instances.
— In controllers, better use grails.async.web.WebPromises
(Servlet 3.0 Async under the covers)
— PromiseFactory allows pluggable implementations.
@alvaro_sanchez
PromiseFactory API
— CachedThreadPoolPromiseFactory (default): unbound
thread pool.
— GparsPromiseFactory: if org:grails:grails-async-gpars
dependecy is on the classpath.
— RxPromiseFactory: if either org.grails:grails-async-
rxjava or org.grails:grails-async-rxjava2 are on the
classpath.
@alvaro_sanchez
PromiseFactory API
— SynchronousPromiseFactory: useful for unit testing.
import org.grails.async.factory.*
import grails.async.*
Promises.promiseFactory = new SynchronousPromiseFactory()
@alvaro_sanchez
Demo
@alvaro_sanchez
PromiseList
import grails.async.*
PromiseList<Integer> list = new PromiseList<>()
list << { 2 * 2 }
list << { 4 * 4 }
list << { 8 * 8 }
list.onComplete { List<Integer> results ->
assert [4,16,64] == results
}
@alvaro_sanchez
 PromiseMap
import static grails.async.Promises.*
PromiseMap promiseMap = tasks one: { 2 * 2 },
two: { 4 * 4},
three:{ 8 * 8 }
assert [one:4,two:16,three:64] == promiseMap.get()
@alvaro_sanchez
@DelegateAsync
import grails.async.*
class BookService {
List<Book> findBooks(String title) { ... }
}
class AsyncBookService {
@DelegateAsync BookService bookService
}
@alvaro_sanchez
@DelegateAsync
import grails.async.*
class BookService {
List<Book> findBooks(String title) { ... }
}
class AsyncBookService { //Equivalent
Promise<List<Book>> findBooks(String title) {
task {
bookService.findBooks(title)
}
}
}
@alvaro_sanchez
Using the service
@Autowired AsyncBookService asyncBookService
def findBooks(String title) {
asyncBookService.findBooks(title)
.onComplete { List<Book> results ->
render "Books = ${results}"
}
}
@alvaro_sanchez
Events
@alvaro_sanchez
Grails Events abstraction
— Add the dependency in build.gradle:
runtime "org.grails.plugins:events"
— By default Grails creates an EventBus based off of the
currently active PromiseFactory.
— Or you can use:
— org.grails:grails-events-gpars
— org.grails:grails-events-rxjava
— org.grails:grails-events-rxjava2
@alvaro_sanchez
Publishing events
— With the @Publisher annotation:
class SumService {
@Publisher
int sum(int a, int b) {
a + b
}
}
@alvaro_sanchez
Publishing events
— With the EventPublisher API:
class SumService implements EventPublisher {
int sum(int a, int b) {
int result = a + b
notify("sum", result)
return result
}
}
@alvaro_sanchez
Subscribing to events
— With the @Subscriber annotation:
class TotalService {
AtomicInteger total = new AtomicInteger(0)
@Subscriber
void onSum(int num) {
total.addAndGet(num)
}
}
@alvaro_sanchez
Subscribing to events
— With the EventBusAware API:
class TotalService implements EventBusAware {
AtomicInteger total = new AtomicInteger(0)
@PostConstruct
void init() {
eventBus.subscribe("sum") { int num ->
total.addAndGet(num)
}
}
}
@alvaro_sanchez
GORM events
— DatastoreInitializedEvent
— PostDeleteEvent
— PostInsertEvent
— PostLoadEvent
— PostUpdateEvent
@alvaro_sanchez
GORM events
— PreDeleteEvent
— PreInsertEvent
— PreLoadEvent
— PreUpdateEvent
— SaveOrUpdateEvent
— ValidationEvent
@alvaro_sanchez
Asynchronous subscription
— They cannot cancel or manipulate the persistence
operations.
@Subscriber
void beforeInsert(PreInsertEvent event) {
//do stuff
}
@alvaro_sanchez
Synchronous subscription
@Listener
void tagFunnyBooks(PreInsertEvent event) {
String title = event.getEntityAccess()
.getPropertyValue("title")
if(title?.contains("funny")) {
event.getEntityAccess()
.setProperty("title", "Humor - ${title}".toString())
}
}
@alvaro_sanchez
Spring events
— Disabled by default, for performance reasons.
— Enable them by setting grails.events.spring to true.
@Events(namespace="spring")
class MyService {
@Subscriber
void applicationStarted(ApplicationStartedEvent event) {
// fired when the application starts
}
@Subscriber
void servletRequestHandled(RequestHandledEvent event) {
// fired each time a request is handled
}
}
@alvaro_sanchez
Async GORM
@alvaro_sanchez
 Get started
— NOTE: drivers are still blocking. This just offloads it to
a different thread pool.
— Include compile "org.grails:grails-datastore-gorm-
async" in your build.
— Implement the AsyncEntity trait:
class Book implements AsyncEntity<Book> {
...
}
@alvaro_sanchez
The async namespace
Promise<Person> p1 = Person.async.get(1L)
Promise<Person> p2 = Person.async.get(2L)
List<Person> people = waitAll(p1, p2) //Blocks
Person.async.list().onComplete { List<Person> results ->
println "Got people = ${results}"
}
Promise<Person> person = Person.where {
lastName == "Simpson"
}.async.list()
@alvaro_sanchez
Async and the Hibernate session
— The Hibernate session is not concurrency safe.
— Objects returned from asynchronous queries will be
detached entities.
— This will fail:
Person p = Person.async.findByFirstName("Homer").get()
p.firstName = "Bart"
p.save()
@alvaro_sanchez
Async and the Hibernate session
— Entities need to be merged first with the session
bound to the calling thread
Person p = Person.async.findByFirstName("Homer").get()
p.merge()
p.firstName = "Bart"
p.save()
— Also, be aware that association lazy loading will not
work. Use eager queries / fetch joins / etc.
@alvaro_sanchez
Multiple async GORM calls
Promise<Person> promise = Person.async.task {
withTransaction {
Person person = findByFirstName("Homer")
person.firstName = "Bart"
person.save(flush:true)
}
}
Person updatedPerson = promise.get()
@alvaro_sanchez
Async models
import static grails.async.WebPromises.*
def index() {
tasks books: Book.async.list(),
totalBooks: Book.async.count(),
otherValue: {
// do hard work
}
}
@alvaro_sanchez
RxJava Support
@alvaro_sanchez
RxJava support
— Add org.grails.plugins:rxjava to your dependencies.
— You can then return rx.Observable's from controllers,
and Grails will:
1. Create a new asynchronous request
2. Spawn a new thread that subscribes to the
observable
3. When the observable emits a result, process the
result using the respond method.
@alvaro_sanchez
Demo
@alvaro_sanchez
Q & A
Álvaro Sánchez-Mariscal
@alvaro_sanchez

More Related Content

What's hot (20)

PDF
Flask Basics
Eueung Mulyana
 
PPT
Graphql presentation
Vibhor Grover
 
PDF
Nitro for your Grails App: how to improve performance. Greach '18
Alberto De Ávila Hernández
 
KEY
Spring AOP
Jeroen Rosenberg
 
PPT
Hibernate
Ajay K
 
PDF
Java spring framework
Rajiv Gupta
 
PPTX
Jquery ppt
044249
 
PPTX
Introduction to spring boot
Santosh Kumar Kar
 
PPTX
What is Ajax technology?
JavaTpoint.Com
 
PDF
SQL on everything, in memory
Julian Hyde
 
PPTX
Grails object relational mapping: GORM
Saurabh Dixit
 
PDF
Rest web services
Paulo Gandra de Sousa
 
PPTX
[Final] ReactJS presentation
洪 鹏发
 
PDF
Spring boot introduction
Rasheed Waraich
 
PPT
Ajax Ppt 1
JayaPrakash.m
 
PDF
Advanced java programming-contents
Self-Employed
 
PPSX
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
PPTX
Spring Boot Tutorial
Naphachara Rattanawilai
 
PPT
Jsp/Servlet
Sunil OS
 
Flask Basics
Eueung Mulyana
 
Graphql presentation
Vibhor Grover
 
Nitro for your Grails App: how to improve performance. Greach '18
Alberto De Ávila Hernández
 
Spring AOP
Jeroen Rosenberg
 
Hibernate
Ajay K
 
Java spring framework
Rajiv Gupta
 
Jquery ppt
044249
 
Introduction to spring boot
Santosh Kumar Kar
 
What is Ajax technology?
JavaTpoint.Com
 
SQL on everything, in memory
Julian Hyde
 
Grails object relational mapping: GORM
Saurabh Dixit
 
Rest web services
Paulo Gandra de Sousa
 
[Final] ReactJS presentation
洪 鹏发
 
Spring boot introduction
Rasheed Waraich
 
Ajax Ppt 1
JayaPrakash.m
 
Advanced java programming-contents
Self-Employed
 
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
Spring Boot Tutorial
Naphachara Rattanawilai
 
Jsp/Servlet
Sunil OS
 

Similar to Asynchronous and event-driven Grails applications (20)

PPTX
Grails transactions
Husain Dalal
 
PDF
GeeCON Krakow 2015 - Grails and the real-time world
Iván López Martín
 
PDF
Greach 2014 - Road to Grails 3.0
graemerocher
 
PPTX
Grails services
Vijay Shukla
 
PPTX
Grails services
NexThoughts Technologies
 
PDF
GGX 2014 - Grails and the real time world
Iván López Martín
 
PDF
Asynchronous web apps with the Play Framework 2.0
Oscar Renalias
 
PDF
Grails Launchpad - From Ground Zero to Orbit
Zachary Klein
 
PDF
Why Grails
Yiguang Hu
 
PDF
Why Grails?
Yiguang Hu
 
PDF
Understanding Database Transactions and Hibernate Sessions in Grails
Jonas Witt
 
PPTX
Spring Northwest Usergroup Grails Presentation
ajevans
 
PDF
Application Architectures in Grails
Peter Ledbrook
 
PPT
Don't Wait! Develop responsive applications with Java EE7 instead
Erin Schnabel
 
PDF
Grails 3.0 Preview
graemerocher
 
POT
intoduction to Grails Framework
Harshdeep Kaur
 
PDF
Grails Integration Strategies
daveklein
 
ODP
Groovy and Grails intro
Miguel Pastor
 
PDF
G* on GAE/J 挑戦編
Tsuyoshi Yamamoto
 
Grails transactions
Husain Dalal
 
GeeCON Krakow 2015 - Grails and the real-time world
Iván López Martín
 
Greach 2014 - Road to Grails 3.0
graemerocher
 
Grails services
Vijay Shukla
 
Grails services
NexThoughts Technologies
 
GGX 2014 - Grails and the real time world
Iván López Martín
 
Asynchronous web apps with the Play Framework 2.0
Oscar Renalias
 
Grails Launchpad - From Ground Zero to Orbit
Zachary Klein
 
Why Grails
Yiguang Hu
 
Why Grails?
Yiguang Hu
 
Understanding Database Transactions and Hibernate Sessions in Grails
Jonas Witt
 
Spring Northwest Usergroup Grails Presentation
ajevans
 
Application Architectures in Grails
Peter Ledbrook
 
Don't Wait! Develop responsive applications with Java EE7 instead
Erin Schnabel
 
Grails 3.0 Preview
graemerocher
 
intoduction to Grails Framework
Harshdeep Kaur
 
Grails Integration Strategies
daveklein
 
Groovy and Grails intro
Miguel Pastor
 
G* on GAE/J 挑戦編
Tsuyoshi Yamamoto
 
Ad

More from Alvaro Sanchez-Mariscal (20)

PDF
Serverless functions with Micronaut
Alvaro Sanchez-Mariscal
 
PDF
6 things you need to know about GORM 6
Alvaro Sanchez-Mariscal
 
PDF
Reactive microservices with Micronaut - GR8Conf EU 2018
Alvaro Sanchez-Mariscal
 
PDF
Reactive microservices with Micronaut - Greach 2018
Alvaro Sanchez-Mariscal
 
PDF
Practical Spring Cloud
Alvaro Sanchez-Mariscal
 
PDF
Creating applications with Grails, Angular JS and Spring Security - G3 Summit...
Alvaro Sanchez-Mariscal
 
PDF
Mastering Grails 3 Plugins - G3 Summit 2016
Alvaro Sanchez-Mariscal
 
PDF
Desarrollo de aplicaciones con Grails 3, Angular JS y Spring Security
Alvaro Sanchez-Mariscal
 
PDF
Creating applications with Grails, Angular JS and Spring Security - GR8Conf U...
Alvaro Sanchez-Mariscal
 
PDF
Mastering Grails 3 Plugins - GR8Conf US 2016
Alvaro Sanchez-Mariscal
 
PDF
Mastering Grails 3 Plugins - GR8Conf EU 2016
Alvaro Sanchez-Mariscal
 
PDF
Creating applications with Grails, Angular JS and Spring Security - GR8Conf E...
Alvaro Sanchez-Mariscal
 
PDF
Mastering Grails 3 Plugins - Greach 2016
Alvaro Sanchez-Mariscal
 
PDF
Creating applications with Grails, Angular JS and Spring Security
Alvaro Sanchez-Mariscal
 
PDF
Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016
Alvaro Sanchez-Mariscal
 
PDF
Efficient HTTP applications on the JVM with Ratpack - JDD 2015
Alvaro Sanchez-Mariscal
 
PDF
Stateless authentication with OAuth 2 and JWT - JavaZone 2015
Alvaro Sanchez-Mariscal
 
PDF
Stateless authentication for microservices - GR8Conf 2015
Alvaro Sanchez-Mariscal
 
PDF
Ratpack 101 - GR8Conf 2015
Alvaro Sanchez-Mariscal
 
PDF
Ratpack 101 - GeeCON 2015
Alvaro Sanchez-Mariscal
 
Serverless functions with Micronaut
Alvaro Sanchez-Mariscal
 
6 things you need to know about GORM 6
Alvaro Sanchez-Mariscal
 
Reactive microservices with Micronaut - GR8Conf EU 2018
Alvaro Sanchez-Mariscal
 
Reactive microservices with Micronaut - Greach 2018
Alvaro Sanchez-Mariscal
 
Practical Spring Cloud
Alvaro Sanchez-Mariscal
 
Creating applications with Grails, Angular JS and Spring Security - G3 Summit...
Alvaro Sanchez-Mariscal
 
Mastering Grails 3 Plugins - G3 Summit 2016
Alvaro Sanchez-Mariscal
 
Desarrollo de aplicaciones con Grails 3, Angular JS y Spring Security
Alvaro Sanchez-Mariscal
 
Creating applications with Grails, Angular JS and Spring Security - GR8Conf U...
Alvaro Sanchez-Mariscal
 
Mastering Grails 3 Plugins - GR8Conf US 2016
Alvaro Sanchez-Mariscal
 
Mastering Grails 3 Plugins - GR8Conf EU 2016
Alvaro Sanchez-Mariscal
 
Creating applications with Grails, Angular JS and Spring Security - GR8Conf E...
Alvaro Sanchez-Mariscal
 
Mastering Grails 3 Plugins - Greach 2016
Alvaro Sanchez-Mariscal
 
Creating applications with Grails, Angular JS and Spring Security
Alvaro Sanchez-Mariscal
 
Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016
Alvaro Sanchez-Mariscal
 
Efficient HTTP applications on the JVM with Ratpack - JDD 2015
Alvaro Sanchez-Mariscal
 
Stateless authentication with OAuth 2 and JWT - JavaZone 2015
Alvaro Sanchez-Mariscal
 
Stateless authentication for microservices - GR8Conf 2015
Alvaro Sanchez-Mariscal
 
Ratpack 101 - GR8Conf 2015
Alvaro Sanchez-Mariscal
 
Ratpack 101 - GeeCON 2015
Alvaro Sanchez-Mariscal
 
Ad

Recently uploaded (20)

PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Generic or Specific? Making sensible software design decisions
Bert Jan Schrijver
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Wondershare PDFelement Pro Crack for MacOS New Version Latest 2025
bashirkhan333g
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 

Asynchronous and event-driven Grails applications

  • 1. Async and event-driven Grails applications Álvaro Sánchez-Mariscal @alvaro_sanchez
  • 2. About me — Coming from Madrid ! — Developer since 2001 (Java / Spring stack). — Grails fanboy since v0.4. — Working @ OCI since 2015: Groovy, Grails & Micronaut! — Father since 2017! " @alvaro_sanchez
  • 4. Grails Async Framework — https://async.grails.org — Introduced in Grails 2.3 with the Promise API. Spun- off in Grails 3.3 — Supports different async libraries: GPars, RxJava and Reactor. — Application events, Spring events, GORM events. — Async GORM. @alvaro_sanchez
  • 5. Grails Async Framework — Server Sent Events. — RxGORM. — Async request/response processing. — Servlet 3.0 async support. @alvaro_sanchez
  • 6.  Ge"ing started — Add the dependency in build.gradle: runtime "org.grails.plugins:async" — Check the Grails plugin page for more information: http://plugins.grails.org/plugin/grails/async @alvaro_sanchez
  • 8.  Servlet 3.0 — Spec'ed in 2009! ! — Allows offloading of blocking operations to a different thread. — Implement grails.async.web.AsyncController to get started. — Call startAsync() to get the javax.servlet.AsyncContext — When done, call complete() or dispatch() @alvaro_sanchez
  • 10.  Advanced usage ctx.addListener(new AsyncListener() { void onStartAsync(AsyncEvent event) throws IOException { } void onComplete(AsyncEvent event) throws IOException { } void onTimeout(AsyncEvent event) throws IOException { } void onError(AsyncEvent event) throws IOException { } }) ctx.timeout = 5_000 @alvaro_sanchez
  • 12. Grails Promise API — Builds on top of java.util.concurrent.Future. — grails.async.Promises helps you to create Promise<T> instances. — In controllers, better use grails.async.web.WebPromises (Servlet 3.0 Async under the covers) — PromiseFactory allows pluggable implementations. @alvaro_sanchez
  • 13. PromiseFactory API — CachedThreadPoolPromiseFactory (default): unbound thread pool. — GparsPromiseFactory: if org:grails:grails-async-gpars dependecy is on the classpath. — RxPromiseFactory: if either org.grails:grails-async- rxjava or org.grails:grails-async-rxjava2 are on the classpath. @alvaro_sanchez
  • 14. PromiseFactory API — SynchronousPromiseFactory: useful for unit testing. import org.grails.async.factory.* import grails.async.* Promises.promiseFactory = new SynchronousPromiseFactory() @alvaro_sanchez
  • 16. PromiseList import grails.async.* PromiseList<Integer> list = new PromiseList<>() list << { 2 * 2 } list << { 4 * 4 } list << { 8 * 8 } list.onComplete { List<Integer> results -> assert [4,16,64] == results } @alvaro_sanchez
  • 17.  PromiseMap import static grails.async.Promises.* PromiseMap promiseMap = tasks one: { 2 * 2 }, two: { 4 * 4}, three:{ 8 * 8 } assert [one:4,two:16,three:64] == promiseMap.get() @alvaro_sanchez
  • 18. @DelegateAsync import grails.async.* class BookService { List<Book> findBooks(String title) { ... } } class AsyncBookService { @DelegateAsync BookService bookService } @alvaro_sanchez
  • 19. @DelegateAsync import grails.async.* class BookService { List<Book> findBooks(String title) { ... } } class AsyncBookService { //Equivalent Promise<List<Book>> findBooks(String title) { task { bookService.findBooks(title) } } } @alvaro_sanchez
  • 20. Using the service @Autowired AsyncBookService asyncBookService def findBooks(String title) { asyncBookService.findBooks(title) .onComplete { List<Book> results -> render "Books = ${results}" } } @alvaro_sanchez
  • 22. Grails Events abstraction — Add the dependency in build.gradle: runtime "org.grails.plugins:events" — By default Grails creates an EventBus based off of the currently active PromiseFactory. — Or you can use: — org.grails:grails-events-gpars — org.grails:grails-events-rxjava — org.grails:grails-events-rxjava2 @alvaro_sanchez
  • 23. Publishing events — With the @Publisher annotation: class SumService { @Publisher int sum(int a, int b) { a + b } } @alvaro_sanchez
  • 24. Publishing events — With the EventPublisher API: class SumService implements EventPublisher { int sum(int a, int b) { int result = a + b notify("sum", result) return result } } @alvaro_sanchez
  • 25. Subscribing to events — With the @Subscriber annotation: class TotalService { AtomicInteger total = new AtomicInteger(0) @Subscriber void onSum(int num) { total.addAndGet(num) } } @alvaro_sanchez
  • 26. Subscribing to events — With the EventBusAware API: class TotalService implements EventBusAware { AtomicInteger total = new AtomicInteger(0) @PostConstruct void init() { eventBus.subscribe("sum") { int num -> total.addAndGet(num) } } } @alvaro_sanchez
  • 27. GORM events — DatastoreInitializedEvent — PostDeleteEvent — PostInsertEvent — PostLoadEvent — PostUpdateEvent @alvaro_sanchez
  • 28. GORM events — PreDeleteEvent — PreInsertEvent — PreLoadEvent — PreUpdateEvent — SaveOrUpdateEvent — ValidationEvent @alvaro_sanchez
  • 29. Asynchronous subscription — They cannot cancel or manipulate the persistence operations. @Subscriber void beforeInsert(PreInsertEvent event) { //do stuff } @alvaro_sanchez
  • 30. Synchronous subscription @Listener void tagFunnyBooks(PreInsertEvent event) { String title = event.getEntityAccess() .getPropertyValue("title") if(title?.contains("funny")) { event.getEntityAccess() .setProperty("title", "Humor - ${title}".toString()) } } @alvaro_sanchez
  • 31. Spring events — Disabled by default, for performance reasons. — Enable them by setting grails.events.spring to true. @Events(namespace="spring") class MyService { @Subscriber void applicationStarted(ApplicationStartedEvent event) { // fired when the application starts } @Subscriber void servletRequestHandled(RequestHandledEvent event) { // fired each time a request is handled } } @alvaro_sanchez
  • 33.  Get started — NOTE: drivers are still blocking. This just offloads it to a different thread pool. — Include compile "org.grails:grails-datastore-gorm- async" in your build. — Implement the AsyncEntity trait: class Book implements AsyncEntity<Book> { ... } @alvaro_sanchez
  • 34. The async namespace Promise<Person> p1 = Person.async.get(1L) Promise<Person> p2 = Person.async.get(2L) List<Person> people = waitAll(p1, p2) //Blocks Person.async.list().onComplete { List<Person> results -> println "Got people = ${results}" } Promise<Person> person = Person.where { lastName == "Simpson" }.async.list() @alvaro_sanchez
  • 35. Async and the Hibernate session — The Hibernate session is not concurrency safe. — Objects returned from asynchronous queries will be detached entities. — This will fail: Person p = Person.async.findByFirstName("Homer").get() p.firstName = "Bart" p.save() @alvaro_sanchez
  • 36. Async and the Hibernate session — Entities need to be merged first with the session bound to the calling thread Person p = Person.async.findByFirstName("Homer").get() p.merge() p.firstName = "Bart" p.save() — Also, be aware that association lazy loading will not work. Use eager queries / fetch joins / etc. @alvaro_sanchez
  • 37. Multiple async GORM calls Promise<Person> promise = Person.async.task { withTransaction { Person person = findByFirstName("Homer") person.firstName = "Bart" person.save(flush:true) } } Person updatedPerson = promise.get() @alvaro_sanchez
  • 38. Async models import static grails.async.WebPromises.* def index() { tasks books: Book.async.list(), totalBooks: Book.async.count(), otherValue: { // do hard work } } @alvaro_sanchez
  • 40. RxJava support — Add org.grails.plugins:rxjava to your dependencies. — You can then return rx.Observable's from controllers, and Grails will: 1. Create a new asynchronous request 2. Spawn a new thread that subscribes to the observable 3. When the observable emits a result, process the result using the respond method. @alvaro_sanchez
  • 42. Q & A Álvaro Sánchez-Mariscal @alvaro_sanchez