SlideShare a Scribd company logo
1 of 42
Download to read offline
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

MongoDB Fundamentals
MongoDB FundamentalsMongoDB Fundamentals
MongoDB FundamentalsMongoDB
 
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013Introduction and Overview of Apache Kafka, TriHUG July 23, 2013
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013mumrah
 
MapReduce : Simplified Data Processing on Large Clusters
MapReduce : Simplified Data Processing on Large ClustersMapReduce : Simplified Data Processing on Large Clusters
MapReduce : Simplified Data Processing on Large ClustersAbolfazl Asudeh
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBMongoDB
 
Achieve Blazing-Fast Ingest Speeds with Apache Arrow
Achieve Blazing-Fast Ingest Speeds with Apache ArrowAchieve Blazing-Fast Ingest Speeds with Apache Arrow
Achieve Blazing-Fast Ingest Speeds with Apache ArrowNeo4j
 
Benchmark MinHash+LSH algorithm on Spark
Benchmark MinHash+LSH algorithm on SparkBenchmark MinHash+LSH algorithm on Spark
Benchmark MinHash+LSH algorithm on SparkXiaoqian Liu
 
Learn Big Data & Hadoop
Learn Big Data & Hadoop Learn Big Data & Hadoop
Learn Big Data & Hadoop Edureka!
 
hbaseconasia2019 HBCK2: Concepts, trends, and recipes for fixing issues in HB...
hbaseconasia2019 HBCK2: Concepts, trends, and recipes for fixing issues in HB...hbaseconasia2019 HBCK2: Concepts, trends, and recipes for fixing issues in HB...
hbaseconasia2019 HBCK2: Concepts, trends, and recipes for fixing issues in HB...Michael Stack
 
3. CPU virtualization and scheduling
3. CPU virtualization and scheduling3. CPU virtualization and scheduling
3. CPU virtualization and schedulingHwanju Kim
 
What Is Hadoop | Hadoop Tutorial For Beginners | Edureka
What Is Hadoop | Hadoop Tutorial For Beginners | EdurekaWhat Is Hadoop | Hadoop Tutorial For Beginners | Edureka
What Is Hadoop | Hadoop Tutorial For Beginners | EdurekaEdureka!
 
Lessons Learned: Using Spark and Microservices
Lessons Learned: Using Spark and MicroservicesLessons Learned: Using Spark and Microservices
Lessons Learned: Using Spark and MicroservicesAlexis Seigneurin
 
MongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To TransactionsMongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To TransactionsMydbops
 
VTU 6th Sem Elective CSE - Module 3 cloud computing
VTU 6th Sem Elective CSE - Module 3 cloud computingVTU 6th Sem Elective CSE - Module 3 cloud computing
VTU 6th Sem Elective CSE - Module 3 cloud computingSachin Gowda
 
Ingesting and Processing IoT Data Using MQTT, Kafka Connect and Kafka Streams...
Ingesting and Processing IoT Data Using MQTT, Kafka Connect and Kafka Streams...Ingesting and Processing IoT Data Using MQTT, Kafka Connect and Kafka Streams...
Ingesting and Processing IoT Data Using MQTT, Kafka Connect and Kafka Streams...confluent
 
RedisConf17 - Roblox - How Roblox Keeps Millions of Users Up to Date with Red...
RedisConf17 - Roblox - How Roblox Keeps Millions of Users Up to Date with Red...RedisConf17 - Roblox - How Roblox Keeps Millions of Users Up to Date with Red...
RedisConf17 - Roblox - How Roblox Keeps Millions of Users Up to Date with Red...Redis Labs
 

What's hot (20)

MongoDB Fundamentals
MongoDB FundamentalsMongoDB Fundamentals
MongoDB Fundamentals
 
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013Introduction and Overview of Apache Kafka, TriHUG July 23, 2013
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013
 
MapReduce : Simplified Data Processing on Large Clusters
MapReduce : Simplified Data Processing on Large ClustersMapReduce : Simplified Data Processing on Large Clusters
MapReduce : Simplified Data Processing on Large Clusters
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Achieve Blazing-Fast Ingest Speeds with Apache Arrow
Achieve Blazing-Fast Ingest Speeds with Apache ArrowAchieve Blazing-Fast Ingest Speeds with Apache Arrow
Achieve Blazing-Fast Ingest Speeds with Apache Arrow
 
NoSql
NoSqlNoSql
NoSql
 
Data sharding
Data shardingData sharding
Data sharding
 
Benchmark MinHash+LSH algorithm on Spark
Benchmark MinHash+LSH algorithm on SparkBenchmark MinHash+LSH algorithm on Spark
Benchmark MinHash+LSH algorithm on Spark
 
