Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

1,902 views
1,804 views

Published on

2011-11-01 | 03:00 PM - 03:50 PM
With all the buzz around rapid web application development frameworks, are enterprise developers left looking on enviously? Not at all. Grails brings the same benefits to Java developers while providing many options for enterprise integration. This talk shows you how to build Grails projects with Ant and Maven; what's involved in talking to legacy databases; and how to talk to Java components.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,902
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
51
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

  1. 1. Peter Ledbrook / VMware Grails in the Java EnterpriseTuesday, 1 November 11
  2. 2. What is Grails? Rapid Web Application Development Framework • for the JVM • with first-class Java integration Inspired by Ruby on Rails, Django and others • Convention over Configuration • Don’t Repeat Yourself (DRY) 2Tuesday, 1 November 11
  3. 3. What is Grails? Grails Servlet Web MVC GSP (Views) Container GORM Database I18n (Data Access) Build Test Support Doc Engine 3Tuesday, 1 November 11
  4. 4. What is Grails? Grails 4Tuesday, 1 November 11
  5. 5. What is Grails? Web Controllers The Domain Model i18n bundles Business Logic Custom View Tags Views & Layouts Libraries (JARs) Build Commands Additional Sources Tests Web Resources 5Tuesday, 1 November 11
  6. 6. Say bye-bye to the plumbing! 6Tuesday, 1 November 11
  7. 7. Demo Demo 7Tuesday, 1 November 11
  8. 8. Enterprise requirements Web App Messaging JEE Legacy Services Databases Is this a problem for Grails apps? 8Tuesday, 1 November 11
  9. 9. Integration Points Build Dependencies Database Deployment Spring 9Tuesday, 1 November 11
  10. 10. Integration Points Build Dependencies Database Deployment Spring 10Tuesday, 1 November 11
  11. 11. Build 11Tuesday, 1 November 11
  12. 12. Build Remember the Grails project structure? • add in build events and... Can’t build natively with other build tools! Ant Maven Gradle Grails Build System 12Tuesday, 1 November 11
  13. 13. Ant Integration An Ant task built in (grails.ant.GrailsTask) Template Ant build: grails integrate-with --ant • Uses Ivy for dependency management • Not compatible with Ant 1.8 ...or use ‘java’ task to call Grails command • Grails manages dependencies • Use ‘grails’ for build 13Tuesday, 1 November 11
  14. 14. Maven Maven Grails Plugin: https://github.com/grails/grails-maven Use Maven 2 or 3 to build Grails projects Declare dependencies in POM Works for both applications and plugins! Integration test framework: https://github.com/grails/ grails_maven_plugin_testing_tests 14Tuesday, 1 November 11
  15. 15. Getting Started mvn archetype-generate ... e.g. -Xmx256m mvn initialize -XX:MaxPermSize=256m Set MAVEN_OPTS Optional: add ‘pom true’ to dependency DSL 15Tuesday, 1 November 11
  16. 16. Packaging Types ‘war’ • Must configure execution section • Works with plugins that depend on ‘war’ ‘grails-app’ • Less configuration ‘grails-plugin’ • For plugins! 16Tuesday, 1 November 11
  17. 17. Maven & Grails Plugins > grails release-plugin == > mvn deploy 17Tuesday, 1 November 11
  18. 18. Maven & Grails Plugins Either: <dependency> <groupId>org.grails.plugins<groupId> <artifactId>hibernate</artifactId> <type>grails-plugin</type> </dependency> Use ‘mvn deploy’ or Release plugin! And ‘pom: false’ 18Tuesday, 1 November 11
  19. 19. Maven & Grails Plugins Or: grails.project.dependency.resolution = { ... plugins { compile ":hibernate:1.3.6" } ... } 19Tuesday, 1 November 11
  20. 20. Customise the Build Create new commands in <proj>/scripts Package the commands in a plugin! Create <proj>/scripts/_Events.groovy • Interact with standard build steps • Add test types • Configure embedded Tomcat 20Tuesday, 1 November 11
  21. 21. What the future holds... Grails 3.0 will probably move to Gradle • More powerful and more flexible • Standard, well documented API • Ant & Maven support 21Tuesday, 1 November 11
  22. 22. Integration Points Build Dependencies Database Deployment Spring 22Tuesday, 1 November 11
  23. 23. Dependencies are JARs Use any Java library you like! Full support for Maven-compatible repositories Declarative dependencies Plugins can be declared the same way 23Tuesday, 1 November 11
  24. 24. Dependency DSL grails.project.dependency.resolution = { inherits "global" log "warn" repositories { grailsHome() mavenCentral() mavenRepo "http://localhost:8081/..." } ... } 24Tuesday, 1 November 11
  25. 25. Dependency DSL grails.project.dependency.resolution = { inherits "global" log "warn" ... dependencies { compile "org.tmatesoft.svnkit:svnkit:1.3.3" test "org.gmock:gmock:0.8.1" } ... } 25Tuesday, 1 November 11
  26. 26. Integration Points Build Dependencies Database Deployment Spring 26Tuesday, 1 November 11
  27. 27. ‘Legacy’ Databases Grails can create a database from your domain model... ...but what if you don’t own the database? • DBA determines structure • Company conventions • Existing ‘legacy’ database 27Tuesday, 1 November 11
  28. 28. Option 1: Custom ORM mapping No existing domain model Schema not too far off the beaten track class Book { ... static mapping = { table "books" title type: "books" author column: "author_ref" } } 28Tuesday, 1 November 11
  29. 29. Option 2: JPA annotations Existing Java/JPA domain model <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE ...> <hibernate-configuration> <session-factory> <mapping class="org.ex.Book"/> <mapping class="org.ex.Author"/> ... </session-factory> </hibernate-configuration> grails-app/conf/hibernate/hibernate.cfg.xml 29Tuesday, 1 November 11
  30. 30. Option 3: Hibernate XML Mappings You have Java model + Hibernate mapping files Schema is way off the beaten track <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE ...> <hibernate-configuration> <session-factory> <mapping resource="org.ex.Book.hbm.xml"/> <mapping resource="org.ex.Author.hbm.xml"/> ... </session-factory> </hibernate-configuration> grails-app/conf/hibernate/hibernate.cfg.xml 30Tuesday, 1 November 11
  31. 31. Constraints Given domain class: org.example.myapp.domain.Book Then: src/java/org/example/myapp/domain/BookConstraints.groovy constraints = { title blank: false, unique: true ... } 31Tuesday, 1 November 11
  32. 32. Option 4: GORM JPA Plugin GORM layer over JPA Use your own JPA provider Useful for cloud services that only work with JPA, not Hibernate 32Tuesday, 1 November 11
  33. 33. Option 5: Remote service back-end Don’t have to use GORM Use only controllers and services • Grails services back onto remote services Web App SOAP, RMI, HTTP Invoker, etc. Invoice Log Service ... 33Tuesday, 1 November 11
  34. 34. Share your model! 34Tuesday, 1 November 11
  35. 35. Database Management Hibernate ‘update’ + Production data = ? 35Tuesday, 1 November 11
  36. 36. Database Migration Plugin Pre-production, Hibernate ‘update’ or ‘create-drop’ dbm-generate-changelog dbm-changelog-sync Change domain model dbm-gorm-diff dbm-update 36Tuesday, 1 November 11
  37. 37. Reverse Engineering Plugin class Person { String name Integer age ... } 37Tuesday, 1 November 11
  38. 38. Database Management Database Migration http://grails.org/plugin/database-migration Reverse Engineering http://grails.org/plugin/database-migration 38Tuesday, 1 November 11
  39. 39. Integration Points Build Dependencies Database Deployment Spring 39Tuesday, 1 November 11
  40. 40. grails war Build properties: • grails.war.copyToWebApp • grails.war.dependencies • grails.war.resources • grails.project.war.file 40Tuesday, 1 November 11
  41. 41. Control of JARs grails.project.dependency.resolution = { defaultDependenciesProvided true inherits "global" log "warn" ... } grails war --nojars => WEB-INF/lib/<empty> => No Grails JARs in WEB-INF/lib 41Tuesday, 1 November 11
  42. 42. Data Source JNDI: dataSource { jndiName = "java:comp/env/myDataSource" } System property: dataSource { url = System.getProperty("JDBC_STRING") } 42Tuesday, 1 November 11
  43. 43. Data Source Config.groovy: grails.config.locations = [ "file:./${appName}-config.groovy", "classpath:${appName}-config.groovy" ] For run-app: ./<app>-config.groovy For Tomcat: tomcat/lib/<app>-config.groovy 43Tuesday, 1 November 11
  44. 44. Integration Points Build Dependencies Database Deployment Spring 44Tuesday, 1 November 11
  45. 45. Grails is Spring Spring MVC under the hood Grails provides many useful beans • e.g. grailsApplication Define your own beans! • resources.xml/groovy • In a plugin 45Tuesday, 1 November 11
  46. 46. Example import ... beans = { credentialMatcher(Sha1CredentialsMatcher) { storedCredentialsHexEncoded = true } sessionFactory(ConfigurableLocalSessionFactoryBean) { dataSource = ref("dataSource") hibernateProperties = [ "hibernate.hbm2ddl.auto": "create-drop", "hibernate.show_sql": true ] } } 46Tuesday, 1 November 11
  47. 47. Enterprise Integration Spring opens up a world of possibilities • Spring Integration/Camel • Messaging (JMS/AMQP) • ESB • RMI, HttpInvoker, etc. Web services & REST 47Tuesday, 1 November 11
  48. 48. Grails Plugins Routing JMS, RabbitMQ CXF, Spring-WS, WS-Client REST 48Tuesday, 1 November 11
  49. 49. JMS Plugin Example Add any required dependencies (BuildConfig.groovy) dependencies { compile org.apache.activemq:activemq-core:5.3.0 } Configure JMS factory (resources.groovy) jmsConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) { brokerURL = vm://localhost } 49Tuesday, 1 November 11
  50. 50. JMS Plugin Example Send messages import javax.jms.Message class SomeController { def jmsService def someAction = { jmsService.send(service: initial, 1) { Message msg -> msg.JMSReplyTo = createDestination(service: reply) msg } } } 50Tuesday, 1 November 11
  51. 51. JMS Plugin Example Listen for messages class ListeningService { static expose = [jms] def onMessage(message) { assert message == 1 } } 51Tuesday, 1 November 11
  52. 52. Demo Demo 52Tuesday, 1 November 11
  53. 53. Summary Various options for integrating Grails with: • Development/build • Deployment processes Works with many external systems • Solid support for non-Grailsy DB schemas • Flexible messaging & web service support 53Tuesday, 1 November 11
  54. 54. More info w: http://grails.org/ f: http://grails.org/Mailing+Lists e: pledbrook@vmware.com t: pledbrook b: http://blog.springsource.com/author/peter-ledbrook/ 54Tuesday, 1 November 11
  55. 55. Q&A Q&A 55Tuesday, 1 November 11

×