56
How to build your Java app and go online in 1 hour Google App Engine by example

Google app engine by example

Embed Size (px)

DESCRIPTION

A guide to create a simple Java application and upload it to the Google Cloud Platform with Google App Engine. This presentation covers usage of persistence API with both Google Cloud SQL and Google Cloud Datastore.

Citation preview

  • 1. Google App Engine by example

2. About me Linked[in]: ua.linkedin.com/in/invaa/ e-mail: [email protected] 3. Google App Engine PaaS 264k websites (258k is active)URL: http://trends.builtwith.com/hosting/Google-App-Engine Coca-cola, Ubisoft, Rovio, BestBuy, Sony Music, ...URL: https://cloud.google.com/customers/ 4. Demo Tic Tac Toe using Google Endpoints Features: API Web frontend Android client Deployed:https://cloudendpointstic.appspot.com Sources:https://github.com/GoogleCloudPlatform 5. PlanGet to know the Google App Engine (GAE) while makinga simple jQuery Calendar (aka Google Calendar killer)and put the app in the Google Cloud. 6. Calendar app Events with persistence in the Google Cloud Start End Title Description Color Attenders Add events Move events 7. Requirements and Prerequisites JDK 1.7 (recommended) Apache Maven 3.1+ MySQL 5+ (for local dev server) Google account Application ID IDEMore:https://cloud.google.com/appengine/docs/java/gettingstarted/setup 8. TODO V1: Project creation and structure V2: Spring MVC V3: View with jQuery Calendar V4: Persistence with Cloud SQL V5: Persistence with Cloud Datastore V6: Attenders and colors V7: update events on drop More: https://github.com/invaa/CloudCalendar 9. Cloud Calendar Service v1 Create project structure Run locally 10. v1. Getting application ID URL: https://appengine.google.com/start 11. v1. Project creation mvn archetype:generate -Dappengine-version=1.9.13 -Dapplication-id=your-app-id -Dfilter=com.google.appengine.archetypes: Google Cloud Console 12. v1. Project structure You'll add your own applicationJava classes to src/main/java/... You'll configure your applicationusing thefile src/main/webapp/WEB-INF/appengine.web.xml You'll configure your applicationdeployment using thefile src/main/webapp/WEB-INF/web.xml 13. v1. Dependencies. Template appengine-api-1.0-sdk servlet-api Jstl appengine-testing appengine-api-stubs 14. The Java Development Server jetty-6.1.x (built-in) The development web server simulates the App EngineJava runtime environment and all of its services,including the datastore More:https://cloud.google.com/appengine/docs/java/tools/devserver 15. Admin console http://localhost:port/_ah/admin 16. Cloud Calendar Service v2 Spring MVC Calendar controller getPages() and / mapping Upload Calendar to GAE 17. v2. Dependencies. Spring MVC org.springframework spring-webmvc ${spring.version} 18. v2. Spring MVC. Controller getPages() and / 19. v2. Uploading App mvn appengine:update More:https://cloud.google.com/appengine/docs/java/tools/uploadinganapp 20. Dev Console https://console.developers.google.com/ 21. Appcfg appengine-java-sdkbinappcfg.cmd [options] More:https://cloud.google.com/appengine/docs/java/tools/uploadinganapp?hl=ru#Uploading_the_App 22. Cloud Calendar Service v3 Button jQuery Calendar Upload Calendar to GAE 23. v3. Spring MVC. View 24. v3. Spring MVC. Viewbody {margin: 0;padding: 0;font-family: "Lucida Grande", Helvetica, Arial, Verdana, sans-serif;font-size: 14px;}#script-warning {display: none;background: #eee;border-bottom: 1px solid #ddd;padding: 0 10px;line-height: 40px;text-align: center;font-weight: bold;font-size: 12px;color: red;}#calendar, #container {max-width: 900px;margin: 40px auto;padding: 0 10px;} 25. v3. Create event formlabel, input {display: block;}input.text {margin-bottom: 12px;width: 95%;padding: .4em;}fieldset {padding: 0;border: 0;margin-top: 25px;}div#users-contain {width: 350px;margin: 20px 0;}div#users-contain table {margin: 1em 0;border-collapse: collapse;width: 100%;}div#users-contain table td, div#users-containtable th {border: 1px solid #eee;padding: .6em 10px;text-align: left;}.ui-dialog .ui-state-error {padding: .3em;}.validateTips {border: 1px solid transparent;padding: 0.3em;} 26. v3. Create event formfunction updateTips(t) {tips.text(t).addClass("ui-state-highlight");setTimeout(function () {tips.removeClass("ui-state-highlight", 1500);}, 500);}function checkLength(o, n, min, max) {if (o.val().length > max || o.val().length < min) {o.addClass("ui-state-error");updateTips("Length of " + n + " must be between " +min + " and " + max + ".");return false;} else {return true;}}function checkRegexp(o, regexp, n) {if (!( regexp.test(o.val()) )) {o.addClass("ui-state-error");updateTips(n);return false;} else {return true;}}function addEvent() {//}//dialog codeform =dialog.find("form").on("submit",function (event) {event.preventDefault();});$("#create-event").button().on("click", function () {dialog.dialog("open");}); 27. Add some persistencyI am CRUD! 28. Datastore and RDBMS 29. Cloud Calendar Service v4 Model Persistence 30. v4. Cloud SQL Spring Data EclipseLink JPA Provider 31. v4. Cloud SQL. Dependenciesmysqlmysql-connector-java5.1.25org.eclipse.persistenceorg.eclipse.persistence.jpa2.4.2org.springframework.dataspring-data-jpa${spring.data.version}com.fasterxml.jackson.corejackson-databind2.3.2 32. v4. Cloud SQL. Contextorg.eclipse.persistence.jpa.PersistenceProvidercom.calendar.service.model.Eventtruefalse 33. v4. Cloud SQL. Other @Temporal(TemporalType.TIMESTAMP) true 34. v4. Spring MVC. Model Event Repository 35. v4. Cloud SQL. Deployment Create DB (dev vs production) More:https://code.google.com/apis/console/b/0/?noredirect&pli=1#project:920231937966:sql 36. Cloud Calendar Service v5 Persistence with Google Datastore Upload Calendar to GAE 37. v5. Cloud Datastore DataNucleus DataNucleus App Engine Plugin Spring Data More:https://cloud.google.com/appengine/docs/java/datastore/jpa/overview 38. v5. Dependencies. Cloud Datastoreorg.datanucleusjavax.persistence2.1.0com.google.appengine.ormdatanucleus-appengine${datanucleus-appengine.version}org.apache.geronimo.specsgeronimo-jpa_2.0_spec1.1providedorg.datanucleusdatanucleus-core${datanucleus.version}org.datanucleusdatanucleus-api-jpa${datanucleus.version} 39. v5. Plugins. Enhance entitiesorg.datanucleusdatanucleus-maven-plugin4.0.0-releaseJPAjpa.unit${basedir}/log4j.propertiestrueorg.datanucleusdatanucleus-core3.1.1process-classesenhance 40. v4. Cloud SQL. Contextorg.datanucleus.api.jpa.PersistenceProviderImpl 41. v5. Spring-data and Cloud Datastore Repository 42. Cloud Calendar Service v6 Attenders and colors Upload Calendar to GAE 43. v6. Attenders and colors Colored events Attenders entity OneToMany relation 44. v6. Color picker