Learn Big Data & Hadoop
Learn Big Data & Hadoop Learn Big Data & Hadoop
Learn Big Data & Hadoop
 
RubiX
RubiXRubiX
RubiX
 
hbaseconasia2019 HBCK2: Concepts, trends, and recipes for fixing issues in HB...
hbaseconasia2019 HBCK2: Concepts, trends, and recipes for fixing issues in HB...hbaseconasia2019 HBCK2: Concepts, trends, and recipes for fixing issues in HB...
hbaseconasia2019 HBCK2: Concepts, trends, and recipes for fixing issues in HB...
 
3. CPU virtualization and scheduling
3. CPU virtualization and scheduling3. CPU virtualization and scheduling
3. CPU virtualization and scheduling
 
What Is Hadoop | Hadoop Tutorial For Beginners | Edureka
What Is Hadoop | Hadoop Tutorial For Beginners | EdurekaWhat Is Hadoop | Hadoop Tutorial For Beginners | Edureka
What Is Hadoop | Hadoop Tutorial For Beginners | Edureka
 
Lessons Learned: Using Spark and Microservices
Lessons Learned: Using Spark and MicroservicesLessons Learned: Using Spark and Microservices
Lessons Learned: Using Spark and Microservices
 
MongoDB
MongoDBMongoDB
MongoDB
 
Designing data intensive applications
Designing data intensive applicationsDesigning data intensive applications
Designing data intensive applications
 
MongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To TransactionsMongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To Transactions
 
VTU 6th Sem Elective CSE - Module 3 cloud computing
VTU 6th Sem Elective CSE - Module 3 cloud computingVTU 6th Sem Elective CSE - Module 3 cloud computing
VTU 6th Sem Elective CSE - Module 3 cloud computing
 
Ingesting and Processing IoT Data Using MQTT, Kafka Connect and Kafka Streams...
Ingesting and Processing IoT Data Using MQTT, Kafka Connect and Kafka Streams...Ingesting and Processing IoT Data Using MQTT, Kafka Connect and Kafka Streams...
Ingesting and Processing IoT Data Using MQTT, Kafka Connect and Kafka Streams...
 
RedisConf17 - Roblox - How Roblox Keeps Millions of Users Up to Date with Red...
RedisConf17 - Roblox - How Roblox Keeps Millions of Users Up to Date with Red...RedisConf17 - Roblox - How Roblox Keeps Millions of Users Up to Date with Red...
RedisConf17 - Roblox - How Roblox Keeps Millions of Users Up to Date with Red...
 

Similar to Asynchronous and event-driven Grails applications

Reactive microservices with Micronaut - GR8Conf EU 2018
Reactive microservices with Micronaut - GR8Conf EU 2018Reactive microservices with Micronaut - GR8Conf EU 2018
Reactive microservices with Micronaut - GR8Conf EU 2018Alvaro Sanchez-Mariscal
 
Workshop: Async and Parallel in C#
Workshop: Async and Parallel in C#Workshop: Async and Parallel in C#
Workshop: Async and Parallel in C#Rainer Stropek
 
Understanding Asynchronous JavaScript
Understanding Asynchronous JavaScriptUnderstanding Asynchronous JavaScript
Understanding Asynchronous JavaScriptjnewmanux
 
Reactive microservices with Micronaut - Greach 2018
Reactive microservices with Micronaut - Greach 2018Reactive microservices with Micronaut - Greach 2018
Reactive microservices with Micronaut - Greach 2018Alvaro Sanchez-Mariscal
 
Marble Testing RxJS streams
Marble Testing RxJS streamsMarble Testing RxJS streams
Marble Testing RxJS streamsIlia Idakiev
 
Webpack Encore Symfony Live 2017 San Francisco
Webpack Encore Symfony Live 2017 San FranciscoWebpack Encore Symfony Live 2017 San Francisco
Webpack Encore Symfony Live 2017 San FranciscoRyan Weaver
 
Samuele Resca - REACTIVE PROGRAMMING, DAMN. IT IS NOT ABOUT REACTJS - Codemot...
Samuele Resca - REACTIVE PROGRAMMING, DAMN. IT IS NOT ABOUT REACTJS - Codemot...Samuele Resca - REACTIVE PROGRAMMING, DAMN. IT IS NOT ABOUT REACTJS - Codemot...
Samuele Resca - REACTIVE PROGRAMMING, DAMN. IT IS NOT ABOUT REACTJS - Codemot...Codemotion
 
Offline First with Service Worker
Offline First with Service WorkerOffline First with Service Worker
Offline First with Service WorkerMuhammad Samu
 
Asynchronous Programming in ASP.NET
Asynchronous Programming in ASP.NETAsynchronous Programming in ASP.NET
Asynchronous Programming in ASP.NETChris Dufour
 
