google appengine (gae/j) - introduction and overview from a java guy
Post on 17-Oct-2014
963 views
DESCRIPTION
You know Java, but what is AppEngine? In this session Max will walk you from a brief introduction of Servlets to an overview of the Google AppEngine. We learn about the basics such as the data store – which is quite different from SQL -, application versions, back-ends, scheduler, instances and logging. Services such as mail, url fetch, task queue are also explained. Since we use GAE/J since 2010 in Calpano we also talk about costs and experience in practice.TRANSCRIPT
Dr. Max Völkel, Calpano 1
You know Java, but what is AppEngine? In this session Max will walk you from a brief introducAon of Servlets to an overview of the Google AppEngine. We learn about the basics such as the data store – which is quite different from SQL -‐, applicaAon versions, back-‐ends, scheduler, instances and logging. Services such as mail, url fetch, task queue are also explained. Since we use GAE/J since 2010 in Calpano we also talk about costs and experience in pracAce.
Poll § Language? § Java? § AppEngine? § Servlets?
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 2
Do you know Servlets? § HttpServlet
• void doGet( HttpServletRequest req, "HttpServletResponse res);"
• void doPost(…)"§ HttpServletRequest
• get Header(…), get InputStream(...)"§ HttpServletResponse
• set Header(…), getOutputStream(...)" See: http://java.sun.com/products/servlet/, AppEngine supports version 2.5"
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 3
AppEngine is a Servlet Container
§ Servlet Container runs a Web Application Archive (WAR) • Class files • Static resources • web.xml"
§ In AppEngine also: • appengine-web.xml, queue.xml, cron.xml, backends.xml, indexes.xml, …
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 4
AppEngine Architecture�Java/Python/Go
§ Running Code § Storing Data
• JVM • Memcache • Data store (Old vs. High
Replication) § Connect to the outside world
• Mail (Incoming, Outgoing) • Urlfetch
§ Delay or schedule work • Taskqueue • Cron
§ More helper services • Images • Users
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 5
Servlet Container
JVM
Data Store Memcache Images
HTTP, Incoming Mail, Cron, Taskqueue
Urlfetch, Outgoing Mail, Taskqueue
Hello AppEngine § Typical first test: • Register, Verify mobile number via SMS • Create new app “helloworld”
• http://helloworld.appspot.com
§ Next step: • Buy domain name • Configure …
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 6
1. Appspot.com > Create App
2. Go to “Apps for your Domain”�https://www.google.com/a/calpano.com
3. Organization & Users > Create user�! Only those can send emails
4. Domain Settings > �Domain names > �Add domain name or alias >
5. Go to appspot.com and “Add Domain”
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 7
Application Versions § http://{version}.{appid}.appspot.com"§ http://{appid].appspot.com �à main version (Selected in Dashboard)
§ All versions • Share the same datastore, memcache (!) • Different logs, stats, instances
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 8
Running Code § Java 6 VM (experimental Java 7) § No Files (only read the ones you put in your .war) § Explicit whitelist which Java classes are allowed (many!)
• https://developers.google.com/appengine/docs/java/jrewhitelist
§ F1: 8 cent/hour • 128MB memory (!), • 600MHz CPU
§ F2 = F1 * 2; F4 = F1 * 4 § ! Servlet response max. time is 60 seconds
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 9
Storing Data § JVM • Great when “always on”
§ Memcache • Really just a cache
§ Data store • Old: Master-Slave (2 data centers) • High Replication (3 data centers)
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 10
Connect to the outside world § Mail • Incoming
• Send as a servlet request • Outgoing
• Via Java Mail API, only some headers used • ! Bounce processing
• http://code.google.com/p/googleappengine/issues/detail?id=1800
§ Urlfetch • ! Calling Twitter API etc.
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 11
Delay or schedule work § Taskqueue Queue queue = QueueFactory.getDefaultQueue();!queue.add(TaskOptions.Builder.withDefaults()!.payload(task));!
• Task is a DeferredTask (has a run() method) • Very quick to enqueue task • Task queue service fires requests as
HttpRequests at defined rates, unwraps task, runs it
• Max runtime: 10 minutes § Cron • Scheduled GET-requests at defined urls
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 12
More helper services § Images • Looks like a subset of ImageMagick
§ Users • Log in via Google Account
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 13
Where is a request going? Typical setup § DNS lookup: CNAME or A-record to Google § GAE scheduler: • Request queue for your app • Boot new instance? Takes ca. 10-30 seconds.
§ On instance: • Run WAR, • Run servlet
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 14
Instances
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 15
Time is Money. What time? § Free vs. Payment enabled § Quotas • Usage – Free = Billable à Pay • Resource quota à Fair use limit à Error page
§ Data store • See also
http://blog.xam.de/2011/11/analysis-of-datastore-operation-cost.html
§ Instance hours • Usage (code time * complexity + 15 min) • What???
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 16
Real time vs. API time
§ CPU-intensive code is more expensive • Is magically speed-up by AppEngine
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 17
The Scheduler Appspot > App Settings > Performance
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 18
Appstats
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 19
More stuff § Backends • Requests run for hours (or even days?)
§ Logging • Java.util.logging runs fine
§ Blobstore § Channel ~ like websockets § XMPP § Remote API: • Call services remotely
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 20
Practical Tips on Datastore § Don’t use JDO abstraction • Leaky abstraction anyway
§ Low-level API quite easy • Objectify (open source) makes using it nice • Xydra.org adds versioning/synchronisation
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 21
Real-World Experiences § Xydra § Calpano.com § SonicMetrics.com
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 22
Servlet Container
JVM
Data Store Memcache Images
HTTP, Incoming Mail, Cron, Taskqueue
Urlfetch, Outgoing Mail, Taskqueue
When to use AppEngine? § Best: Spikes in demand § OK: Outsource server management
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 23
Where to learn more? § Next talk from Florian Krupicka on
Continuous Integration with AppEngine and Maven
§ Google webseits – Docu, JavaDocs, Articles, § Stackoverflow – Googles official developer
support moved there § Books • Programming Google AppEngine – was OK when
it came out, but now too outdated § Book a workshop from Calpano Consulting J
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 24
Dr. Max Völkel
25
Servlet Container
JVM
Data Store Memcache Images
HTTP, Incoming Mail, Cron, Taskqueue
Urlfetch, Outgoing Mail, Taskqueue
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com
Contact Information § Dr. Max Völkel § [email protected] § +49 171 8359678
§ www.Calpano.com
GDG Karlsruhe DevFest 2012 | Dr. Max Völkel | Calpano.com 26