52
Software Development Done Right Modern Web Applica0on Development with Java EE 7 By Shekhar Gula0 1

Modern web application development with java ee 7

Embed Size (px)

DESCRIPTION

Modern web application development with java ee 7

Citation preview

Page 1: Modern web application development with java ee 7

Software Development Done Right

Modern  Web  Applica0on  Development  with    

Java  EE  7  

By  Shekhar  Gula0  

1

Page 2: Modern web application development with java ee 7

Software Development Done Right

Who  am  I  –  Shekhar  Gula0  

•  Currently  working  @  Xebia  •  Polyglot  programmer  •  Previously  OpenShiG  Evangelist  @  Red  Hat  •  Author  of  OpenShiG  Cookbook  •  TwiLer  handle  @  shekhargula0  

2

Page 3: Modern web application development with java ee 7

Software Development Done Right

Agenda  

•  Showcase  miles2run  •  Defining  modern  web  applica0on  •  How  we  built  this  applica0on  using  Java  EE  7  

– GeOng  started  with  Java  EE  7  – Technology  Stack  – Cloud  deployment  op0ons  –  Java  EE  7  limita0ons  and  challenges  

•  Going  polyglot  persistence  

3

Page 4: Modern web application development with java ee 7

Software Development Done Right

How  it  all  started?  

•  A  cool  Java  EE  7  demo  applica0on  •  A  Java  EE  7  app  that  would  allow  users  to  store  their  running  ac0vi0es  

•  It  should  run  on  OpenShiG  •  It  should  use  modern  technologies  •  It  should  look  and  feel  modern  •  It  should  be  open  source  

4

Page 5: Modern web application development with java ee 7

Software Development Done Right

   

PetStore  or  CargoTracker  are  not  modern  real  applica0ons  

5

Page 6: Modern web application development with java ee 7

Software Development Done Right 6

https://github.com/miles2run/miles2run

Page 7: Modern web application development with java ee 7

Software Development Done Right 7

Modern Java Web Application

Page 8: Modern web application development with java ee 7

Software Development Done Right 8

Page 9: Modern web application development with java ee 7

Software Development Done Right

What  makes  a  modern  web  applica0on?  

•  Exposes  REST  JSON  web  services  •  Embraces  HTML  5    •  Single  page  web  applica0on  •  Stateless  so  that  you  can  scale  horizontally  •  Uses  Oauth  •  Embraces  Polyglot  Persistence  •  Responsive  •  Cloud  aware  •  Social  

Page 10: Modern web application development with java ee 7

Software Development Done Right

Some  op0ons  in  Java  world  

10

Page 11: Modern web application development with java ee 7

Software Development Done Right

Building  Miles2Run  –    Ini0al  Requirements  

•  Applica0on  that  can  store  daily  running  ac0vi0es  

•  Authen0ca0on  using  TwiLer  •  Responsive  web  applica0on  •  Each  user  should  have  a  public  profile  

11

Page 12: Modern web application development with java ee 7

Software Development Done Right

Technology  Stack  

•  Java  EE  7  with  JDK  7  –  JPA,  EJB,  CDI,  JAX-­‐RS  

•  SLF4J  •  MySQL  •  AngularJS  •  TwiLer  Bootstrap  •  WildFly  on  OpenShiG  

12

Page 13: Modern web application development with java ee 7

Software Development Done Right

Java  EE  7  

•  Standards  based  development  •  Portability  across  applica0on  servers  •  Lightweight  deployment  •  Easy  to  get  started  •  Java  EE  API  provides  a  good  star0ng  point  

– CDI,  JAX-­‐RS,  JPA  are  at  the  heart  of  any  modern  web  applica0on  

13

Page 14: Modern web application development with java ee 7

Software Development Done Right

Java  EE  7  

14

Page 15: Modern web application development with java ee 7

Software Development Done Right

LOOK  AT  FEW  JAVA  EE  API  

15

Page 16: Modern web application development with java ee 7

Software Development Done Right

CDI  1.1-­‐-­‐  Context  and  Dependency  Injec0on    •  Allows  you  to  use  dependency  injec0on  in  Java  EE  

environment  without  third  party  libraries.  •  Don’t  call  us.  We  will  call  you.  •  CDI  container  manages  the  life  cycle  of  components.  •  CDI  brings  dependency  injec0on,  context  and  scopes,  

interceptors,  loose  coupling  and  strong  typing.  •  Injected  bean  lifecycle  depends  on  the  target  bean.  

•  Turns  nearly  every  Java  class  into  CDI  bean  •  It  is  not  a  non  sta0c  inner  class  •  It  is  a  concrete  class  or  annotated  with  decorators  •  Has  default  constructor  with  no  parameters  or  declares  a  constructor  with  @Inject  

Page 17: Modern web application development with java ee 7

Software Development Done Right

