google app engine overview - barcamp phnom penh 2011

41
Google App Engine (Google App Engine Overview) Barcamp Phnom Penh 2011 Phnom Penh, Cambodia Guy Flysher

Upload: traactivity

Post on 06-May-2015

533 views

Category:

Technology


1 download

DESCRIPTION

Google App Engine Overview - by Guy Flysher at BarCamp Phnom Penh 2011, Cambodia

TRANSCRIPT

Page 1: Google App Engine Overview - BarCamp Phnom Penh 2011

Google App Engine

(Google App Engine Overview)

Barcamp Phnom Penh 2011

Phnom Penh, Cambodia

Guy Flysher

Page 2: Google App Engine Overview - BarCamp Phnom Penh 2011

About me

● Developer in the Emerging markets team.

● Joined Google in 2007.

● Previously worked on Social graphs,Gmail and Google Accounts.

● Currently work on SMS products (Chat SMS, G+ SMS and more to come...)

● G+ profile: http://gplus.name/GuyFlysher

Page 3: Google App Engine Overview - BarCamp Phnom Penh 2011

Agenda

● Part I: What is App Engine?

● Part II: App Engine Product Usage

● Part III: How to use App Engine ○ Hello world example○ App Engine Services○ Code examples○ Demos of non web uses

Page 4: Google App Engine Overview - BarCamp Phnom Penh 2011

Why does App Engine exist?

Page 5: Google App Engine Overview - BarCamp Phnom Penh 2011

App Engine is a full development platform

Tools

Hosting

APIs

App Engine provides great tools, APIs & hosting

Easy to build Easy to manage Easy to scale

Page 6: Google App Engine Overview - BarCamp Phnom Penh 2011

Language Runtime Options

GOJavaExperimental

Page 7: Google App Engine Overview - BarCamp Phnom Penh 2011

App Engine APIs/Services

BlobstoreImages

Mail XMPP Task Queue

Memcache Datastore URL Fetch

User Service

Page 8: Google App Engine Overview - BarCamp Phnom Penh 2011

Administration Console

Page 9: Google App Engine Overview - BarCamp Phnom Penh 2011

Agenda

● Part I: What is App Engine?

● Part II: App Engine Product Usage

● Part III: How to use App Engine ○ Hello world example○ App Engine Services○ Code examples○ Demos of non web uses

Page 10: Google App Engine Overview - BarCamp Phnom Penh 2011

App Engine - A Larger Number

1,500,000,000+Page views per day

Page 11: Google App Engine Overview - BarCamp Phnom Penh 2011

Notable App Engine Customers

Page 12: Google App Engine Overview - BarCamp Phnom Penh 2011

Royal Wedding - Scalability Success

Official blog & live stream apps hosted on App Engine

On Wedding day...Blog app served:

● Up to 2k requests per second● 15 million pageviews ● 5.6 million visitors

Live stream app served:● Up to 32k requests per second● 37.7 million pageviews ● 13.7 million visitors

http://goo.gl/F1SGc

Page 13: Google App Engine Overview - BarCamp Phnom Penh 2011

Not all apps user-facing or web-based!

● Need backend server processing? Want to build your own?● Go cloud with App Engine!● No UI needed for app to talk to App Engine, just need HTTP or XMPP● Great place for user info e.g., high scores, contacts, levels/badges, etc.● Better UI: move user data off phone & make universally available

Page 14: Google App Engine Overview - BarCamp Phnom Penh 2011

Agenda

● Part I: What is App Engine?

● Part II: App Engine Product Usage

● Part III: How to use App Engine○ Servlets and JSP files○ Hello world example○ App Engine Services and code examples○ Demos of non web uses

Page 15: Google App Engine Overview - BarCamp Phnom Penh 2011

Java HttpServlet

● Abstract class for processing HTTP requests.

● Override its methods for processing various HTTP requests, e.g:

○ doGet○ doPost○ doHead○ etc

● Part of Java (not App Engine specific)

Page 16: Google App Engine Overview - BarCamp Phnom Penh 2011

HttpServlet example

public class Hello_worldServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

String userIp = req.getRemoteAddr(); resp.setContentType("text/plain"); resp.getWriter().println("Hello, " + userIp); }}

Page 17: Google App Engine Overview - BarCamp Phnom Penh 2011

JSP - JavaServer Pages

● Used to create dynamically generated web pages based on HTML.

● A mix of Java and HTML.

● Can be though of as the Java equivalent of PHP.

Page 18: Google App Engine Overview - BarCamp Phnom Penh 2011

JSP example

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html><head> <title> Welcome!</title></head><body>

<% if (request.getParameter("name") != null) {%><p>Hello, <%= request.getParameter("name") %> </p><% } else {%><p>Hello, stranger. </p><% }%>

Page 19: Google App Engine Overview - BarCamp Phnom Penh 2011

