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

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

21a532a137b506128914478ac521fc8b?s=47 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

21a532a137b506128914478ac521fc8b?s=128

Michael Plöd

December 14, 2015
Tweet

More Decks by Michael Plöd

Other Decks in Programming

Transcript

  1. GRAILS 3 EVENTS FOR LOOSELY COUPLED AND REACTIVE APPLICATIONS MICHAEL

    PLÖD
  2. 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
  3. REACTIVE? WHAT DOES IT MEAN

  4. ? Responsive ? Resilient ? Elastic ? Message driven

  5. ASYNC FEATURES IN GRAILS 2.X ▸ Promises ▸ Async GORM

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

    HIGH COHESION Some software craftsman
  7. 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()
  8. EVEN BY USING PROMISES WE TIGHTLY COUPLE OUR COMPONENTS Some

    software craftsman
  9. < THIS
 IS THE ANATOMY OF A TYPICAL SYSTEM AFTER

    A FEW YEARS LET’S BE HONEST
  10. EVENTS: AN INTERESTING ALTERNATIVE SAVE TO DATABASE WITH GORM ARTICLE

    CONTROLLER
 
 SAVE() ARTICLE CREATED TWITTER HANDLER NOTIFICATION HANDLER FACEBOOK HANDLER
  11. 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.
  12. 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!"));
  13. 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
  14. REACTOR IN GRAILS 3: CONFIGURATION CONFIGURATION IN APPLICATION.YML reactor: dispatchers:

    default: myExecutor myExecutor: type: threadPoolExecutor size: 5 backlog: 2048
  15. 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 }
  16. REACTOR IN GRAILS 3: EVENTS TRAIT SERVICES AND CONTROLLERS IMPLEMENT

    THE EVENTS TRAIT class EventController {
 @PostConstruct
 void init()
 {
 on("articlePublished") { payload ->
 println "article published "
 }
 } … }
  17. 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"
 }
 } }
  18. REACTOR IN GRAILS 3: EVENTS TRAIT YOU CAN ALSO USE

    SPRING ANNOTATIONS FOR SELECTORS @Transactional
 @Consumer
 class TwitterService{
 @Selector('articlePublished')
 void publishArticleOnTwitter()
 {
 ….. } }
  19. 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"
 }
 } }
  20. 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
  21. REACTOR IN GRAILS 3: EVENTS FROM SPRINT SPRING ALSO FIRES

    EVENTS on("spring:applicationStarted") { ApplicationStartedEvent event -> // …. } on("spring:servletRequestHandled") { RequestHandledEvent event -> // …. }
  22. LET’S GET OUR HANDS DIRTY DEMO

  23. THANK YOU! MICHAEL PLOED - INNOQ @BITBOSS Example: https://github.com/mploed/grails-event-example