CDI  1.1:  What’s  new  in  Java  EE  7?  

•  Finer  scanning  control  in  beans.xml  •  bean-­‐discovery-­‐mode  aLribute  –  all,  none,  annotated  

•  @Vetoed  annota0on  to  veto  processing  of  a  package  or  class.  

•  You  can  use  @Transac0onal  with  CDI  beans.  •  You  can  get  away  from  EJBs.  

Page 18: Modern web application development with java ee 7

Software Development Done Right

JPA    •  Describes  the  management  of  rela0onal  data  in  applica0ons.  •  JPA  implementa0ons  provide  ORM  framework  •  JPA  provides  an  API  to  perform  CRUD  and  find  opera0ons  on  

en00es.  •  An  en0ty  is  a  POJO,  whose  state  is  mapped  to  a  rela0onal  

database  table.  •  JPA  also  provides  a  query  language  to  make  queries  against  

en00es  stored  in  rela0onal  database.  

Page 19: Modern web application development with java ee 7

Software Development Done Right

JPA  2.1  –  What’s  new  in  Java  EE  7?  

•  Schema  genera0on  using  standard  proper0es.  

•  Use  @Index  to  define  addi0onal  indexes  in  schema  genera0on.  

•  New  En0tyGraph  API  available  in  En0tyManager.  

Page 20: Modern web application development with java ee 7

Software Development Done Right

Storing  data  in  UTC  

•  Store  all  the  dates  in  the  UTC  format  and  then  depending  on  the  client  0mezone  show  it  to  the  user  

20

Page 21: Modern web application development with java ee 7

Software Development Done Right

Bean  Valida0on  

•  Specifies  common  valida0on  concept  for  JavaBeans.  

•  Allow  you  to  put  constraints  on  data  to  make  sure  it  is  valid.  

•  Annota0on  based  constraints  •  Use  constraint  in  any  layer  

Page 22: Modern web application development with java ee 7

Software Development Done Right

Bean  Valida0on  Example  

public  class  Status{          @NotNull      @Size(max  =  140)          private  String  message;          @NotNull  @Past          private  final  Date  postedAt;          @UniqueUrl          private  String  url;          public  Status()  {}  }  

Page 23: Modern web application development with java ee 7

Software Development Done Right

Bean  Valida0on  1.1  –    What’s  new  in  Java  EE  7?  

•  Approaches  Programming  by  Contract  •  Can  inject  with  CDI  into  Validator  implementa0ons  •  Integra0on  with  JAX-­‐RS  

@Path(“/statuses”) public class StatusService { @POST @Produces(“application/json”) public @NotNull Status postStatus(@Valid @NotNull Status status) { // persist in database return status; } }

Page 24: Modern web application development with java ee 7

Software Development Done Right

JAX-­‐RS  

•  Java  API  for  RESTful  web  services  •  Annota0on  driven  API  that  aims  to  help  developers  build  RESTful  web  services  in  Java  

•  POJO  based  Resource  classes  •  HTTP  centric  

– Maps  HTTP  requests  to  Java  method  invoca0ons  

 

24

Page 25: Modern web application development with java ee 7

Software Development Done Right

JAX-­‐RS  Resource  

25

Page 26: Modern web application development with java ee 7

Software Development Done Right

What’s  new  in  Java  EE  7?  

•  Client  API  •  Asynchronous  JAX-­‐RS  services  •  Filters  and  interceptors  

26

Page 27: Modern web application development with java ee 7

Software Development Done Right

SLF4J  

•  Abstrac0on  or  façade  on  top  of  different  logging  API  

•  SLF4J  with  Logback  provides  a  faster  logging  implementa0on  

•  SLF4J  has  great  paLern  subs0tu0on  support.    logger.debug("Calcula0ng  Goal-­‐{}  progress  between  {}  and  {}",  goalId,  startDate,  endDate);    

