Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building loosely coupled and reactive applicati...

Michael Plöd
December 14, 2015

Building loosely coupled and reactive applications with the Grails 3 event system

Grails 3 has introduced a brand new event system which is based on Reactor. This new feature allows developers to build reactive applications with Grails 3. In this talk, Michael will cover the event system in Grails 3 in detail and explain how it can be used for asynchronous and decoupled communication between commands, services and even plugins. In addition to that, he will explain the characteristics of reactive applications and show in how far the Grails 3 event system will support you in developing such applications

Michael Plöd

December 14, 2015
Tweet

More Decks by Michael Plöd

Other Decks in Programming

Transcript

  1. MICHAEL PLÖD ▸ Principal Consultant at innoQ ▸ Architecture and

    Software Development Consulting ▸ Areas of interest: Event Sourcing, Caching, Grails, Software Architecture ▸ Follow me on Twitter: @bitboss
  2. ASYNC FEATURES IN GRAILS 2.X ▸ Promises ▸ Async GORM

    ▸ Async Requens Handling ▸ Servlet 3.0 async
  3. I WANT TO CREATE A LOOSELY COUPLED APPLICATION WITH A

    HIGH COHESION Some software craftsman
  4. THESE QUALIFY FOR ASYNC PROCESSING ARE THEY _REALLY_ A CORE

    CONCERN OF THE ARTICLE CONTROLLER? COUPLING REVISED SAVE TO DATABASE WITH GORM PUBLISH ON TWITTER PUBLISH ON FACEBOOK NOTIFY CHIEF FOR REVIEW (BY MAIL) ARTICLE CONTROLLER
 
 SAVE()
  5. EVENTS: AN INTERESTING ALTERNATIVE SAVE TO DATABASE WITH GORM ARTICLE

    CONTROLLER
 
 SAVE() ARTICLE CREATED TWITTER HANDLER NOTIFICATION HANDLER FACEBOOK HANDLER
  6. WHAT IS REACTOR ▸ Foundational Framework for asynchronous applications on

    the JVM ▸ Abstractions for Java, Groovy and other JVM languages ▸ Aims at making event and data-driven applications easier ▸ High performance (15.000.000 events / second with the fastest non-blocking dispatcher) ▸ https://github.com/reactor/reactor.
  7. THE BASIC BUILDING BLOCKS ARE: 
 SELECTORS, CONSUMERS AND EVENTS

    IMPORT STATIC REACTOR.FN.$; REACTOR REACTOR = R.CREATE(); REACTOR.ON($("PARSE"), NEW CONSUMER<EVENT<STRING>>() { PUBLIC VOID CALL(EVENT<STRING> EV) { SERVICE.HANDLEEVENT(EV); } }); REACTOR.NOTIFY("PARSE", FN.EVENT("HELLO WORLD!"));
  8. REACTOR IN GRAILS 3 ▸ Configuration ▸ Consuming Events ▸

    Sending Events ▸ Events trait for Services and Controllers ▸ Spring Annotations ▸ Events trait for other classes (that aren’t Services or Controllers) ▸ GORM Events ▸ Spring Events
  9. REACTOR IN GRAILS 3: CONFIGURATION CONFIGURATION IN APPLICATION.YML reactor: dispatchers:

    default: myExecutor myExecutor: type: threadPoolExecutor size: 5 backlog: 2048
  10. REACTOR IN GRAILS 3: CONSUMING EVENTS CONSUMING EVENTS WITH CONSUMERS

    AND SELECTORS on("myEvent") { println "Event fired!" } on(„articlePublishedEvent") { Article article -> //do something with the article }
  11. REACTOR IN GRAILS 3: EVENTS TRAIT SERVICES AND CONTROLLERS IMPLEMENT

    THE EVENTS TRAIT class EventController {
 @PostConstruct
 void init()
 {
 on("articlePublished") { payload ->
 println "article published "
 }
 } … }
  12. REACTOR IN GRAILS 3: EVENTS TRAIT SERVICES MUST BE ANNOTATED

    WITH @CONSUMER @Transactional
 @Consumer
 class TwitterService{
 @PostConstruct
 void init()
 {
 on("articlePublished") { payload ->
 println "article published"
 }
 } }
  13. REACTOR IN GRAILS 3: EVENTS TRAIT YOU CAN ALSO USE

    SPRING ANNOTATIONS FOR SELECTORS @Transactional
 @Consumer
 class TwitterService{
 @Selector('articlePublished')
 void publishArticleOnTwitter()
 {
 ….. } }
  14. REACTOR IN GRAILS 3: EVENTS TRAIT EVENTS OF NON-SERVICE OR

    -CONTROLLER CLASSES @Component
 @Consumer
 class TwitterPublisher implements Events {
 @PostConstruct
 void init()
 {
 on("articlePublished") { payload ->
 println "article published"
 }
 } }
  15. REACTOR IN GRAILS 3: EVENTS FROM GORM YOU CAN EVEN

    REACT TO EVENTS FROM GORM ▸ gorm:preInsert ▸ gorm:postInsert ▸ gorm:preDelete ▸ gorm:postDelete ▸ gorm:preLoad ▸ gorm:postLoad ▸ gorm:preUpdate ▸ gorm:postUpdate ▸ gorm:saveOrUpdate ▸ gorm:datastoreInitialized ▸ gorm:validation on(„gorm:preInsert“) { PreInsertEvent e -> //do something with the event } GORM EVENTS ARE ASYNCHRONOUS: THEY CAN’T MODIFY PERSISTENCE OPERATIONS
  16. REACTOR IN GRAILS 3: EVENTS FROM SPRINT SPRING ALSO FIRES

    EVENTS on("spring:applicationStarted") { ApplicationStartedEvent event -> // …. } on("spring:servletRequestHandled") { RequestHandledEvent event -> // …. }