Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Groovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume Laforge
1. Groovy in the Enterprise:
Case Studies
Guillaume Laforge
VP Technology
G2One, Inc.
glaforge@g2one.com
2. Guillaume Laforge
Groovy Project Manager
• Spec Lead of JSR-241
Initiator of the Grails framework
Co-author of Groovy in Action
VP Technology at G2One
• The Groovy / Grails company
• Training, support, consulting
Evangelizing Groovy, Grails and DSLs
• JavaOne, JavaPolis, QCon, JAX, Sun TechDays...
3. Goal of this talk
Discover real-world Groovy
usage in the Enterprise
to better understand:
•How you can leverage Groovy
in your own environment
•How to integrate Groovy
in your applications
4. Agenda
About Groovy and Grails
Groovy usage patterns
Integrating Groovy in your applications
Case studies
5. About Groovy and Grails
Groovy, a dynamic language for the JVM
Grails, an agile web application framework
6. Groovy is...
The fastest dynamic language for the JVM
• that integrates seamlessly with Java
without any impedance mismatch
An Apache-licensed Open Source project
• successful project hosted at Codehaus
Aiming at simplifying the life of developers
• by bringing expressiveness and productivity boosts
• by borrowing good ideas from other languages
An innovative and creative project
7. Java-like on steroids
Syntax derived from the Java 5 grammar
• Flat learning curve for Java developers
• Supports both static and dynamic typing
Support Java 5 features
• Annotations, generics, static imports, enums...
• Sole dynamic language to support this!
GInterface
JInterface
Real full Java / Groovy interop <<implements>>
<<implements>>
JClass
GClass
• Joint compiler
• or can be evaluated on the fly JClass GClass
8. A Java program
import java.util.List;
import java.util.ArrayList;
class Erase {
private List filterLongerThan(List strings, int length) {
List result = new ArrayList();
for (int i = 0; i < strings.size(); i++) {
String s = (String) strings.get(i);
if (s.length() <= length) {
result.add(s);
}
}
return result;
}
public static void main(String[] args) {
List names = new ArrayList();
names.add(quot;Tedquot;);
names.add(quot;Fredquot;);
names.add(quot;Jedquot;);
names.add(quot;Nedquot;);
System.out.println(names);
Erase e = new Erase();
List shortNames= e.filterLongerThan(names, 3);
System.out.println(shortNames.size());
for (inti= 0; i< shortNames.size(); i++) {
String s = (String) shortNames.get(i);
System.out.println(s);
}
}
}
9. A Groovy program
import java.util.List;
import java.util.ArrayList;
class Erase {
private List filterLongerThan(List strings, int length) {
List result = new ArrayList();
for (int i = 0; i < strings.size(); i++) {
String s = (String) strings.get(i);
if (s.length() <= length) {
result.add(s);
}
}
return result;
}
public static void main(String[] args) {
List names = new ArrayList();
names.add(quot;Tedquot;);
names.add(quot;Fredquot;);
names.add(quot;Jedquot;);
names.add(quot;Nedquot;);
System.out.println(names);
Erase e = new Erase();
List shortNames= e.filterLongerThan(names, 3);
System.out.println(shortNames.size());
for (inti= 0; i< shortNames.size(); i++) {
String s = (String) shortNames.get(i);
System.out.println(s);
}
}
}
14. Groovy usage patterns
A tool in the developer toolbox
A full stack web application framework
An extension point in your application
Domain-Specific Languages
15. Pattern: Developer tool
Great support for unit
testing and mock objects
• Nice way to introduce Groovy in a project
Shell scripting reusing all your JARs
• Easy to control Ant task for custom builds
Template engine for code generation needs
Excellent XML parsing and creation support
Easy JDBC for import/export database scripts
16. Pattern: CoC web app development
Convention over Configuration
• Productive in minutes with scaffolding
• No useless configuration, focus on what matters
• Dir. layout, naming conventions, transparent wiring...
Grails = Groovy + Spring + Hibernate + ...
• Groovy is the glue to write
your views (Groovy Server Pages)
your controllers
your services
your domain classes
17. Pattern: Application extension point
Customize or extend your application at
extension points through Groovy scripting
Create plugins adding new functionality
Add / Update business rules at runtime
• See also Domain-Specific Languages
Personalize your reporting screens
• With Groovy templates
Remote introspection of your app
• Embed a remote Groovy shell
18. Pattern: Domain-Specific Language
Use a more expressive language
• than a general purpose language
Share a common metaphore between
developers and subject matter experts
Domain experts can help write the rules!
Avoid boilerplate technical code
Cleanly seperate business logic
from application plumbing code
19. Integrating Groovy
in your applications
JSR-223, one API to rule them all
Spring dynamic beans
Groovy’s own mechanisms
20. JSR-223: javax.script.*
One API to rule them all
Groovy engine JAR at scripting.dev.java.net
• drop it in your classpath
ScriptEngineManager mgr =
new ScriptEngineManager();
ScriptEngine eng =
mgr.getEngineByName(“Groovy”);
String result =
(String)eng.eval(“‘foo’*2”);
21. Spring 2 dynamic language beans
Spring 2 provides support for alternative
language bean definitions & implementations
• POGOs can be wired, proxied, injected in POJOs
Configuration with the <lang:*> namespace
• <lang:groovyid=’bean’
script-source=’classpath:com.foo.GBean’
customizer-ref=’specialMetaClass’/>
Groovy beans can be “refreshed”
22. Groovy’s own mechanisms
Several integration mechanisms
• Eval, GroovyShell, GroovyScriptEngine
• def binding = new Binding()
binding.mass = 22.3
binding.velocity = 10.6
def shell = new GroovyShell()
def expr = “mass * velocity ** 2 / 2”
assert shell.evalute(expr) == 1252.814
GroovyClassLoader for more advanced
integration scenario
25. LinkedIn
Main site
• Java / Tomcat / Spring / Hibernate / custom MVC
But their corporate solutions are in Grails
• Private portals for recruiters, for premium customers
with focused needs
Why Grails?
• Needed a more productive webapp framework with
rapid prototyping capabilities
• Needed deep integration with their Java backend
custom session, reuse business services, SSO
26. showbiz
Biggest UK satellite
broadcaster
• also known as BSkyB
• owned by News Corp.
Developed their showbiz website on Grails
• 186+ million page views per month
• “Grails just scales” ™
Currently rewriting their main portal in Grails
27. Groovy as a Developer Tool
Patterson Institute for Cancer Research
French Ministry of Justice
Canoo WebTest
28. Patterson Institute for Cancer Research
Manchester University / Cancer Research UK
Groovy in two projects
• X:Map: a Genome browser
using a 54GB tileset for Google Maps
• exonmap: microarrays analysis program
Groovy used to
• Fetch latest genome sequencing information (FTP)
• Homogenize data sources
• Scan databases, extrapolate and filter data
29. Code generation
Groovy was used as a developer tool
• but no line of Groovy in production code
Groovy XML
Parsers
XMI
UML
Groovy
Template
Engine
30. Canoo WebTest
Open Source tool for automating testing
of web applications
invoke “http://google.com”
verifyTitle “Google”
setInputField name: ‘q’,
value: ‘WebTest’
clickButton “I’m feeling lucky”
verifyTitle “Canoo WebTest”
31. Groovy as a Language for
Application Extension Points
CodeStreet Market Data Studio
Hyperic HQ
32. codestreet
Market Data Works simplifies
• capturing, auditing
• editing Reuters market data
Traders can use Groovy
• modify market data feeds
• record and replay feeds
• test evolutionary scenario
34. Hyperic HQ: open source web infrastructure
monitoring and management suite
• used in Spring Application Management Suite
Big Groovy-based plugin infrastructure
• script deployments, server reboots, threshold alerts,
resources monitoring, etc...
• agent side: custom handling of monitored resources
• also embeds a Groovy console for interactive work
• plugins updatable / reloadable at runtime
36. Mutual of Omaha
US Fortune 500 insurance company
Risk calculation engine in Groovy
part of a mission-critical application
50,000 lines of Groovy code
• half business rules, half test code
Module part of a large EJB-based application
Choice of Groovy
• Business rules readability, maintenance by IT and
Subject Matter experts, seamless Java integration
38. National Cancer Institute
Cancer registry management
• Organizes large amounts of medical records
• JBoss / Oracle / Hibernate / Struts application
Groovy used in several places
• As an architecture validation system — dev. tool
ensure proper layer seperation, if not, fail the build
• Param validation for customizing reporting screens
• Business rules to edit and validation medical records
validity of fields, of set of fields, of records
check / fix / improve the patient files
700k patient files * 1,300 edits
39. Nuclear safety organization
Scientific context, with intensive
and complex computation needs
Matlab/Mathematica-like DSL on top of super
efficient Java custom math library
• Thin Groovy DSL layer
• Enjoyed a math-like syntax for matrices, seamless
integration with Java
• Groovy’s operator overloading mechanism
• Friendlier to scientists and mathematicians
• Application targets 200-300 engineers & researchers
40. Human Resources DSL
Architecture / Agile consultancy
• Needed a way to track consultant skills & knowledge
pay raises, finding the right person for the gig, etc...
Developed a DSL to represent skills
• Textual DSL transformed into a treemap-like graphics
• Integrated in Confluence as a plugin
the plugin parses and generates an in-memory model
a servlet renders graphics to embed in the wiki pages
a Confluence macro renders everything on the wiki page
• Leverages Confluence’s search capabilities
41. Human Resources DSL
etre {
idees {
capture 1
formule 1
produit 1
}
organisation {
controle 1
abandonne 1
aligne 1
}
engagement {
euros 1
gens 1
idees 1
enseigner 1
}
}
faire {
...
}
43. Summary
Groovy is a successful, mature and
performant dynamic language for the JVM
Provides several interesting usage patterns
• Developer tool, CoC webapp development,
application extension mechanism,
DSLs & business rules externalization
Used in production for mission-critical
applications, and integrated in many
frameworks