 27

Page 28: Modern web application development with java ee 7

Software Development Done Right

AngularJS  

•  Extends  HTML  to  add  dynamic  nature  so  that  we  can  build  modern  web  applica0ons  with  ease  

•  Declara0ve  approach  •  Eliminates  DOM  manipula0on  by  two  way  data  binding  

•  Ideal  for  building  single  page  web  applica0ons  

28

Page 29: Modern web application development with java ee 7

Software Development Done Right

AngularJS  in  Ac0on  

29

Page 30: Modern web application development with java ee 7

Software Development Done Right

OpenShift is

PaaS by Red Hat

Multi-language, Auto-Scaling, Self-service,

Elastic, Cloud Application Platform

Page 31: Modern web application development with java ee 7

Software Development Done Right

CLOUD COMPUTING 101

COMPUTE RESOURCES (CPU, RAM, NETWORK, DISK)

VIRTUAL GUEST IMAGES

OPERATING SYSTEM

APPLICATION PLATFORM (App Server, Middleware, Languages)

APPLICATION

Automated and Managed by the Cloud Provider

Provided and Controlled by Cloud Consumer

IaaS PaaS SaaS

Increased Control

Increased Automation

DEVELOPER TOOLING (IDE, Source Control, Build Tools, CI)

Page 32: Modern web application development with java ee 7

Software Development Done Right

Why  OpenShiZ?  

l  Focus  on  code,  not  on  configura0on  l  Speeds  up  deployment  l  Scales  your  app  l  Efficient  l  Embraces  polyglot  programming  and  persistence.  

Page 33: Modern web application development with java ee 7

Software Development Done Right

Bringing  everything  together  

•  Start  by  crea0ng  WildFly  app  on  OpenShiG  •  OpenShiG  will  create  a  Maven  app  •  Clone  the  app  Git  repo  on  your  local  machine  •  Import  the  app  in  your  favorite  IDE  •  Write  domain  model  •  Write  EJB  services  that  uses  JPA  en0ty  manager  •  Expose  REST  API  •  Commit  the  code  to  local  repo  and  push  changes  to  remote  Git  repository  

33

Page 34: Modern web application development with java ee 7

Software Development Done Right

Challenges  

•  Rendering  HTML  5  pages  from  JAX-­‐RS  •  Use  JAX-­‐RS  as  MVC  framework  •  Servlet  containers  do  not  create  sessions  when  you  render  HTML  5  pages  

•  Your  whole  applica0on  is  not  a  single  SPA  •  Java  EE  has  no  support  for  OAuth  providers  like  TwiLer  

34

Page 35: Modern web application development with java ee 7

Software Development Done Right

To  overcome  some  of  these  challenges  

   I  wrote  a  small  framework  called  JUG  hLps://github.com/miles2run/jug    It  allows  me  to  render  HTML  5  pages  using  Thymeleaf  template  engine  Provides  basic  security  check  Enables  sessions  for  HTML  5  pages        

35

Page 36: Modern web application development with java ee 7

Software Development Done Right

JUG  Example  

36

Page 37: Modern web application development with java ee 7

Software Development Done Right

Why  Thymeleaf?  

•  Thymeleaf  documents  are  valid  HTML  5  documents  so  you  can  work  with  them  offline  

•  Extends  HTML  by  a  tag  library  •  All  thymeleaf  documents  are  valid  HTML  5  documents  

•  Allows  designers  and  developers  to  work  together  

37

createProfile.html

Page 38: Modern web application development with java ee 7

Software Development Done Right

Make  app  social  –  new  requirements  

•  Ability  to  follow  other  users  •  View  their  ac0vi0es  on  my  home  page      

38

Page 39: Modern web application development with java ee 7

Software Development Done Right

Redis  to  rescue  

•  REmote  DIc0onary  Server  •  Redis  is  an  in-­‐memory  key  value  data  store  •  Supports  persistence  •  Supports  rich  set  of  data  types  

– List,  Set,  Sorted  Set,  Hash  

39

Page 40: Modern web application development with java ee 7

Software Development Done Right

Redis  Set  for  storing  follower  and  following  

40

Page 41: Modern web application development with java ee 7

Software Development Done Right

Redis  update  0meline  with  follower  0meline  

41 TimelineRepository.java

Page 42: Modern web application development with java ee 7

Software Development Done Right

Redis  –  Storing  Ac0vi0es  

42 TimelineRepository.java

Page 43: Modern web application development with java ee 7

Software Development Done Right

Other  use  case  of  Redis  

•  Maintaining  applica0on  counters  •  Latest  ac0vi0es  on  user  home  page  •  Caching  ac0vi0es    

43

Page 44: Modern web application development with java ee 7

Software Development Done Right

Sugges0ng  users  –  another  requirement  

•  Sugges0ng  users  based  on  loca0on    

44

Page 45: Modern web application development with java ee 7

Software Development Done Right

MongoDB  to  rescue  

45

Page 46: Modern web application development with java ee 7

Software Development Done Right

Storing  GeoSpa0al  data  

46

Page 47: Modern web application development with java ee 7

Software Development Done Right

Querying  data  

47

Page 48: Modern web application development with java ee 7

Software Development Done Right

Loca0onBasedUserSuggester  

48

Page 49: Modern web application development with java ee 7

Software Development Done Right

Adding  beau0ful  graphs  

•  C3.js  for  crea0ng  graphs  •  Provides  API  to  write  D3  based  charts  •  No  need  to  work  with  D3  API  •  Examples  hLp://c3js.org/examples.html    

49

Page 50: Modern web application development with java ee 7

Software Development Done Right

Genera0ng  a  Donut  chart  

50

Page 51: Modern web application development with java ee 7

Software Development Done Right

Configuring  error  pages  

51

Page 52: Modern web application development with java ee 7

Software Development Done Right

THANKS  Q  &  A  

52