java tech & tools | grails in the java enterprise | peter ledbrook

55
Grails in the Java Enterprise Peter Ledbrook / VMware Tuesday, 1 November 11

Upload: jax-london

Post on 10-May-2015

1.624 views

Category:

Technology


2 download

DESCRIPTION

2011-11-01 | 03:00 PM - 03:50 PMWith 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.

TRANSCRIPT

Page 1: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Grails in the Java Enterprise

Peter Ledbrook / VMware

Tuesday, 1 November 11

Page 2: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

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)

2

Tuesday, 1 November 11

Page 3: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Grails

What is Grails?

3

Build

Web MVC GSP (Views)

GORM(Data Access)

Doc Engine

Servlet Container

Test Support

Database I18n

Tuesday, 1 November 11

Page 4: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Grails

What is Grails?

4

Tuesday, 1 November 11

Page 5: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

What is Grails?

5

Web ControllersThe Domain Model

Business Logic Custom View Tags

Views & LayoutsLibraries (JARs)

Additional Sources

Web Resources

i18n bundles

Build Commands

Tests

Tuesday, 1 November 11

Page 6: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Say bye-bye to the plumbing!

6

Tuesday, 1 November 11

Page 7: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Demo

Demo

7

Tuesday, 1 November 11

Page 8: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Enterprise requirements

8

Web App

Messaging

Legacy Databases Services

JEE

Is this a problem for Grails apps?

Tuesday, 1 November 11

Page 9: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Integration Points

BuildDependenciesDatabaseDeploymentSpring

9

Tuesday, 1 November 11

Page 10: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Integration Points

BuildDependenciesDatabaseDeploymentSpring

10

Tuesday, 1 November 11

Page 11: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Build

11

Tuesday, 1 November 11

Page 12: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Build

12

Remember the Grails project structure?• add in build events and...

Can’t build natively with other build tools!

Grails Build System

Ant GradleMaven

Tuesday, 1 November 11

Page 13: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

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

13

Tuesday, 1 November 11

Page 14: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Maven

Maven Grails Plugin: https://github.com/grails/grails-mavenUse Maven 2 or 3 to build Grails projectsDeclare dependencies in POMWorks for both applications and plugins!Integration test framework:

https://github.com/grails/grails_maven_plugin_testing_tests

14

Tuesday, 1 November 11

Page 15: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Getting Started

mvn archetype-generate ...

mvn initialize

Set MAVEN_OPTS

Optional: add ‘pom true’ to dependency DSL

e.g. -Xmx256m -XX:MaxPermSize=256m

15

Tuesday, 1 November 11

Page 16: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Packaging Types

‘war’• Must configure execution section• Works with plugins that depend on ‘war’

‘grails-app’• Less configuration

‘grails-plugin’• For plugins!

16

Tuesday, 1 November 11

Page 17: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Maven & Grails Plugins

> grails release-plugin

==

> mvn deploy

17

Tuesday, 1 November 11

Page 18: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Maven & Grails Plugins

<dependency> <groupId>org.grails.plugins<groupId> <artifactId>hibernate</artifactId> <type>grails-plugin</type></dependency>

Either:

Use ‘mvn deploy’ or Release plugin!

18

And ‘pom: false’

Tuesday, 1 November 11

Page 19: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Maven & Grails Plugins

grails.project.dependency.resolution = { ... plugins { compile ":hibernate:1.3.6" } ...}

Or:

19

Tuesday, 1 November 11

Page 20: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Customise the Build

Create new commands in <proj>/scriptsPackage the commands in a plugin!Create <proj>/scripts/_Events.groovy

• Interact with standard build steps• Add test types• Configure embedded Tomcat

20

Tuesday, 1 November 11

Page 21: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

What the future holds...

Grails 3.0 will probably move to Gradle• More powerful and more flexible• Standard, well documented API• Ant & Maven support

21

Tuesday, 1 November 11

Page 22: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Integration Points

BuildDependenciesDatabaseDeploymentSpring

22

Tuesday, 1 November 11

Page 23: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Dependencies are JARs

Use any Java library you like!Full support for Maven-compatible repositoriesDeclarative dependenciesPlugins can be declared the same way

23

Tuesday, 1 November 11

Page 24: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Dependency DSL

grails.project.dependency.resolution = { inherits "global" log "warn" repositories { grailsHome() mavenCentral() mavenRepo "http://localhost:8081/..." } ...}

24

Tuesday, 1 November 11

Page 25: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

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" } ...}

25

Tuesday, 1 November 11

Page 26: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Integration Points

BuildDependenciesDatabaseDeploymentSpring

26

Tuesday, 1 November 11

Page 27: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

‘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

27

Tuesday, 1 November 11

Page 28: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

class Book { ... static mapping = { table "books" title type: "books" author column: "author_ref" }}

28

No existing domain modelSchema not too far off the beaten track

Option 1: Custom ORM mapping

Tuesday, 1 November 11

Page 29: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Option 2: JPA annotations

<?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>

29

Existing Java/JPA domain model

grails-app/conf/hibernate/hibernate.cfg.xml

Tuesday, 1 November 11

Page 30: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

<?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>

30

