Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

Google App Engine Tutorial for Java

Lars Vogel

Version 2.7

12.03.2010

Revision History
Revision 0.108.04.2009Lars Vogel
Created
Revision 0.2 - 2.725.04.2009 - 12.03.2010Lars Vogel
bug fixes and enhancements

Google App Engine and JPA

This article describes the creation of a Java web application on the Google App Engine. It uses JPA. The tutorial is based on Java 1.6, Eclipse 3.6 and GAE version 1.4.2.


Table of Contents

1. Google App Engine for Java (GAE/J)
1.1. Overview
1.2. Standard API's via App Engine specific API's
1.3. AppEngine technologies
1.4. Services
1.5. Versions
1.6. Restrictions
1.7. Quotas
2. Installation of the Google Tools for Eclipse
3. Register at the Google App Engine
4. Running an application
4.1. On the App Engine
4.2. Running application locally
5. Persistence with JPA
6. Developing a todo application
6.1. Overview
6.2. Project
6.3. Create your model
6.4. Setup JPA and your DAO
6.5. Create servlets
6.6. Create JSP
6.7. web.xml
6.8. Run your application again
6.9. Maintain application
6.10. Deploy your application
7. Email handling
8. Cron jobs
9. Logging
10. Admin Console
10.1. Overview
10.2. Extend the Admin Console
11. Thank you
12. Questions and Discussion
13. Links and Literature
13.1. Google App Engine Links
13.2. vogella Resources

1. Google App Engine for Java (GAE/J)

1.1. Overview

Google offers a cloud computing infrastructure called Google App Engine (App Engine) for creating and running web applications . App Engine allows the dynamic allocation of system resources for an application based on the actual demand. Currently App Engine supports Python and Java based applications. This includes Java Virtual Machine (JVM) based languages, e.g. Groovy or Scala . This article will cover the App Engine for Java (GAE/J)).

1.2. Standard API's via App Engine specific API's

The App Engine offers frequently standard Java API's and App Engine specific API's for the same task. If you want to be able to port your application from the AppEngine to other webcontainers, e.g. Tomcat or Jetty, you should only use Java standard API.

1.3. AppEngine technologies

App Engine uses the Jetty servlet container to host applications and supports the Java Servlet API in version 2.4. It provides access to databases via Java Data Objects (JDO) and the Java Persistence API (JPA) . In the background App Engine uses Google Bigtable as the distributed storage system for persisting application data.

Google provides Memcache as a caching mechanism. Developers who want to code against the standard Java API can use the JCache implementation (based on JSR 107).

1.4. Services

The App Engine provides several services. For example the Blobstore allows to upload and store and serve large data objects (blobs) with a limit of 2 Gigabyte. To create a blob you upload a file via an HTTP request.

1.5. Versions

Google App Engine supports the creation of several version of your application. In the Admin Console you can select which version should be active. Your active application "your-name" will be accessible via the URL "http://your-name.appspot.com". Each version can also be accessed for example to test a new version. The version are accessable via "http://versionnumber.latest.your-name.appspot.com" where version is for example "2" and "latest" is a fixed string.

1.6. Restrictions

App Engine runs a version of Java 6 but does not provide all Java classes, for example Swing and most AWT classes are not supported.

You cannot use Threads or frameworks which uses Threads. You can also not write to the filesystem and only read files which are part of your application. Certain "java.lang.System" actions, e.g. gc() or exit() will do nothing. You can not call JNI code. Reflection is possible for your own classes and standard Java classes but your cannot use reflection to access other classes outside your application.

See Java Whitelist for a full list of supported classes.

A servlet needs also to reply within 30 seconds otherwise a "com.google.apphosting.api.DeadlineExceededException" is thrown.

1.7. Quotas

Google offers free hosting for websites which are not highly frequented, e.g. 5 Millions page views. The price model for the websites that exceed thier daily quota is listed on the Google billing documentation pages . The usage quotas of the App Engine are constantly changing but, at the time of this writing, are around 5 millions pages views per month, which translates approx. in 6.5 CPU hours and 1 gigabyte of outbound traffic.

Currently a user can create a maximum of 10 applications on the Google App Engine. The user can delete existing application in the Admin Console under Application Settings.