45. v6. Color picker#red, #green, #blue {float: left;clear: left;width: 300px;margin: 15px;}#swatch {width: 120px;height: 100px;margin-top: 18px;margin-left: 350px;background-image: none;}#red .ui-slider-range { background: #ef2929; }#red .ui-slider-handle { border-color: #ef2929; }#green .ui-slider-range { background: #8ae234; }#green .ui-slider-handle { border-color: #8ae234; }#blue .ui-slider-range { background: #729fcf; }#blue .ui-slider-handle { border-color: #729fcf; } 46. v6. Color pickerfunction hexFromRGB(r, g, b) {var hex = [r.toString( 16 ),g.toString( 16 ),b.toString( 16 )];$.each( hex, function( nr, val ) {if ( val.length === 1 ) {hex[ nr ] = "0" + val;}});return hex.join( "" ).toUpperCase();}function refreshSwatch() {var red = $( "#red" ).slider( "value" ),green = $( "#green" ).slider( "value" ),blue = $( "#blue" ).slider( "value" ),hex = hexFromRGB( red, green, blue );$( "#swatch" ).css( "background-color", "#" + hex );}$(function() {$( "#red, #green, #blue").slider({orientation: "horizontal",range: "min",max: 255,value: 127,slide: refreshSwatch,change: refreshSwatch});$( "#red" ).slider( "value", 255 );$( "#green" ).slider( "value", 140);$( "#blue" ).slider( "value", 60 );}); 47. Cloud Calendar Service v7 Update on drop Upload Calendar to GAE 48. v7. Update on drop eventDrop: function (event, delta, revertFunc) { //update in db $.getJSON("${pageContext.request.contextPath}/updateEventOnDrop", { id: event.id, start: event.start.format() }, function (data) { }) .done(function () { }) .fail(function () { }) .complete(function () { }); } 49. An App Engine application cannot write to the filesystem. Applications must use the AppEngine datastore for storing persistent data. Readingfrom the filesystem is allowed, and all application filesuploaded with the application are available. respond slowly. A web request to an application mustbe handled within a few seconds. Processes that take avery long time to respond are terminated to avoidoverloading the web server. make other kinds of system calls. 50. JRE White list https://cloud.google.com/appengine/docs/java/jrewhitelist 51. web.xml Features Not Supported App Engine supports the element for servletdeclarations. However, the load actually occurs during the firstrequest handled by the web server instance, not prior to it. Some deployment descriptor elements can take a humanreadable display name, description and icon for use in IDEs. AppEngine doesn't use these, and ignores them. App Engine doesn't support JNDI environment variables (). App Engine doesn't support EJB resources (). Notification of the destruction of servlets, servlet context, orfilters is not supported. The element is ignored. Servlet scheduling with is not supported. 52. Further reading https://cloud.google.com/appengine/ https://github.com/tommysiu/spring-data-gae/blob/v0.1/README.md https://cloud.google.com/developers/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore/ http://www.datanucleus.org/products/datanucleus/jpa/maven.html https://code.google.com/p/datanucleus-appengine/wiki/Compatibility http://www.loop81.com/2013/02/gae-google-app-engine-jpa2-maven-and.html http://gae-java-persistence.blogspot.com/ http://www.datanucleus.org/products/datanucleus/jpa/enhancer.html 53. Q&A Thank you!