ZK MVVM, Spring & JPA On Two PaaS Clouds


Published on

Published in: Technology
1 Like
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

ZK MVVM, Spring & JPA On Two PaaS Clouds

  1. 1. ZK, Spring & JPA On Two PaaS Clouds(Java MVVM Adventures Featuring Heroku & Openshift) Simon Massey
  2. 2. OverviewWhat is PaaS "Cloud" and why we should care?Background to the ZkToDo2 sample appOutline of the MVVM PatternWalk through of MVVM sample page (new ZK6 features!)Quick pass of the Spring & JPA internalsThe key ingredient: maven build profilesLive push of source to two clouds! (Fingers crossed!)
  3. 3. Clouds (In One Slide)Clouds come "as a service" (aaS) Software aaS Infrastructure aaS Platform aaSSaaS is your data on the cloud with leased software (they do everything)IaaS is leased managed hardware (they can do the network)PaaS is leased application/database capacity (they keep it running whilst you write the app)
  4. 4. The PaaS PropositionServer virtualization is very mainstream. High density host servers reduce overheadsFailover, off-site backup, standby servers, spare parts, upgrades, network is divided by number of virtual serversRent a "virtual private server" (VPS) and you have to set it up and care for it yourself (logs filling up your disks?)How do you source 0.2 system adminstrators for your app? And if they change jobs?Outsource this to a PaaS provider!
  5. 5. The PaaS Proposition (2)A big provider with a big server farm has automated deployment "elastic platform"Need more power? "Click here Sir to approve the monthy credit card payments..."Choice is a double edged sword; standardization is the key to maintaining a large estate of applicationsBig cloud farms run thousands of servers and are evolving best practice at a high rateRunning your own servers is very last century; you can offload that work to the experts
  6. 6. Is Your PaaS RIA Friendly?Not all PaaS clouds are AJAX friendly. Some assume that you are hosting a blog (i.e. stateless full page reload app)Not all PaaS clouds provide a relational database. ZK apps tend to be data orientated desktop-like apps which need an RDBMSJ2EE is "complex" but PHP and Ruby are "simple"(??). Java sat outside the party whilst Ruby On Rails danced up a storm… but now the big Rails clouds are doing Java and are making it look very easy!
  7. 7. InterludeCode https://github.com/simbo1905/ZkToDo2Where is it running?Redhat Openshift zktd2-zkdemo.rhcloud.comHeruko glowing-light-1070.herokuapp.com(note: apps may suspend when idle so may take a long time to start on first hit)What IDE, DB, etc?
  8. 8. The Code In ContextThe "ZK ToDo 2" sample app has the same screen implimented three times; using MVP, MVC and ZK6 MVVM (aka MVB) patternsThree ZK articles document the different patterns in the code MVP (2007), MVC (2009), MVVM (2011). MVVM article is out of date as sample code now does ZK6 "ZK Bind" MVVMSample code has moved to the zktodo2 repo on GitHub with an Apache2 licenseThe code now deploys to Heroku or Openshift PaaS clouds!
  9. 9. About The CodeThe "ZkToDo2" sample app (aka zktd2) is a simple database CRUD app written with ZK, Spring and JPACode uses a relational DB and has configurations for HSQL, MySQL and PostreSQL databasesSpring instantiates the objects and wires them together when the ZUL page "asks for" a spring bean due to XML databindingsLeverages SpringMVC scopes and ZKSpring for elegant care free coding
  10. 10. The MVVM Screen....
  11. 11. … is all databindings! Your ClickMe! 1 Java 2ZK Web ZKDesktop Bind 4 3Saved! this.msg="Saved!"
  12. 12. Model-View-ViewModel View == Web Desktop Binder <<reflection>> Legend ViewModel compiles to command DomainModel load
  13. 13. The ViewModel Class[show code] org.zkforge.zktodo2.ZkToDoViewModelHolds the List<Reminder> the user is working withHolds the selectedItem Reminder the user is currently editingUses the ReminderService which updates the databaseHas the add(), save(), delete() methodsCan be shared by different viewsThe view is active; "@bind" has AJAX updates write to the selectedItem Reminder
  14. 14. Data BindingsPage load calls getReminders on toDoViewModel and renders each date in a list:<window apply="org.zkoss.bind.BindComposer" viewModel="@id(vm) @init(toDoViewModel2)"><listbox model="@load(vm.reminders)" selectedItem="@bind(vm.selectedReminder)"><template name="model" var="reminder"><listitem> <listcell label="@load(reminder.date)"/></listitem></template</listbox></window>
  15. 15. Data Bindings 2When the user selects a different reminder in the list let the user edit it in the edit panel:Date:<datebox value="@bind(vm.selectedReminder.date)" />The vm.selectedReminder is refered to in multiple places. It is written to by the onSelect Event of the Listbox:<listbox model="@load(vm.reminders)" selectedItem="@bind(vm.selectedReminder)">ZKBind keeps track what it changes and reloads UI with changed state
  16. 16. Command BindingsNew in ZK6!<button label="Save" onClick="@command(save)"/>There is zero code to read/write data between screen and the JPA entites: binder is updating them over AJAXsave() only calls save(selectedReminder) or delete(selectedReminder)The binder is a generic UI ControllerZK Bind updates UI + Model + EntitiesYou just write very testable Java!
  17. 17. Less Boiler Plate CodeJava methods which change state have hints as to what to reload into the screen:@Command@NotifyChange({"reminders","selectedReminder"})public void delete() { .... }Annotations on the Reminder entity setters and on the ViewModel CRUD methodsDesktop "zul" is bound onto the ViewModelCould make "admin screen" & "read-only screen" for the same ViewModel (else "touch screen" & "mouse screen")
  18. 18. Spring Bean XML Definitions...ViewModel bean has "desktop" scope <bean id="toDoViewModel2" class="org.zkforge.zktodo2.ZkToDoViewModel" p:reminderService-ref="reminderService" scope="desktop" />So only one bean per web desktop (you may have many desktops per http session)... has singleton reminder service: <bean id="reminderService" class="org.zkforge.zktodo2.ReminderService"p:basicDao-ref="basicDao" />… has singleton basicDao: <bean id="basicDao" class="org.zkforge.zktodo2.BasicDao"/ >
  19. 19. … Stable Since 2007Same BasicDao for other patterns demo screensThese days you dont need the Spring XML. Spring has alternative way to specificy how to wire up the beans (with Java annotations)CDI would not be hard at all (see zkcdi library)Back-end code is "orthodox" allowing us to focus on the code and whats new; ZK Patterns… and deploying onto different Clouds!
  20. 20. ZK Cloud "Bake Off"Two leading cloud providers chosen scientifically and at random (google+beer)Each backed by a major software company (Salesforce vs Redhat)Are they opensource like ZK, do they run it well, and are they easy to use...?
  21. 21. What Do You Get? Feature Heroku Openshift Deploy from Yes. Git + Maven Yes. Git + Maven source?[1] Database? PostgreSQL MySQL Container? Jetty7.4 JBossAS7.0 (embedded) (standalone) Proprietary No problems for No problems for Gotchas?[2] the sample app the sample app[1] With Git you commit locally and "push" all the commits to one or more repos. One remote repo is the cloud; it compiles+deploys[2] ”Can I move my production app at any time to my own servers?”
  22. 22. The Maven Build ProfilesDevelop and debug on the jetty-maven-plugin ”mvn jetty:run” then deploy to JBoss, Weblogic, Websphere or Tomcat when it is ready to user testWhen in JUnit or debugging (jetty:run) Spring is configured to setup the resources (datasource, jms)To run in JBossAS7 Spring is configured to pull in the platform resources (jndi-datasource, jndi-jms)Maven build profiles swap the Spring configuration based on a "-P" parameter using webapp "overlays"Write once deploy to any cloud: mvn -P "openshift" package mvn -P "cloudX" package
  23. 23. Live Deploy DemoThis could be wishful thinking...![p.s. All went well, we committed a change to the screen locally and with a single command deployed to each of the two cloud services]
  24. 24. … And The Winner Is ...?Both clouds are lightening fast for ZK AJAX!Both have fully opensourced stacks!Both are oh-so-easy-to-use
  25. 25. ReferencesThe code https://github.com/simbo1905/ZkToDo2ZK6 MVVM introduction Hello ZK MVVMMVC article Desktop MVC Patterns ZK, Spring & JPAOriginal MVP article SmallTalks 2008 Best MVC PatternsBook on how to build a layered domain model with Spring POJOs In ActionMartin Fowler GUI Patterns pages UI ArchitecturesJosh Smiths inspirational Microsoft .Net MVVM Article