Google App Engine overview (GAE/J)

nazrvl 11,595 views 34 slides Feb 04, 2014
Slide 1
Slide 1 of 34
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34

About This Presentation

Googe App Engine Overview, Getting Started using Java


Slide Content

GAE Overview
Moch Nasrullah R
Samsung R&D Institute Indonesia
(SRIN)
November 2013

As presented at:

Agenda
•Cloud Computing
•GAE Overview
•GAE/J Overview
•GAE/J Getting Started

Cloud Computing
Cloud Services
Cloud Clients
(Web browser, Desktop App,
Mobile App, embeded, ...)

http://upload.wikimedia.org/wikipedia/commons/b/b5/Cloud_computing.svg

Cloud classification
Software as a Service(SaaS) Application:
-Web Apps
-DesktopApps
-Mobile Apps
(Google Apps, Google Translate, Office
360, NetSuite, IBM Lotus Live, GitHub)
Platform as a Service(PaaS) Development Platform+ RuntimeTools +
Environment
(Google App Engine, Heroku, Windows
Azure, force.com, Rollbase)
Infrastructure as a Service(IaaS) CPU
Networks
Data Storage
(AWS,VM Ware, Joyent, Rackspace)

Google App Engine
•run your web applications on Google's
infrastructure
–Google handles the maintenance infrasturcture:
hardware failures, security patches, OS upgrades
•Free ... within quota

GAE Limits & Quota
•10 Apps per user
•5 Mio pageview free per month
•6.5 hours of CPU and 1 Gb in & out traffic
•https://developers.google.com/appengine/do
cs/quotas

Why GAE
•Easy to build
–Language support (Java, Python, GO, PHP)
–Automatic scaling & load balancing
•Easy to maintain
–Web based admin dashboard
•Easy to scale (traffic & data storage)
–GAE Datastore
–Google Cloud SQL
–Google Cloud Storage

GAE/J Overview
developers.google.com/appengine/docs/java/

Let’s give it a try
•Follow the getting started in link below:
•https://developers.google.com/appengine/docs/java/gettingstarted/introduction

Run Eclipse after Installing JDK

Installing Plugin
https://developers.google.com/eclipse/docs/install-eclipse-4.3

Install from zip

Creating Project

Configure SDK
Directory where
Appengine extracted

The Servlet Class
packageguestbook;
importjava.io.IOException;
importjavax.servlet.http.*;
publicclassGuestbookServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequestreq,
HttpServletResponseresp)
throwsIOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
}
}

Project Structure
Java source code
other configuration
JSPs, images, data files
app configuration
JARs for libraries

Running Application

Preparation for deployment
•Register to Google App Engine
•Create an Application
•Deploy via Eclipse

Register to App Engine
•Register at: https://appengine.google.com/
•Create an Application

Create an Application
•For now, just fill in ‘Application Identifier’ and ‘Application
Title’, than accept ‘Term of Service’

Problem when Deploying
•Adding VM config
oOpen eclipse.ini in the eclipse folder
oAdd below lines before -vmargs
-vm
C:\Java\jdk1.7.0_40\bin\javaw.exe

Adding VM config
•Open eclipse.ini in the eclipse folder
•Add below lines before -vmargs
•-vm
•C:\Java\jdk1.7.0_40\bin\javaw.exe

Sign in to Deploy

Setting App ID & Version
Input Application Identifier
registered at appspot.com

Refactor Example to MVC
•Using JSP as View template
–JSP files will resides inside ‘WEB-INF/jsp’ folder
–So users can not access our template directly
•Using Servlet as Controller
–Put model in request attribute
–Forward to proper View
–Change SignGuestbookServlet.java so it redirect to
servlet (not JSP):
resp.sendRedirect("/guestbook?guestbookName="+ guestbookName);

GuestbookServlet.java –doGet()
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
String signUrl = "";
String userNickname = "";
if(user!=null) {
signUrl = userService.createLogoutURL(req.getRequestURI());
userNickname = user.getNickname();
} else{
signUrl = userService.createLoginURL(req.getRequestURI());
}
String guestbookName = req.getParameter("guestbookName");
if(guestbookName == null) {
guestbookName = "default";
}
DatastoreService datastore = DatastoreServiceFactory. getDatastoreService();
Key guestbookKey= KeyFactory.createKey("Guestbook", guestbookName);
Query query = newQuery("Greeting", guestbookKey).addSort("date", Query.SortDirection.DESCENDING);
List<Entity> greetings = datastore.prepare(query).asList(FetchOptions.Builder. withLimit(5));
// put data tobedisplayed in JSP
req.setAttribute("signUrl", signUrl);
req.setAttribute("userNickname", userNickname);
req.setAttribute("guestbookName", guestbookName);
req.setAttribute("greetingList", greetings);
String templateFile = "/WEB-INF/jsp/guestbook.jsp";
RequestDispatcher rd = getServletContext().getRequestDispatcher(templateFile);
rd.forward(req, resp);
Put data in Request
Attribute
Forward to View
Login or Logout URL
Retrieve data from
datastore

/WEB-INF/jsp/guestbook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
</head>
<body>
<c:iftest="${userNickname!=''}">
<p>Hello, ${fn:escapeXml(userNickname)}! (You can <a href="${signUrl}">sign
out</a>.)</p>
</c:if>
<c:iftest="${userNickname==''}">
<p>Hello!
<a href="${signUrl}">Sign in</a>to include your name with greetings you post. </p>
</c:if>
<c:iftest="${empty greetingList}">
<p>Guestbook '${fn:escapeXml(guestbookName)}' has no messages.</p>
</c:if>
<c:iftest="${not empty greetingList}">
<p>Messages in Guestbook '${fn:escapeXml(guestbookName)}'. </p>
</c:if>
Taglib
Say proper hello to
sign in user

/WEB-INF/jsp/guestbook.jsp
<c:forEachitems="${greetingList}"var="greeting">
<c:iftest="${not empty greeting.properties['user']}">
<p><b>${fn:escapeXml(greeting.properties['user'].nickname)} </b>wrote:</p>
</c:if>
<c:iftest="${empty greeting.properties['user']}">
<p>An anonymous person wrote:</p>
</c:if>
<blockquote>${fn:escapeXml(greeting.properties['content'])} </blockquote>
</c:forEach>
<form action="/sign" method="post">
<div><textareaname="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Post Greeting" /></div>
<input type="hidden" name="guestbookName"
value="${fn:escapeXml(guestbookName)}"/>
</form>
</body>
</html>
Iterate Greeting List
Passed from Servlet
Form same as
previous

Maybe Next Time
•Using Guicein GAE/J
•Using GAE Python

Reference
•http://www.slideshare.net/dimityrdanailov/google-app-engine-
varna-lab-19062013
•http://www.slideshare.net/LarsVogel/google-app-engine-for-java-
7698966
•http://www.google.com/events/io/2009/sessions.html#appengine
•http://www.google.com/events/io/2010/sessions.html#App%20Eng
ine
•http://www.google.com/events/io/2011/sessions.html#app-
engine-track
•https://developers.google.com/events/io/2012/sessions#cloud-
platform
•https://developers.google.com/events/io/2013/sessions#t-google-
cloud-platform
•https://developers.google.com/appengine/

Thanks
•SRIN Members

About
•https://sites.google.com/site/meetnasrul/