You have Java model + Hibernate mapping filesSchema is way off the beaten track

Option 3: Hibernate XML Mappings

grails-app/conf/hibernate/hibernate.cfg.xml

Tuesday, 1 November 11

Page 31: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

constraints = { title blank: false, unique: true ...}

ConstraintsGiven domain class:

Then:

org.example.myapp.domain.Book

src/java/org/example/myapp/domain/BookConstraints.groovy

31

Tuesday, 1 November 11

Page 32: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

GORM layer over JPAUse your own JPA providerUseful for cloud services that only work with JPA, not

Hibernate

32

Option 4: GORM JPA Plugin

Tuesday, 1 November 11

Page 33: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Option 5: Remote service back-endDon’t have to use GORMUse only controllers and services

• Grails services back onto remote services

33

Web App

Invoice Log Service ...

SOAP, RMI, HTTP Invoker, etc.

Tuesday, 1 November 11

Page 34: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Share your model!

34

Tuesday, 1 November 11

Page 35: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Database Management

35

?

Hibernate ‘update’+

Production data=

Tuesday, 1 November 11

Page 36: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Database Migration Plugin

36

Pre-production, Hibernate ‘update’ or ‘create-drop’

dbm-generate-changelogdbm-changelog-sync

Change domain model

dbm-gorm-diffdbm-update

Tuesday, 1 November 11

Page 37: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Reverse Engineering Plugin

37

class Person { String name Integer age ...}

Tuesday, 1 November 11

Page 38: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Database Management

38

Database Migration

http://grails.org/plugin/database-migration

Reverse Engineering

http://grails.org/plugin/database-migration

Tuesday, 1 November 11

Page 39: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Integration Points

BuildDependenciesDatabaseDeploymentSpring

39

Tuesday, 1 November 11

Page 40: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

grails war

Build properties:• grails.war.copyToWebApp• grails.war.dependencies• grails.war.resources• grails.project.war.file

40

Tuesday, 1 November 11

Page 41: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Control of JARsgrails.project.dependency.resolution = { defaultDependenciesProvided true inherits "global" log "warn" ...}

grails war --nojars => WEB-INF/lib/<empty>

=> No Grails JARs in WEB-INF/lib

41

Tuesday, 1 November 11

Page 42: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Data Source

dataSource { jndiName = "java:comp/env/myDataSource"}

JNDI:

dataSource { url = System.getProperty("JDBC_STRING")}

System property:

42

Tuesday, 1 November 11

Page 43: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Data Source

grails.config.locations = [ "file:./${appName}-config.groovy", "classpath:${appName}-config.groovy" ]

Config.groovy:

For run-app: ./<app>-config.groovy

For Tomcat: tomcat/lib/<app>-config.groovy

43

Tuesday, 1 November 11

Page 44: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Integration Points

BuildDependenciesDatabaseDeploymentSpring

44

Tuesday, 1 November 11

Page 45: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Grails is Spring

Spring MVC under the hoodGrails provides many useful beans

• e.g. grailsApplicationDefine your own beans!

• resources.xml/groovy• In a plugin

45

Tuesday, 1 November 11

Page 46: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Exampleimport ...beans = { credentialMatcher(Sha1CredentialsMatcher) { storedCredentialsHexEncoded = true }

sessionFactory(ConfigurableLocalSessionFactoryBean) { dataSource = ref("dataSource") hibernateProperties = [ "hibernate.hbm2ddl.auto": "create-drop", "hibernate.show_sql": true ] }}

46

Tuesday, 1 November 11

Page 47: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Enterprise Integration

Spring opens up a world of possibilities• Spring Integration/Camel• Messaging (JMS/AMQP)• ESB• RMI, HttpInvoker, etc.

Web services & REST

47

Tuesday, 1 November 11

Page 48: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Grails Plugins

RoutingJMS, RabbitMQCXF, Spring-WS, WS-ClientREST

48

Tuesday, 1 November 11

Page 49: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

JMS Plugin Example

49

dependencies { compile 'org.apache.activemq:activemq-core:5.3.0'}

Add any required dependencies (BuildConfig.groovy)

jmsConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) { brokerURL = 'vm://localhost'}

Configure JMS factory (resources.groovy)

Tuesday, 1 November 11

Page 50: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

JMS Plugin Example

50

import javax.jms.Message

class SomeController { def jmsService

def someAction = { jmsService.send(service: 'initial', 1) { Message msg -> msg.JMSReplyTo = createDestination(service: 'reply') msg } }}

Send messages

Tuesday, 1 November 11

Page 51: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

JMS Plugin Example

51

class ListeningService { static expose = ['jms']

def onMessage(message) { assert message == 1 }}

Listen for messages

Tuesday, 1 November 11

Page 52: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Demo

Demo

52

Tuesday, 1 November 11

Page 53: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

SummaryVarious 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

53

Tuesday, 1 November 11

Page 54: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

More info

w: http://grails.org/f: http://grails.org/Mailing+Lists

e: [email protected]: pledbrookb: http://blog.springsource.com/author/peter-ledbrook/

54

Tuesday, 1 November 11

Page 55: Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook

Q&A

55

Q & A

Tuesday, 1 November 11