Appengine Nljug


Published on

An introduction to Google AppEngine. This presentation was made for a NLJUG university event.

Published in: Technology
  • Be the first to comment

Appengine Nljug

  1. 1. Google AppEngine Paul Bakker
  2. 2. About me Paul Bakker • Trainer Info Support Hibernat EJB 3 e • NLJUG speaker JSF Flex • Java Magazine author Seam Groovy JavaFX Grails Spring
  3. 3. About Info Support • Financieel gezond sinds oprichting 1986 • Focus op IT vakmanschap • Brede en integrale dienstverlening • Partnerships met o.a. Microsoft, IBM, NLJUG • Vestigingen in Nederland en België - 250+ medewerkers in Nederland - 25 medewerkers in België
  4. 4. Presentation Goal Introduction to Developing applications for AppEngine
  5. 5. Outline AppEngine Cloud Services offerings Using Introducing frameworks AppEngine Pricing Developing and deploying Storing data
  6. 6. Clouds N’ stuff
  7. 7. Infrastructure as a Service Platform as a Service Software as a Service
  8. 8. Infrastructure as a Service A virtual hardware platform Full control of the operating system Custom configuration of application servers
  9. 9. Infrastructure as a Service Pros It’s just works like your old hardware No development limitations No lock-in Cons You still have to manage servers No automatic scaling
  10. 10. Platform as a Service A virtual application server No control of the operating system Running in a sandbox
  11. 11. Platform as a Service Pros No more server management Easy deployment Automatic scaling Cons Limited by the sandbox Can’t run existing apps
  12. 12. Software as a Service Run an application from the cloud Might extend the application using a proprietary API
  13. 13. AppEngine Intro
  14. 14. AppEngine architecture
  15. 15. Handling requests Each application gets a free domain name You can add your own domain name Each request has a 30 second timeout Streaming responses are not supported
  16. 16. Scaling JVMs are started on demand on app startup after inactivity when upscaling Starting a JVM causes a “loading request” the app is loaded during that request
  17. 17. Loading request optimization Application startup time is now important the app might be started after a request Reduce the use of libraries Prevent heavy initialization code share data with multiple JVMs using MemCache
  18. 18. The sandbox You can’t Write to the file system Open sockets Start threads Invoke most System calls Reflect on classes not belonging to the app Use JRE classes not on the white list
  19. 19. Supported Java Data Objects (JDO) Java Persistence API (JPA) Java Server Faces (JSF) Java Server Pages (JSP) and JSTL Java Servlet API 2.4 JavaBeans Activation Framework (JAF) Java Architecture for XML Binding (JAXB) JavaMail XML processing APIs including DOM, SAX and XSLT
  20. 20. Not Supported Enterprise Java Beans (EJB) JAX-WS and JAX-RPC JDBC Java EE Connector Architecture (JCA) Java Management Extensions (JMX) Java Message Service (JMS) Java Naming and Directory Interface (JNDI) Remote Method Invocation (RMI)
  21. 21. Creating and uploading an application
  22. 22. Storing Data
  23. 23. DataStore Distributed architecture Highly scalable Not relational Schemaless Properties have one or more values
  24. 24. Sharded architecture Row name Values A [...] B [...] C [...] Row name Values D [...] E [...] F [...] Row name Values G [...] H [...] I [...]
  25. 25. Entity storage Kind => table Key => primary key Entity Group => partition Typed properties => columns Kind Person Entity Group /Person:Paul Key /Person:Paul name string: Paul Bakker colleagues Key:/Speaker(68) Key:/Speaker(15)
  26. 26. Entity Groups Entities are hierarchical Useful for “owned” relations a chapter can’t exist without a book /Book(24)/Chapter(2)/Paragraph(3)
  27. 27. Queries Supported filters < less than <= less than or equal to = equal to > greater than >= greater than or equal to != not equal to
  28. 28. Indexes Each query uses an index One index for each combination of: kind filter property operator sort order
  29. 29. Index example JPQL queries select s from Speaker s order by s.firstname, s.lastname select s from Speaker s WHERE s.firstname LIKE :firstname order by s.firstname, s.lastname select s from Speaker s WHERE lastname LIKE :lastname order by s.lastname, s.firstname Index configuration
  30. 30. Transactions The DataStore is transactional All DataStore write operations are atomic Only a single Entity Group can be used within a transaction entities in the same group live physically together
  31. 31. Transactions
  32. 32. Transactions Begin transaction
  33. 33. Transactions Begin transaction Create new Speaker (/Speaker(1))
  34. 34. Transactions Begin transaction Create new Speaker (/Speaker(1)) Add Talk (/Speaker(1)/Talk(1))
  35. 35. Transactions Begin transaction Create new Speaker (/Speaker(1)) Add Talk (/Speaker(1)/Talk(1)) Add new Speaker (/Speaker(2))
  36. 36. Transactions Begin transaction Create new Speaker (/Speaker(1)) Add Talk (/Speaker(1)/Talk(1)) Add new Speaker (/Speaker(2)) Commit transaction
  37. 37. Transactions Begin transaction Create new Speaker (/Speaker(1)) Add Talk (/Speaker(1)/Talk(1)) Commit transaction Begin transaction Add new Speaker (/Speaker(2)) Commit transaction
  38. 38. Transaction Isolation Within a transaction: Serializable transactions are completely isolated Outside a transaction: Read Committed get() will only consist of committed data queries are more subtle
  39. 39. Understand commits A commit() consists of two milestones 1 changes to entities visible 2 changes to indexes visible the entity is updated, but might not be returned by a predicate query
  40. 40. Understand commits bob.age == 18 Transaction 1 bob.age += 1 select ... where age = 18 Transaction 2
  41. 41. Understand commits bob.age == 18 Transaction 1 bob.age += 1 select ... where age = 18 Transaction 2 bob is returned
  42. 42. Understand commits bob.age == 18 Transaction 1 bob.age += 1 select ... where age = 18 Transaction 2 bob is returned bob.age == 19
  43. 43. The low level API JDBC like API Work directly with untyped entities and properties Query using GQL
  44. 44. Creating entities
  45. 45. Queries
  46. 46. GQL SELECT [* | __key__] FROM <kind> [WHERE <condition> [AND <condition> ...]] [ORDER BY <property> [ASC | DESC] [, <property> [ASC | DESC] ...]] [LIMIT [<offset>,]<count>] [OFFSET <offset>] <condition> := <property> {< | <= | > | >= | = | != } <value> <condition> := <property> IN <list> <condition> := ANCESTOR IS <entity or key>
  47. 47. GQL
  48. 48. Setting up JPA Add JPA and DataNucleus jars Create persistence.xml Configure post-compilation class enhancement
  49. 49. Persistence Unit
  50. 50. EntityManagerFactory
  51. 51. Creating an Entity
  52. 52. JPA limitations No “Joined” inheritance No “Single Table” inheritance would be technically possible No many-to-many relationships No join-queries No aggregation queries No polymorphic queries
  53. 53. Owned relationships The “owned” entity only has meaning when it’s parent exists Key includes parent key /Department(24)/Employee(2)
  54. 54. Owned relations
  55. 55. Owned relations
  56. 56. Polymorphic relations Throws exception!
  57. 57. Using JPA
  58. 58. Unowned relations No referential checks!
  59. 59. Unowned relations Both entities can exist without the other an employee and it’s colleagues Cannot be used within the same transaction
  60. 60. Loading collections
  61. 61. Many-to-many GAE doesn’t support JPA many-to-many Work around by mapping key collections
  62. 62. Unowned collections
  63. 63. Constraining pages Uses Google Accounts Require users to log-in Require the admin role admin == developer on GAE
  64. 64. Require to login Require admin role
  65. 65. Login and logout Login Logout
  66. 66. Using Google Accounts
  67. 67. Services Mail Users Queus MemCache Blob Store Cron Jobs
  68. 68. Mail service
  69. 69. Receiving mail !   Mail can be send to! POST! AppEngine! MyServlet! /_ah/mail/address!
  70. 70. Cron Jobs Tasks that run automatically on specific times or intervals sending a week report nightly cleanup Executing a job is invoking a URL
  71. 71. Configuring jobs Configure job in WEB-INF/cron.xml
  72. 72. Cron syntax every N (hours|mins|minutes) ("every"|ordinal) (days) ["of" (monthspec)] (time) Examples every 5 minutes every 12 hours 2nd,third mon,wed,thu of march 17:00 every monday 09:00 1st monday of sep,oct,nov 17:00 every day 00:00
  73. 73. Cron jobs
  74. 74. Task Queues Process asynchronous, parallel background tasks Modeled as web hooks a request to a web hook is scheduled the request handler is just like any other user request a web hook has data and code
  75. 75. Task queue use cases Send multiple confirmation emails each email is sent by a request to the same task with different data Place order send confirmation email (task 1) send notification to admin (task 2)
  76. 76. Adding tasks /notification
  77. 77. Configuring queues WEB-INF/queue.xml rate: throttling task execution bucket-size: “token-bucket” algorithm
  78. 78. Task Queues
  79. 79. Saving files Store files up to 50 mb in the BlobStore Upload by form POST file is uploaded to the BlogStore get a blob key in code Serve file using blob key
  80. 80. Upload file
  81. 81. Serving files
  82. 82. Listing blobs
  83. 83. BlobStore
  84. 84. MemCache Distributed in-memory cache e.g. cache frequent queries JCache API (JSR 107) Low level API
  85. 85. Using a cache Cache expires after 3600 miliseconds
  86. 86. Memcache
  87. 87. Frameworks on AppEngine
  88. 88. Frameworks A lot of frameworks work “mostly” but you never know if/when it breaks... JSF 1.2 e.g. RichFaces is not! JSF 2 disable threads Grails using a plugin Weld multiple issues JAX-WS EJB Spring some modules are not JMS
  89. 89. Testing
  90. 90. Unit Testing Easy out of container unit testing In-memory DataService, MailService, MemcacheService, TaskQueue, AuthenticationService etc.
  91. 91. Setup local datastore
  92. 92. Create datastore instance
  93. 93. Use datastore API directly
  94. 94. Using JPA
  95. 95. TaskQueue testing (setup)
  96. 96. TaskQueue testing (test)
  97. 97. Unit Testing
  98. 98. Costs
  99. 99. Quotas Free Default Quota Billing Enabled Default Quota Resource Daily Limit Maximum Rate Daily Limit Maximum Rate 7,400 requests/ 43,000,000 30,000 requests/ Requests 1,300,000 requests minute requests minute Outgoing 56 megabytes/ 1 gigabyte free; 10 gigabytes/ Bandwidth (billable, 1 gigabyte 1,046 gigabytes minute minute includes HTTPS) maximum Incoming 56 megabytes/ 1 gigabyte free; 10 gigabytes/ Bandwidth (billable, 1 gigabyte 1,046 gigabytes minute minute includes HTTPS) maximum 15 CPU-minutes/ 6.5 CPU-hours free; 72 CPU-minutes/ CPU Time (billable) 6.5 CPU-hours 1,729 CPU-hours minute minute maximum
  100. 100. Pricing Resource Unit Unit cost Outgoing gigabytes $0.12 Bandwidth Incoming gigabytes $0.10 Bandwidth CPU Time CPU hours $0.10 gigabytes per Stored Data $0.15 month Recipients recipients $0.0001 Emailed
  101. 101. Setting a budget
  102. 102. The verdict Deployment can’t get any easier Sandbox can be a show stopper Apps should be developed specifically for AppEngine
  103. 103. Google AppEngine Paul Bakker