Upload
simbo1905
View
8.750
Download
3
Embed Size (px)
Citation preview
ZK, Spring & JPA On Two PaaS Clouds
(Java MVVM Adventures Featuring Heroku & Openshift)
Simon Massey
Overview
What is PaaS "Cloud" and why we should care?
Background to the ZkToDo2 sample app
Outline of the MVVM Pattern
Walk through of MVVM sample page (new ZK6 features!)
Quick pass of the Spring & JPA internals
The key ingredient: maven build profiles
Live push of source to two clouds! (Fingers crossed!)
Clouds (In One Slide)
Clouds come "as a service" (aaS)
Software aaS
Infrastructure aaS
Platform aaS
SaaS 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)
The PaaS Proposition
Server virtualization is very mainstream. High density host servers reduce overheads
Failover, off-site backup, standby servers, spare parts, upgrades, network is divided by number of virtual servers
Rent 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!
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 applications
Big cloud farms run thousands of servers and are evolving best practice at a high rate
Running your own servers is very last century; you can offload that work to the experts
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 RDBMS
J2EE 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!
Interlude
Code https://github.com/simbo1905/ZkToDo2
Where is it running?
Redhat Openshift zktd2-zkdemo.rhcloud.com
Heruko 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?
The Code In Context
The "ZK ToDo 2" sample app has the same screen implimented three times; using MVP, MVC and ZK6 MVVM (aka MVB) patterns
Three 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" MVVM
Sample code has moved to the zktodo2 repo on GitHub with an Apache2 license
The code now deploys to Heroku or Openshift PaaS clouds!
About The Code
The "ZkToDo2" sample app (aka zktd2) is a simple database CRUD app written with ZK, Spring and JPA
Code uses a relational DB and has configurations for HSQL, MySQL and PostreSQL databases
Spring instantiates the objects and wires them together when the ZUL page "asks for" a spring bean due to XML databindings
Leverages SpringMVC scopes and ZKSpring for elegant care free coding
The MVVM Screen....
… is all databindings!
ZKBind
ZK WebDesktop
ClickMe!
this.msg="Saved!"
12
4
YourJava
3Saved!
Model-View-ViewModel
Legendcompiles to
command
load
View == Web Desktop
Binder
ViewModel
DomainModel
<<reflection>>
The ViewModel Class
[show code] org.zkforge.zktodo2.ZkToDoViewModel
Holds the List<Reminder> the user is working with
Holds the selectedItem Reminder the user is currently editing
Uses the ReminderService which updates the database
Has the add(), save(), delete() methods
Can be shared by different views
The view is active; "@bind" has AJAX updates write to the selectedItem Reminder
Data Bindings
Page 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>
Data Bindings 2
When 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
Command Bindings
New 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 AJAX
save() only calls save(selectedReminder) or delete(selectedReminder)
The binder is a generic UI Controller
ZK Bind updates UI + Model + Entities
You just write very testable Java!
Less Boiler Plate Code
Java 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 methods
Desktop "zul" is bound onto the ViewModel
Could make "admin screen" & "read-only screen" for the same ViewModel (else "touch screen" & "mouse screen")
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"/>
… Stable Since 2007
Same BasicDao for other patterns demo screens
These days you don't 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 what's new; ZK Patterns
… and deploying onto different Clouds!
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...?
What Do You Get?
Feature Heroku Openshift
Deploy from source?[1]
Yes. Git + Maven Yes. Git + Maven
Database? PostgreSQL MySQL
Container? Jetty7.4 (embedded)
JBossAS7.0 (standalone)
Proprietary Gotchas?[2]
No problems for the sample app
No problems for 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?”
The Maven Build Profiles
Develop and debug on the jetty-maven-plugin ”mvn jetty:run” then deploy to JBoss, Weblogic, Websphere or Tomcat when it is ready to user test
When 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
Live Deploy Demo
This 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]
… And The Winner Is ...?
Both clouds are lightening fast for ZK AJAX!
Both have fully opensourced stacks!
Both are oh-so-easy-to-use
References
The code https://github.com/simbo1905/ZkToDo2
ZK6 MVVM introduction Hello ZK MVVM
MVC article Desktop MVC Patterns ZK, Spring & JPA
Original MVP article SmallTalks 2008 Best MVC Patterns
Book on how to build a layered domain model with Spring POJOs In Action
Martin Fowler GUI Patterns pages UI Architectures
Josh Smith's inspirational Microsoft .Net MVVM Article