Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Grails 3 Inside and Out

1,483 views

Published on

Recorded at SpringOne2GX
Speaker: Graeme Rocher
GG Special Topics Track

In this talk, Grails project lead Graeme Rocher will take a deep dive into the internals of Grails 3.x and reveal some hidden features and gems that you can take advantage of when developing your own Grails applications and plugins.

Discussing key concepts such as Traits, ASTs and the Grails application lifecycle this talk aims to clarify some of the emerging patterns in Grails 3.x application and plugin development.

Published in: Technology

Grails 3 Inside and Out

  1. 1. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ SPRINGONE2GX WASHINGTON, DC Grails 3 Inside & Out By Graeme Rocher @graemerocher
  2. 2. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Who we are? Jeff Scott Brown Object Computing Inc. @jeffscottbrown brownj@ociweb.com 2 Graeme Rocher Object Computing Inc. @graemerocher rocherg@ociweb.com
  3. 3. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Grails + OCI • OCI focused on Grails • Full service support – Consulting – Training – Support • Commitment to engineering • Grails team at OCI growing – Come speak to us! 3
  4. 4. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 4 More at ociweb.com/grails Visit the OCI’s Table #2 to meet our Grails Team! Graeme Rocher Jeff Brown Dave Klein Colin Harrington
  5. 5. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Agenda • Plugins & Traits • Plugins & Events • Configuration API • Code Generation API • Gradle & The Shell 5
  6. 6. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ The Example • Build a Mail Sender Plugin • Trait • Events • Configuration • Code Generation 6
  7. 7. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ SPRINGONE2GX WASHINGTON, DC Plugins & Traits
  8. 8. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Adding Behaviour with Traits • Use Enhances to add behaviour 8 import grails.artefact.* @Enhances('Controller') trait MyTrait { void myNewMethod() { … } }
  9. 9. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Core Grails Traits • Controller - implemented by all controllers • DomainClass - implemented by all domain classes • GormEntity - implemented by all GORM entities • Service - implemented by all services • TagLibrary - implemented by all tag libraries • Interceptor - all Grails interceptors • Events - Any object that wants to send events 9
  10. 10. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ SPRINGONE2GX WASHINGTON, DC Events
  11. 11. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Events • Any class that implements the Events trait can send and receive events • Controllers and services implement the trait by default • You can effectively decouple systems using an event driven approach • Fits nicely with reactive programming • Events are built on the Reactor framework • http://projectreactor.io 11
  12. 12. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Events • Use on to listen and notify to send or combine with sendAndReceive 12 notify('mail:sent', eventFor(message)) on('mail:sent') { Event<TYPE> event -> // do stuff }
  13. 13. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Events • The Consumer and Selector annotations from Spring Reactor work too! 13 @Consumer class MyService { @Selector('mail:sent') void mailEventListener(MailMessage msg) { // do stuff } }
  14. 14. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ SPRINGONE2GX WASHINGTON, DC Configuration API
  15. 15. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Configuration API 15 boolean isFooEnabled = config .getProperty('foo.bar', Boolean, true) // or @Value('foo.bar') boolean isFooEnabled
  16. 16. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Plugins & Configuration • Applications inherit plugin configuration • You should keep your plugin configuration minimal • Plugin configuration defines the default values • Applications can override any plugin provided value 16
  17. 17. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ SPRINGONE2GX WASHINGTON, DC Code Generation & Grails
  18. 18. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Code Generation Scripts • Go in src/main/scripts in plugins or applications • All scripts subclass the GroovyScriptCommand class • Some handy properties • gradle - for invoking Gradle • ant - for invoking Ant • methodMissing - for invoking other code gen commands • Useful traits • TemplateRenderer - for rendering templates • FileSystemInteraction - for interacting with the file system 18
  19. 19. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Code Generation Scripts • The model method exposes a Model interface for introspecting conventions 19 interface Model { String getClassName() String getFullName() String getPackageName() … String getLowerCaseName() String convention(String conventionName) Map<String, Object> asMap() }
  20. 20. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ DSL for Code Generation Scripts 20 description("Creates a service that sends a mail") { usage "This does cools stuff" } def model = model(args[0]) render template: template('MailService.groovy'), destination: file("grails-app/services/$ {model.packagePath}/$ {model.convention('Service')}.groovy"), model: model
  21. 21. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ SPRINGONE2GX WASHINGTON, DC Gradle & The Shell
  22. 22. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Gradle & The Shell • Create Gradle tasks that invoke your app • Without knowing any Gradle! ;-) 22 class MailBatcherCommand implements ApplicationCommand {
 
 boolean handle(ExecutionContext context) { // DO STUFF
 return true
 }
 }
  23. 23. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Gradle & The Shell • Just add to build.gradle build class path and run the command 23 classpath 'org.grails.plugins:mailSender:0.1-SNAPSHOT' gradle batchSendMail grails batch-send-mail
  24. 24. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Summary • Grails 3 provides powerful techniques to build plugins that empower your application • The core philosophy of plugins and modular applications continues • Through traits and events Grails 3 is more powerful that ever before! • Grails’ internals are all based on the described techniques 24
  25. 25. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Q & A 25
  26. 26. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 26 More at ociweb.com/grails The OCI Grails Team is growing — will You join us?
  27. 27. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a
 Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Stay Connected. Web: grails.org StackOverflow: http://stackoverflow.com/tags/grails Slack: http://slack-signup.grails.org Twitter: twitter.com/grailsframework LinkedIn: http://linkedin.com/groups/Grails-User-Group-39757 27 More at ociweb.com/grails

×