Step By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts AppStep By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts AppSyed Shahul
 
Reliable Javascript
Reliable Javascript Reliable Javascript
Reliable Javascript Glenn Stovall
 
High Performance Microservices with Ratpack and Spring Boot
High Performance Microservices with Ratpack and Spring BootHigh Performance Microservices with Ratpack and Spring Boot
High Performance Microservices with Ratpack and Spring BootDaniel Woods
 
PWA 與 Service Worker
PWA 與 Service WorkerPWA 與 Service Worker
PWA 與 Service WorkerAnna Su
 
JQuery UK February 2015: Service Workers On Vacay
JQuery UK February 2015: Service Workers On VacayJQuery UK February 2015: Service Workers On Vacay
JQuery UK February 2015: Service Workers On VacayNatasha Rooney
 
JQuery UK Service Workers Talk
JQuery UK Service Workers TalkJQuery UK Service Workers Talk
JQuery UK Service Workers TalkNatasha Rooney
 
Access to User Activities - Activity Platform APIs
Access to User Activities - Activity Platform APIsAccess to User Activities - Activity Platform APIs
Access to User Activities - Activity Platform APIsAtlassian
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSam Brannen
 
Service Worker - Reliability bits
Service Worker - Reliability bitsService Worker - Reliability bits
Service Worker - Reliability bitsjungkees
 

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

Reactive microservices with Micronaut - GR8Conf EU 2018
Reactive microservices with Micronaut - GR8Conf EU 2018Reactive microservices with Micronaut - GR8Conf EU 2018
Reactive microservices with Micronaut - GR8Conf EU 2018
 
Workshop: Async and Parallel in C#
Workshop: Async and Parallel in C#Workshop: Async and Parallel in C#
Workshop: Async and Parallel in C#
 
Understanding Asynchronous JavaScript
Understanding Asynchronous JavaScriptUnderstanding Asynchronous JavaScript
Understanding Asynchronous JavaScript
 
Reactive microservices with Micronaut - Greach 2018
Reactive microservices with Micronaut - Greach 2018Reactive microservices with Micronaut - Greach 2018
Reactive microservices with Micronaut - Greach 2018
 
Marble Testing RxJS streams
Marble Testing RxJS streamsMarble Testing RxJS streams
Marble Testing RxJS streams
 
Webpack Encore Symfony Live 2017 San Francisco
Webpack Encore Symfony Live 2017 San FranciscoWebpack Encore Symfony Live 2017 San Francisco
Webpack Encore Symfony Live 2017 San Francisco
 
Samuele Resca - REACTIVE PROGRAMMING, DAMN. IT IS NOT ABOUT REACTJS - Codemot...
Samuele Resca - REACTIVE PROGRAMMING, DAMN. IT IS NOT ABOUT REACTJS - Codemot...Samuele Resca - REACTIVE PROGRAMMING, DAMN. IT IS NOT ABOUT REACTJS - Codemot...
Samuele Resca - REACTIVE PROGRAMMING, DAMN. IT IS NOT ABOUT REACTJS - Codemot...
 
Offline First with Service Worker
Offline First with Service WorkerOffline First with Service Worker
Offline First with Service Worker
 
Asynchronous Programming in ASP.NET
Asynchronous Programming in ASP.NETAsynchronous Programming in ASP.NET
Asynchronous Programming in ASP.NET
 
Step By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts AppStep By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts App
 
Server Side Swift: Vapor
Server Side Swift: VaporServer Side Swift: Vapor
Server Side Swift: Vapor
 
Reliable Javascript
Reliable Javascript Reliable Javascript
Reliable Javascript
 
High Performance Microservices with Ratpack and Spring Boot
High Performance Microservices with Ratpack and Spring BootHigh Performance Microservices with Ratpack and Spring Boot
High Performance Microservices with Ratpack and Spring Boot
 
PWA 與 Service Worker
PWA 與 Service WorkerPWA 與 Service Worker
PWA 與 Service Worker
 
JQuery UK February 2015: Service Workers On Vacay
JQuery UK February 2015: Service Workers On VacayJQuery UK February 2015: Service Workers On Vacay
JQuery UK February 2015: Service Workers On Vacay
 
JQuery UK Service Workers Talk
JQuery UK Service Workers TalkJQuery UK Service Workers Talk
JQuery UK Service Workers Talk
 
Access to User Activities - Activity Platform APIs
Access to User Activities - Activity Platform APIsAccess to User Activities - Activity Platform APIs
Access to User Activities - Activity Platform APIs
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. REST
 
Spring boot
Spring boot Spring boot
Spring boot
 
