google app engine overview (gae/j)

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

Upload: moch-nasrullah

Post on 01-Nov-2014

8.084 views

Category:

Technology


1 download

DESCRIPTION

Googe App Engine Overview, Getting Started using Java

TRANSCRIPT

Page 1: Google App Engine overview (GAE/J)

GAE Overview

Moch Nasrullah R

Samsung R&D Institute Indonesia

(SRIN)November 2013

Page 2: Google App Engine overview (GAE/J)

As presented at:

Page 3: Google App Engine overview (GAE/J)

Agenda

• Cloud Computing

• GAE Overview

• GAE/J Overview

• GAE/J Getting Started

Page 4: Google App Engine overview (GAE/J)

Cloud Computing

Cloud Services

Cloud Clients(Web browser, Desktop App,

Mobile App, embeded, ...)

Page 5: Google App Engine overview (GAE/J)

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

Page 6: Google App Engine overview (GAE/J)

Cloud classification

Software as a Service (SaaS) Application:-Web Apps-Desktop Apps-Mobile Apps(Google Apps, Google Translate, Office 360, NetSuite, IBM Lotus Live, GitHub)

Platform as a Service (PaaS) Development Platform + Runtime Tools + Environment(Google App Engine, Heroku, Windows Azure, force.com, Rollbase)

Infrastructure as a Service (IaaS) CPUNetworksData Storage(AWS, VM Ware, Joyent, Rackspace)

Page 7: Google App Engine overview (GAE/J)

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

Page 8: Google App Engine overview (GAE/J)

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/docs/quotas

Page 9: Google App Engine overview (GAE/J)

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

Page 10: Google App Engine overview (GAE/J)

GAE/J Overview

developers.google.com/appengine/docs/java/

Page 11: Google App Engine overview (GAE/J)

Let’s give it a try

• Follow the getting started in link below:• https://developers.google.com/appengine/docs/java/gettingstarted/introduction

Page 12: Google App Engine overview (GAE/J)

Run Eclipse after Installing JDK

Page 14: Google App Engine overview (GAE/J)

Install from zip

Page 15: Google App Engine overview (GAE/J)

Creating Project

Page 16: Google App Engine overview (GAE/J)

Configure SDK

Directory where Appengine extracted

Page 17: Google App Engine overview (GAE/J)

The Servlet Class

package guestbook;

import java.io.IOException;

import javax.servlet.http.*;

public class GuestbookServlet extends HttpServlet {

public void doGet(HttpServletRequest req,

HttpServletResponse resp)

throws IOException {

resp.setContentType("text/plain");

resp.getWriter().println("Hello, world");

}

}

Page 18: Google App Engine overview (GAE/J)

Project Structure

Java source code

other configuration

JSPs, images, data files

app configuration

JARs for libraries

Page 19: Google App Engine overview (GAE/J)

Running Application

Page 20: Google App Engine overview (GAE/J)

Preparation for deployment

• Register to Google App Engine

• Create an Application

• Deploy via Eclipse

Page 21: Google App Engine overview (GAE/J)

Register to App Engine

• Register at: https://appengine.google.com/

• Create an Application

Page 22: Google App Engine overview (GAE/J)

Create an Application

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

Page 23: Google App Engine overview (GAE/J)

Problem when Deploying

• Adding VM configo Open eclipse.ini in the eclipse folder

o Add below lines before -vmargs-vm

C:\Java\jdk1.7.0_40\bin\javaw.exe

Page 24: Google App Engine overview (GAE/J)

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

Page 25: Google App Engine overview (GAE/J)

Sign in to Deploy

Page 26: Google App Engine overview (GAE/J)

Setting App ID & Version

Input Application Identifier registered at appspot.com

Page 27: Google App Engine overview (GAE/J)

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);

Page 28: Google App Engine overview (GAE/J)

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 = new Query("Greeting", guestbookKey).addSort("date", Query.SortDirection.DESCENDING);

List<Entity> greetings = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));

// put data tobe displayed 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

Page 29: Google App Engine overview (GAE/J)

/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:if test="${userNickname!=''}">

<p>Hello, ${fn:escapeXml(userNickname)}! (You can <a href="${signUrl}">sign out</a>.)</p>

</c:if>

<c:if test="${userNickname==''}">

<p>Hello!

<a href="${signUrl}">Sign in</a> to include your name with greetings you post.</p>

</c:if>

<c:if test="${empty greetingList}">

<p>Guestbook '${fn:escapeXml(guestbookName)}' has no messages.</p>

</c:if>

<c:if test="${not empty greetingList}">

<p>Messages in Guestbook '${fn:escapeXml(guestbookName)}'.</p>

</c:if>

Taglib

Say proper hello to sign in user

Page 30: Google App Engine overview (GAE/J)

/WEB-INF/jsp/guestbook.jsp<c:forEach items="${greetingList}" var="greeting">

<c:if test="${not empty greeting.properties['user']}">

<p><b>${fn:escapeXml(greeting.properties['user'].nickname)}</b> wrote:</p>

</c:if>

<c:if test="${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><textarea name="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

Page 31: Google App Engine overview (GAE/J)

Maybe Next Time

• Using Guice in GAE/J

• Using GAE Python

Page 32: Google App Engine overview (GAE/J)

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/

Page 33: Google App Engine overview (GAE/J)

Thanks

• SRIN Members

Page 34: Google App Engine overview (GAE/J)

About

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