Hello World Demo

Deploy from scratch(In under 3 minutes)

Page 20: Google App Engine Overview - BarCamp Phnom Penh 2011

App Engine setup

Servlets and other Java code

JSP, html and other static files

Page 21: Google App Engine Overview - BarCamp Phnom Penh 2011

App Engine setup

Configuration files

Page 22: Google App Engine Overview - BarCamp Phnom Penh 2011

web.xml

Used to map URLs to the servlets that will handle them:

Page 23: Google App Engine Overview - BarCamp Phnom Penh 2011

web.xml

Page 24: Google App Engine Overview - BarCamp Phnom Penh 2011

web.xml

Used to map URLs to the servlets that will handle them:

Page 25: Google App Engine Overview - BarCamp Phnom Penh 2011

App Engine Services

The User Service

Page 26: Google App Engine Overview - BarCamp Phnom Penh 2011

The user system

Building your own user system is a lot of work

● Needs to be very secure - destructive result if broken into.

● Needs to be very reliable - if it is down your app can't be used.

● Lots of other services you need to build - a recovery mechanism etc.

Page 27: Google App Engine Overview - BarCamp Phnom Penh 2011

The Google user system

App Engine User Service lets you use Google's user system for your app.Benefits:

● Users don't need to create a new account to use your app, they can use their Google account

● Very secure, highly reliable.

● Already has recovery mechanisms etc.

● Very easy to use!

Page 28: Google App Engine Overview - BarCamp Phnom Penh 2011

The User Service

Checking if a user is logged in:<% UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); if (user != null) {%>

<p>Hello, <%= user.getNickname() %> </p>!<p>Our records show your email as: <%= user.getEmail() %> </p>

<% } else {%>

<p>Hello! Please log in. </p>

<% }%>

Page 29: Google App Engine Overview - BarCamp Phnom Penh 2011

The User Service

Creating a sign in/out links

<% UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); if (user != null) {%>

<p>Hello, <%= user.getNickname() %>!<p> <a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">Sign out </a></p>

<% } else {%><p><a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a</p>...

Page 30: Google App Engine Overview - BarCamp Phnom Penh 2011
Page 31: Google App Engine Overview - BarCamp Phnom Penh 2011
Page 32: Google App Engine Overview - BarCamp Phnom Penh 2011
Page 33: Google App Engine Overview - BarCamp Phnom Penh 2011

App Engine Services

The XMPP (chat) Service

Page 34: Google App Engine Overview - BarCamp Phnom Penh 2011

Sending a chat message

...

JID fromJid = new JID("[email protected]");JID toJid = new JID("[email protected]");Message msg = new MessageBuilder() .withRecipientJids(toJid) .withFromJid(fromJid) .withBody("Hi there. Is this easy or what?") .build(); XMPPService xmpp = XMPPServiceFactory.getXMPPService(); SendResponse status = xmpp.sendMessage(msg); boolean messageSent = (status.getStatusMap().get(toJid) == SendResponse.Status.SUCCESS);

...

Page 35: Google App Engine Overview - BarCamp Phnom Penh 2011

App Engine Services

The Mail Service

Page 36: Google App Engine Overview - BarCamp Phnom Penh 2011

Sending an email message

...

Properties props = new Properties();Session session = Session.getDefaultInstance(props, null);

try { Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress( "[email protected]", "Guy's App Engine App")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]")); msg.setSubject("You confirmation email"); msg.setText("..."); Transport.send(msg);

} catch (AddressException e) { ... } catch (MessagingException e) { ... } catch (UnsupportedEncodingException e) { ... }

...

Page 37: Google App Engine Overview - BarCamp Phnom Penh 2011

Demos

Page 38: Google App Engine Overview - BarCamp Phnom Penh 2011

�����!

Q&AMore documentation and information:

http://code.google.com/appengine

Page 39: Google App Engine Overview - BarCamp Phnom Penh 2011

Backup

Page 40: Google App Engine Overview - BarCamp Phnom Penh 2011

Receiving a chat message

Signup your app to receive chat messages

<inbound-services> <service>xmpp_message</service> </inbound-services>

appengine-web.xml

<servlet> <servlet-name>xmppreceiver</servlet-name> <servlet-class>gday.ReceiveChatMessageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>xmppreceiver</servlet-name> <url-pattern>/_ah/xmpp/message/chat/</url-pattern> </servlet-mapping>

web.xml

Page 41: Google App Engine Overview - BarCamp Phnom Penh 2011

Receiving a chat message

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

XMPPService xmpp = XMPPServiceFactory.getXMPPService(); Message message = xmpp.parseMessage(req);

JID fromJid = message.getFromJid(); String body = message.getBody(); String emailAddress = fromJid.getId().split("/")[0]; if (body.equalsIgnoreCase("hello")) { ... return; }

...