Service Worker - Reliability bits
Service Worker - Reliability bitsService Worker - Reliability bits
Service Worker - Reliability bits
 

More from Alvaro Sanchez-Mariscal

Creating applications with Grails, Angular JS and Spring Security - G3 Summit...
Creating applications with Grails, Angular JS and Spring Security - G3 Summit...Creating applications with Grails, Angular JS and Spring Security - G3 Summit...
Creating applications with Grails, Angular JS and Spring Security - G3 Summit...Alvaro Sanchez-Mariscal
 
Mastering Grails 3 Plugins - G3 Summit 2016
Mastering Grails 3 Plugins - G3 Summit 2016Mastering Grails 3 Plugins - G3 Summit 2016
Mastering Grails 3 Plugins - G3 Summit 2016Alvaro Sanchez-Mariscal
 
Desarrollo de aplicaciones con Grails 3, Angular JS y Spring Security
Desarrollo de aplicaciones con Grails 3, Angular JS y Spring SecurityDesarrollo de aplicaciones con Grails 3, Angular JS y Spring Security
Desarrollo de aplicaciones con Grails 3, Angular JS y Spring SecurityAlvaro Sanchez-Mariscal
 
Creating applications with Grails, Angular JS and Spring Security - GR8Conf U...
Creating applications with Grails, Angular JS and Spring Security - GR8Conf U...Creating applications with Grails, Angular JS and Spring Security - GR8Conf U...
Creating applications with Grails, Angular JS and Spring Security - GR8Conf U...Alvaro Sanchez-Mariscal
 
Mastering Grails 3 Plugins - GR8Conf US 2016
Mastering Grails 3 Plugins - GR8Conf US 2016Mastering Grails 3 Plugins - GR8Conf US 2016
Mastering Grails 3 Plugins - GR8Conf US 2016Alvaro Sanchez-Mariscal
 
Mastering Grails 3 Plugins - GR8Conf EU 2016
Mastering Grails 3 Plugins - GR8Conf EU 2016Mastering Grails 3 Plugins - GR8Conf EU 2016
Mastering Grails 3 Plugins - GR8Conf EU 2016Alvaro Sanchez-Mariscal
 
Creating applications with Grails, Angular JS and Spring Security - GR8Conf E...
Creating applications with Grails, Angular JS and Spring Security - GR8Conf E...Creating applications with Grails, Angular JS and Spring Security - GR8Conf E...
Creating applications with Grails, Angular JS and Spring Security - GR8Conf E...Alvaro Sanchez-Mariscal
 
Mastering Grails 3 Plugins - Greach 2016
Mastering Grails 3 Plugins - Greach 2016Mastering Grails 3 Plugins - Greach 2016
Mastering Grails 3 Plugins - Greach 2016Alvaro Sanchez-Mariscal
 
Creating applications with Grails, Angular JS and Spring Security
Creating applications with Grails, Angular JS and Spring SecurityCreating applications with Grails, Angular JS and Spring Security
Creating applications with Grails, Angular JS and Spring SecurityAlvaro Sanchez-Mariscal
 
Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016
Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016
Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016Alvaro Sanchez-Mariscal
 
Efficient HTTP applications on the JVM with Ratpack - JDD 2015
Efficient HTTP applications on the JVM with Ratpack - JDD 2015Efficient HTTP applications on the JVM with Ratpack - JDD 2015
Efficient HTTP applications on the JVM with Ratpack - JDD 2015Alvaro Sanchez-Mariscal
 
Stateless authentication with OAuth 2 and JWT - JavaZone 2015
Stateless authentication with OAuth 2 and JWT - JavaZone 2015Stateless authentication with OAuth 2 and JWT - JavaZone 2015
Stateless authentication with OAuth 2 and JWT - JavaZone 2015Alvaro Sanchez-Mariscal
 
Stateless authentication for microservices - GR8Conf 2015
Stateless authentication for microservices - GR8Conf 2015Stateless authentication for microservices - GR8Conf 2015
Stateless authentication for microservices - GR8Conf 2015Alvaro Sanchez-Mariscal
 
Stateless authentication for microservices - Spring I/O 2015
Stateless authentication for microservices  - Spring I/O 2015Stateless authentication for microservices  - Spring I/O 2015
Stateless authentication for microservices - Spring I/O 2015Alvaro Sanchez-Mariscal
 
Stateless authentication for microservices - Greach 2015
Stateless authentication for microservices - Greach 2015Stateless authentication for microservices - Greach 2015
Stateless authentication for microservices - Greach 2015Alvaro Sanchez-Mariscal
 

More from Alvaro Sanchez-Mariscal (20)

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

Recently uploaded

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 

Recently uploaded (20)

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 

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