Why	
  Grails?	
  

  Yiguang	
  Hu	
  
What	
  is	
  Grails?	
  
•  Grails	
  is	
  a	
  web	
  framework	
  based	
  on	
  
    –  JAVA,	
  GROOVY,	
  Spring,	
  GORM	
  
•  Scaffolding!	
  
•  Plugin	
  everything!!!	
  
    –  GWT,	
  DWR,	
  YUI,	
  ACEGI,	
  Google	
  AppEngine,	
  Google	
  
       Wave,	
  COMET,	
  JQuery,	
  Clojure,	
  Scala,	
  JSF,	
  Struts…	
  
•  Support	
  all	
  modern	
  browsers	
  automaRcally	
  
•  Render	
  object	
  to	
  JSON,	
  XML	
  free!	
  
•  MVC+IOC(services)+plugin	
  
Why	
  Grails?	
  
•  Rapid	
  
    –  Have	
  your	
  next	
  Web	
  2.0	
  project	
  done	
  in	
  weeks	
  instead	
  of	
  
       months.	
  Grails	
  delivers	
  a	
  new	
  age	
  of	
  Java	
  web	
  applicaRon	
  
       producRvity.	
  
•  Dynamic	
  
    –  Get	
  instant	
  feedback,	
  see	
  instant	
  results.	
  Grails	
  is	
  the	
  
       premier	
  dynamic	
  language	
  web	
  framework	
  for	
  the	
  JVM.	
  
•  Robust	
  
    –  Powered	
  by	
  Spring,	
  Grails	
  out	
  performs	
  the	
  compeRRon.	
  
       Dynamic,	
  agile	
  web	
  development	
  without	
  compromises.	
  
•  Proven	
  Technologies:	
  JAVA(Groovy),	
  Spring,	
  GORM	
  
What	
  is	
  Groovy?	
  
•  Groovy	
  =	
  
    –  JAVA+Dynamic	
  Typing+Closure+	
  
        DSL,	
  GPars,Griffon….	
  
•  Runs	
  on	
  JVM	
  


• 0      	
  Learning	
  curve	
  for	
  Java	
  programmer	
  
Success	
  Stories	
  
•  The	
  Sites	
  I	
  personally	
  involved:	
  
    –  Worthpoint.com	
  (80	
  million	
  records)	
  
    –  Gsword:	
  h_p://www.ccimweb.org/gsword	
  
        •  Web	
  2.0.	
  Highly	
  interacRve.	
  
        •  Done	
  in	
  two	
  days!	
  
    –  Others	
  
Demo	
  
•  Install	
  java,	
  groovy,	
  grails	
  
•  grails	
  create-­‐app	
  interop	
  
    –  Goto	
  dir	
  interop	
  and	
  see	
  the	
  dires/files	
  created	
  
•  Run	
  it!	
  
•  Add	
  domain	
  
•  Generate	
  controller/views	
  and	
  run	
  
    –  SorRng,	
  paginaRon	
  are	
  there	
  already!	
  
•  Add	
  a	
  service	
  and	
  use	
  in	
  controller	
  
•  Run	
  it!	
  
Demo…	
  
•  SOA	
  
    –  Render	
  objects	
  as	
  JSON,	
  XML	
  etc	
  
    –  Import	
  grails.converters.*;	
  
•  Parallel	
  compuRng	
  in	
  Services	
  using	
  GPars	
  
•  Using	
  other	
  languages	
  like	
  Clojure	
  in	
  grails	
  
•  …	
  
Performance	
  Concern?	
  
•    User	
  Java	
  in	
  high	
  demanding	
  place	
  
•    Gpars	
  (Actors,	
  map	
  reducer…)	
  
•    Use	
  clojure	
  in	
  services	
  
•    User	
  Scala	
  in	
  services	
  

•  Yes.	
  It	
  is	
  AGILE!	
  
Clojure	
  Demo	
  
•  grails	
  install-­‐plugin	
  clojure	
  
•  New	
  dir	
  src/clj	
  is	
  created	
  
•  Add	
  file	
  Addnumber.clj	
  in	
  src/clj	
  
        (ns	
  grails)	
  
        (def	
  twenty	
  20)	
  
        (defn	
  add_numbers	
  [a	
  b]	
  
        	
  	
  	
  	
  (+	
  a	
  b))	
  
Call	
  Clojure	
  from	
  Service	
  
class	
  ClojService	
  {	
  
	
  	
  	
  	
  boolean	
  transacRonal	
  =	
  false	
  
	
  	
  	
  	
  def	
  addNumbers(x,	
  y)	
  {	
  
	
  	
  	
  	
  clj.add_numbers(x,	
  y)	
  
	
  	
  	
  	
  }	
  
	
  	
  	
  def	
  addTwenty(x)	
  {	
  
	
  	
  	
  clj.add_numbers(x,	
  clj.twenty)	
  
	
  	
  	
  }	
  
}	
  
Call	
  Service	
  in	
  Controller	
  
import	
  grails.converters.XML;	
  
class	
  PlayController	
  {	
  
	
  	
  	
  	
  def	
  index	
  =	
  {	
  }	
  
	
  	
  def	
  clojService	
  
def	
  cljaddnumber={	
  
	
  	
  	
  	
  	
  	
  	
  render	
  clojService.addNumbers(Integer.parseInt(params.a?:"5"),	
  
                        Integer.parseInt(params.b?:"4"))	
  
	
  	
  }	
  
	
  	
  def	
  cljadd20={	
  
	
  	
  	
  	
  render	
  clojService.addTwenty(Integer.parseInt(params.a?:"10"))	
  
	
  	
  }	
  
}	
  
Gpars	
  Map	
  Reducer	
  Demo	
  
//@Grab(group='org.codehaus.gpars',	
  module='gpars',	
  version='0.9')	
  
import	
  staRc	
  groovyx.gpars.Parallelizer.*	
  
class	
  ConcurrentService	
  {	
  
	
  	
  	
  	
  boolean	
  transacRonal	
  =	
  true	
  
	
  	
  	
  	
  def	
  sqr()	
  {	
  
	
  	
  	
  	
  	
  	
  def	
  myNumbers	
  
	
  	
  	
  	
  	
  	
  doParallel{	
  
	
  	
  	
  	
  	
  myNumbers	
  =	
  (1..1000).parallel.filter{it	
  %	
  2	
  ==	
  0}.map{Math.sqrt	
  
                        it}.collecRon	
  
	
  	
  	
  	
  	
  	
  }	
  
	
  	
  	
  	
  	
  	
  myNumbers	
  
	
  	
  	
  	
  }	
  
Gpars	
  Map	
  Reducer	
  Demo	
  
def	
  search(String	
  key,keys){	
  
	
  	
  	
  	
  	
  key+":"+	
  keys.replaceAll(key,key.toUpperCase())	
  	
  	
  	
  /turns	
  key	
  to	
  upper	
  case	
  
	
  	
  }	
  
	
  	
  def	
  webs=["a	
  b	
  c	
  d	
  e	
  f	
  g	
  h	
  j	
  k	
  i	
  o	
  as	
  da	
  o	
  sa	
  ds	
  h	
  vcx	
  aw	
  s	
  dsf	
  gdsg	
  	
  ds	
  ,	
  bbx	
  
                        fdwvcx	
  786	
  ",	
  "b	
  sd	
  sd	
  sd	
  dsfgh	
  fds	
  	
  ewr	
  56y	
  	
  k	
  o	
  aw	
  	
  n	
  q	
  	
  12	
  	
  g	
  	
  n	
  1	
  	
  	
  	
  	
  uy	
  t	
  r",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  "sad	
  assa	
  sa	
  sa	
  sa	
  sa	
  456t465367	
  45	
  45	
  23	
  23	
  12	
  	
  	
  453	
  43	
  5	
  3	
  234	
  523	
  ",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  "8sdf	
  ds	
  5ue	
  	
  we	
  	
  123	
  	
  42	
  hg	
  	
  h	
  k	
  	
  	
  s	
  x	
  d	
  f	
  	
  t	
  r	
  g	
  h	
  j	
  	
  y	
  k	
  v	
  	
  b	
  n	
  	
  m	
  l	
  qw	
  ew	
  c"]	
  
	
  	
  def	
  reducer(String	
  key)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  def	
  rst	
  
	
  	
  	
  	
  doParallel	
  {	
  
rst	
  =	
  webs.parallel.map	
  {search(key,it)}.collecRon	
  
	
  	
  	
  	
  }	
  
	
  	
  	
  	
  rst	
  
	
  	
  }}	
  

Why Grails?

  • 1.
    Why  Grails?   Yiguang  Hu  
  • 2.
    What  is  Grails?   •  Grails  is  a  web  framework  based  on   –  JAVA,  GROOVY,  Spring,  GORM   •  Scaffolding!   •  Plugin  everything!!!   –  GWT,  DWR,  YUI,  ACEGI,  Google  AppEngine,  Google   Wave,  COMET,  JQuery,  Clojure,  Scala,  JSF,  Struts…   •  Support  all  modern  browsers  automaRcally   •  Render  object  to  JSON,  XML  free!   •  MVC+IOC(services)+plugin  
  • 3.
    Why  Grails?   • Rapid   –  Have  your  next  Web  2.0  project  done  in  weeks  instead  of   months.  Grails  delivers  a  new  age  of  Java  web  applicaRon   producRvity.   •  Dynamic   –  Get  instant  feedback,  see  instant  results.  Grails  is  the   premier  dynamic  language  web  framework  for  the  JVM.   •  Robust   –  Powered  by  Spring,  Grails  out  performs  the  compeRRon.   Dynamic,  agile  web  development  without  compromises.   •  Proven  Technologies:  JAVA(Groovy),  Spring,  GORM  
  • 4.
    What  is  Groovy?   •  Groovy  =   –  JAVA+Dynamic  Typing+Closure+   DSL,  GPars,Griffon….   •  Runs  on  JVM   • 0  Learning  curve  for  Java  programmer  
  • 5.
    Success  Stories   • The  Sites  I  personally  involved:   –  Worthpoint.com  (80  million  records)   –  Gsword:  h_p://www.ccimweb.org/gsword   •  Web  2.0.  Highly  interacRve.   •  Done  in  two  days!   –  Others  
  • 6.
    Demo   •  Install  java,  groovy,  grails   •  grails  create-­‐app  interop   –  Goto  dir  interop  and  see  the  dires/files  created   •  Run  it!   •  Add  domain   •  Generate  controller/views  and  run   –  SorRng,  paginaRon  are  there  already!   •  Add  a  service  and  use  in  controller   •  Run  it!  
  • 7.
    Demo…   •  SOA   –  Render  objects  as  JSON,  XML  etc   –  Import  grails.converters.*;   •  Parallel  compuRng  in  Services  using  GPars   •  Using  other  languages  like  Clojure  in  grails   •  …  
  • 8.
    Performance  Concern?   •  User  Java  in  high  demanding  place   •  Gpars  (Actors,  map  reducer…)   •  Use  clojure  in  services   •  User  Scala  in  services   •  Yes.  It  is  AGILE!  
  • 9.
    Clojure  Demo   • grails  install-­‐plugin  clojure   •  New  dir  src/clj  is  created   •  Add  file  Addnumber.clj  in  src/clj   (ns  grails)   (def  twenty  20)   (defn  add_numbers  [a  b]          (+  a  b))  
  • 10.
    Call  Clojure  from  Service   class  ClojService  {          boolean  transacRonal  =  false          def  addNumbers(x,  y)  {          clj.add_numbers(x,  y)          }        def  addTwenty(x)  {        clj.add_numbers(x,  clj.twenty)        }   }  
  • 11.
    Call  Service  in  Controller   import  grails.converters.XML;   class  PlayController  {          def  index  =  {  }      def  clojService   def  cljaddnumber={                render  clojService.addNumbers(Integer.parseInt(params.a?:"5"),   Integer.parseInt(params.b?:"4"))      }      def  cljadd20={          render  clojService.addTwenty(Integer.parseInt(params.a?:"10"))      }   }  
  • 12.
    Gpars  Map  Reducer  Demo   //@Grab(group='org.codehaus.gpars',  module='gpars',  version='0.9')   import  staRc  groovyx.gpars.Parallelizer.*   class  ConcurrentService  {          boolean  transacRonal  =  true          def  sqr()  {              def  myNumbers              doParallel{            myNumbers  =  (1..1000).parallel.filter{it  %  2  ==  0}.map{Math.sqrt   it}.collecRon              }              myNumbers          }  
  • 13.
    Gpars  Map  Reducer  Demo   def  search(String  key,keys){            key+":"+  keys.replaceAll(key,key.toUpperCase())        /turns  key  to  upper  case      }      def  webs=["a  b  c  d  e  f  g  h  j  k  i  o  as  da  o  sa  ds  h  vcx  aw  s  dsf  gdsg    ds  ,  bbx   fdwvcx  786  ",  "b  sd  sd  sd  dsfgh  fds    ewr  56y    k  o  aw    n  q    12    g    n  1          uy  t  r",                      "sad  assa  sa  sa  sa  sa  456t465367  45  45  23  23  12      453  43  5  3  234  523  ",                      "8sdf  ds  5ue    we    123    42  hg    h  k      s  x  d  f    t  r  g  h  j    y  k  v    b  n    m  l  qw  ew  c"]      def  reducer(String  key)  {                def  rst          doParallel  {   rst  =  webs.parallel.map  {search(key,it)}.collecRon          }          rst      }}