26,04 €
Google Application Engine is a cloud computing technology that lets you run web applications that are easy to build and maintain as well as being scaled and load-balanced automatically. To build interesting and interactive web applications developers are turning to Java. However, building and deploying scalable web applications using Google Web Toolkit and Google App Engine for Java can be challenging even to developers. The start to finish approach of this book helps you meet these challenges.
This book is designed to give developers all the information they need to develop their own GAE+GWT applications, with a particular focus on some of the technologies useful for building scalable social-media-oriented applications. It is an easy-to-follow guide that shows you how to get the most out of combining the powerful features of GAE and GWT. It provides you with solutions to many of the problems that arise in developing, maintaining, and scaling web applications.
The book introduces you to the features of Google App Engine (GAE) and the Google Web Toolkit (GWT) to help you build interesting and interactive real-world AJAX applications. Over the course of the book, you are guided by the development of a sample application using the Google Web Toolkit (GWT) plugin for Eclipse. To make things easier for you, all application building blocks are explained in detail, walking you through the development process step by step.
The book teaches you the key technologies and methodologies that can be applied to developing your application locally, enhancing your application functionality and accelerating performance, integrating your application with Google Accounts, Facebook, and Twitter as well as monitoring and maintaining your web application.
Packed with great examples and clear explanations, you will be able to build your own web applications that are scalable and reliable using GAE and GWT with Java.
Build a real-world interactive web application in the cloud using GAE/J and GWT, with Push, background processing, OAuth, social media APIs, and more
This is a practical guide with a step-by-step approach that builds an application in stages. Each stage of the application is used along with screenshots to introduce or develop your understanding of how to write powerful GAE applications.
If you are a developer with prior programming experience of Java development and object-oriented programming and want to understand the key concepts of how to build AJAX web applications with Java, then this book is for you. The book assumes some previous exposure to the basics of using GWT, for example at the level of Google's online introductory GWT tutorials. You don't need to know anything about Google App Engine. This book will teach you everything you need.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 602
Veröffentlichungsjahr: 2010
Copyright © 2010 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: November 2010
Production Reference: 1161110
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-849690-44-7
www.packtpub.com
Cover Image by Sujay Gawand (<[email protected]>)
Authors
Daniel Guermeur
Amy Unruh
Reviewers
Dom Derrien
Yağız Erkan
Samuel Goebert
Ravi Sharma
Acquisition Editor
David Barnes
Development Editor
Hyacintha D'Souza
Technical Editors
Paramanand N.Bhat
Namita Sahni
Copy Editor
Laxmi Subramanian
Editorial Team Leader
Aditya Belpathak
Project Team Leader
Lata Basantani
Project Coordinator
Vincila Colaco
Indexers
Monica Ajmera Mehta
Rekha Nair
Proofreader
Kevin McGowan
Graphics
Geetanjali Sawant
Production Coordinator
Arvindkumar Gupta
Cover Work
Arvindkumar Gupta
Daniel Guermeur is the founder and CEO of Metadot Corporation. He holds a Diplome d'Ingenieur of Informatique from University of Technology of Compiegne (France) as well as a Master in Photonics from Ecole Nationale Superieure de Physique of Strasbourg (France). Before starting Metadot in 2000, he worked for oil services companies including giant Schlumberger Ltd where he was in charge of improving the worldwide IT infrastructure.
He has been developing large scale database-backed web applications since the very beginning of the democratization of the Internet in 1995, including an open source software content management system Metadot Portal Server, Mojo Helpdesk, a web-based customer support application and Montastic, a popular website monitor service.
Thank you to my daughter Alexandra Guermeur and Cheryl Ridall for their love and continuous support while writing this book. This meant a lot to me. Cheryl, I miss you.
Amy Unruh currently does technology training and course development, with a focus on web technologies. Previously, she has been a Research Fellow at the University of Melbourne, where she taught web technologies and researched robust distributed agent systems. She has worked at several startups, building web applications using a variety of languages; served as adjunct faculty at the University of Texas; and was a member of the Distributed Agents Program at MCC. She received her Ph.D. in CS/AI from Stanford University, in the area of AI planning, and has a BS degree in CS from UCSB. She has numerous publications, and has co-edited a book on Safety and Security in Multiagent Systems.
Thanks to Moon, and to Wesley and Eleanor Unruh, without whose love and support this book would not have been possible. And thanks also to Daniel, for getting us started!
Dom Derrien joined AnotherSocialEconomy.com (ASE) as co-founder after having worked for more than 15 years in the software development area.
Dom's responsibilities in ASE cover the architecture definition of its communication engine and respective connectors enabling its multi-channel interfaces like e-mail, Twitter, Facebook, Android, Adobe AIR, and others. Dom is also in charge of defining the ASE public API (REST-based). He produces the vast majority of code and corresponding tests for the implementation ported on Google App Engine. Dom also coordinates the work of other contributors working on specific connectors as well as reviewing web materials and contributing to pre-sales consulting, the internal community site and business decision-making.
Prior to ASE, Dom worked for Compuware Corporation as Technical Consultant in the Web application and Mobile application domains, to bootstrap new projects in the Vantage product line. Before joining Compuware, as a Software Architect, he worked on the development of a Web 2.0 client for Rational Portfolio Manager enterprise project management server, at IBM Canada. Dom also worked at Oracle Canada, as a Senior Developer, in the Oracle Collaboration Suite team, to build its first Web 2.0 client, just at the time the corresponding technology was emerging.
Aside from his protected work for ASE, Dom shares some projects on github.com to give back materials to the open source community (like a library for Amazon FPS on GAE, utilities for internationalized Web applications, and others) and publishes his own blog at domderrien.blogspot.com.
Dom is also active in the Diku Dilenga organization which aims to bring microfinance services to the poorest in the Democratic Republic of the Congo. His dream is to connect ASE to Diku Dilenga, to use modern communication technologies at the service of microentrepreneurs.
Also important in his life is his family – his wife and two sons. They all enjoy their peaceful life in Montreal, Quebec, Canada, and their practice of karate, Chito-Ryu style.
Yağız Erkan is the Chief Technical Architect with DeCare Systems Ireland, an Ireland-based software development company building highly-scalable, large enterprise systems and retail sites. Before taking on an architectural role, he worked as a software engineer for various companies in France.
Yağız studied in Galatasaray High School in Istanbul, Turkey, then he obtained his MSc in Computer Science in Université Claude Bernard in Lyon, France. After working several years as a software engineer in Lyon, he moved to Ireland and joined DeCare Systems Ireland as a Technical Architect in 2000. He currently leads a team of hands-on architects who ensure the most up-to-date and suitable practices, technologies and tools are utilized, leading to the delivery of high quality applications and solutions.
Even though Yağız's background is mainly Java and Web technologies, he has worked on Microsoft .NET and mobile (iPhone and Android) projects. He has already been mentioned in various books as a reviewer and he keeps an on-line presence through blogging and social networking.
Samuel Goebert is a computer science master student at the University of Applied Sciences Darmstadt, Germany. Goebert has over 7 years of experience in web related development and associated technologies.
In his role as technology consultant for bigcurl (http://www.bigcurl.de) Goebert is responsible for the overall design and execution of various products from conception to launch including web applications, mobile applications, intranet tools and custom API's.
He wrote his award winning bachelor thesis about scalable data delivery in local area networks at Apple Inc. in California, which also awarded him with a students scholarship from 2006 until 2009.
Goebert holds a Diploma from the School of Audio Engineering and received a bachelors degree from the University of Applied Sciences Darmstadt, Germany in Computer Science.
He is currently working on receiving his master degree in Computer Science in the field of restoring communications for the population of post disaster areas with autonomously deploying cell phone towers.
Ravi Sharma has a B.Tech (Computer Science) from Y.M.C.A Institute of Engineering, Faridabad and has worked in banking and higher studies. He has 8 years of experience in java especially low latency applications.He runs his own company, NextInfotech (www.nextinfotech.com) and also works as a consultant.
This book is designed to give developers the tools they need to build their own Google App Engine (GAE) with Google Web Toolkit (GWT) applications, with a particular focus on some of the technologies useful for building social-media-oriented applications. The book is centered on a GAE + GWT Java application called Connectr, which is developed throughout the chapters and demonstrates, by example, the use of the technologies described in the book. The application includes social-media information gathering and aggregation activities and incorporates the use of many App Engine services and APIs, as well as GWT design patterns and widget examples.
Several stages of the Connectr application are used throughout the book as features are added to the app. Code is included with the book for all application stages, and each chapter indicates the stage used.
Chapter 1, Introduction, introduces the approaches and technology covered in the book, and discusses what lies ahead.
Chapter 2, Using Eclipse and the Google Plugin, describes the basics of setting up a project using the Eclipse IDE and Google's GWT/GAE plugin. Topics include defining, compiling and running an Eclipse GWT/GAE project, and using the GWT developer browser plugin with the interactive debugger. The chapter also covers how to set up an App Engine account and create applications, and how to deploy an app to App Engine and access its Admin Console.
Chapter 3, Building The Connectr User Interface with GWT, focuses on GWT, and building the first iteration of the Connectr application's frontend. The chapter looks at how to specify widgets, with a focus on declarative specification using GWT's UIBinder and using the GWT RPC API for server-side communication.
Chapter 4, Persisting Data: The App Engine Datastore, covers Datastore basics. In the process, the first iteration of Connectr's server-side functionality is built. The chapter looks at how the Datastore works, and the implications of its design for your data models and code development. It covers how to use Java Data Objects (JDO) as an interface to the Datastore and how to persist and retrieve Datastore entities.
Chapter 5, JDO Object Relationships and Queries, builds on the topics of Chapter 4. It describes how to build and manage JDO objects that have relationships to each other, such as one-to-many and one-to-one parent-child relationships. It also covers how to query the Datastore, and the important role that Datastore indexes play in this process.
Chapter 6, Implementing MVP, an Event Bus and Other GWT Patterns, builds on the client-side code of Chapter 3, and shows how to make the frontend code modular and extensible. It accomplishes this via use of the MVP (Model-View-Presenter) and Event Bus design patterns, history/bookmark management, and an RPC abstraction, which supports call retries and progress indicators.
Chapter 7, Background Processing and Feed Management, centers on defining and running decoupled backend asynchronous tasks. In the process, the chapter introduces several App Engine services, including URLFetch and Task Queues, shows the use of Query Cursors to distribute Datastore-related processing across multiple Tasks, and introduces the use of Java Servlets and the incorporation of third-party libraries in a deployed application.
Chapter 8, Authentication using Twitter and Facebook OAuth and Google Accounts, adds authentication, login, and account functionality to Connectr, allowing it to support multiple users. The chapter demonstrates the use of both the Google Accounts API and the OAuth protocol for creating user accounts.
Chapter 9, Robustness and Scalability: Transactions, Memcache, and Datastore Design, delves into more advanced Datastore-related topics. The chapter investigates Datastore-related means of increasing the robustness, speed, and scalability of an App Engine app, including several ways to design data classes for scalability and to support efficient join-like queries. The chapter also introduces App Engine transactions and Transactional Tasks and the use of Memcache, App Engine's volatile-memory key-value store.
Chapter 10, Pushing fresh content to clients with the Channel API, covers the implementation of a message push system using the App Engine Channel API, used by Connectr to keep application data streams current. The chapter describes how to open back-end channels connected to client-side socket listeners, and presents a strategy for preventing the server from pushing messages to unattended web clients.
Chapter 11, Managing and Backing Up Your App Engine Application, focuses on useful App Engine deployment strategies, and admin and tuning tools. It includes ways to quickly upload configuration files without redeploying your entire application and describes how to do bulk uploads and downloads of application data. The chapter also discusses tools to analyze and tune your application's behavior, and the App Engine billing model.
Chapter 12, Asynchronous Processing with Cron, Task Queue, and XMPP, finishes building the server-side part of the Connectr app. The chapter introduces the use of App Engine Cron jobs, configuration of customized Task Queues, and App Engine's XMPP service and API, which supports push notifications. The chapter shows the benefits of proactive and asynchronous updating—the behind-the scenes work that keeps Connectr's data stream fresh—and looks at how App Engine apps can both send and receive XMPP messages.
Chapter 13, Conclusion, summarizes some of the approaches and technology covered in the book, and discusses what might lie ahead.
The book assumes some previous exposure to the basics of using Google Web Toolkit (GWT). "Recommended GWT Background" in Chapter 1 lists the basic GWT concepts that will be useful for you to know, and points to the online GWT documentation that covers these concepts. The book does not assume prior Google App Engine exposure.
This is an intermediate-level book. It is designed for readers with some prior programming experience, and previous experience with Java development and object-oriented programming. It also assumes a general understanding of web technologies and concepts, and how to build web applications.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail <[email protected]>.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code for this book
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
Google App Engine (GAE) is a platform and SDK for developing and hosting web applications, using Google's servers and infrastructure. Google Web Toolkit (GWT) is a development toolkit for building complex AJAX-based web applications using Java, which is then compiled to optimized JavaScript. Used together, GAE/Java and GWT provide an end-to-end Java solution for AJAX web applications, which can solve many of the problems that arise in developing, maintaining, and scaling web applications.
GAE and GWT together provide:
This book is designed to give developers the tools they need to build their own GAE+GWT applications with a particular focus on some of the technologies useful for building social-media-oriented applications. The book is centered on a GAE+GWT Java application called Connectr that is developed throughout the chapters and demonstrates by example the use of the technologies described.
The remainder of this chapter will introduce the features of Google App Engine and Google Web Toolkit in more detail. Then we describe the Connectr application whose development provides a running example throughout the chapters, and highlight some of the technologies and approaches used by the application.
The final section of the chapter gives pointers to some useful online resources.
Google App Engine (GAE) is a platform and SDK for developing and hosting web applications using Google's servers and infrastructure. The most current version of GAE at the time of writing is 1.3.7. App Engine supports both Java and Python runtime environments and SDKs, with each environment supporting standard protocols and technologies for web application development. This book focuses on the Java runtime.
Google App Engine as a platform is designed for scalability, robustness, and performance. App Engine allows you to build applications that are scaled and load-balanced automatically for you as the demand for your application and your data storage needs increase. With App Engine applications, you do not need to perform any server or database maintenance yourself—you just upload and run your app. Google's infrastructure offers reliable performance even under a heavy load and when using very large amounts of data.
A request to an App Engine app is routed to a selected app server, and the application is started on the server if necessary. No state on the server is saved between requests. (Instead, GAE has other means to persist data between requests). There is no guarantee that the same server will handle two subsequent requests, even if the time period between them is very short. Thus, a runtime instance often comes into existence when a request handler begins, and is terminated when it ends—though the instance may sometimes be retained, depending upon app traffic.
By making no assumption that a runtime will be maintained on a given server between requests, App Engine can distribute request traffic across as many servers as necessary, and target requests at servers that it expects will provide the fastest response.
It is useful to view App Engine in terms of the several facets of its functionality—the App Engine Datastore, its scalable services, and its runtime environments.
App Engine's Datastore is based on Google's BigTable technology (http://labs.google.com/papers/bigtable.html). It is a non-centralized, persistent store, designed specifically to be distributed and to scale as it grows.
The Datastore is not a "join-query" relational database. Rather, it is essentially a property-value store, holding entities—each of a given kind—that contain property-value sets.
App Engine's Datastore is designed so that the request time is linear in the size of the results, not the size of the data stored. This is accomplished in part by the way in which Datastore builds indexes at write-time, and App Engine's imposition of constraints on the types of queries supported, resulting in extremely efficient reads over large distributed datasets.
The Datastore supports transactions and uses optimistic concurrency control. The transactional model is specific to how App Engine works: the allowed transactions are determined by how entities are defined and grouped in the Datastore.
App Engine supports a number of useful capabilities and services, all designed to scale with the size of your app. The Datastore is in fact one such service. Other services include:
Both the Python and Java App Engine runtimes support the above-mentioned capabilities, and many discussions of App Engine capabilities and design approaches can be largely runtime-environment agnostic. In a few cases, the Python features or development tools, which have a head start on the Java version, are currently more capable, but in future their respective capabilities are intended to equalize.
Both runtime environments have some "sandbox" restrictions, imposed to allow App Engine to use any server for a new incoming request, and to control server responsiveness and security. For example, application code cannot access the server on which it is running, in the traditional sense.
SDKs are provided for both the Python and Java runtimes, along with tools for uploading and downloading app information, including bulk data uploads and downloads.
This book uses Google App Engine's Java platform, with the exception of a few forays into Python in order to access some administrative tools.
The Google App Engine Java (GAE/J) runtime includes the Java SE Runtime Environment (JRE) 6 platform and libraries (with some sandbox restrictions implemented in the JVM). The Java Servlet standard is supported, and is a basis for how your app interacts with its environment.
The App Engine Java SDK currently supports developing apps using either Java 5 or 6. However, support in the SDK for Java 1.5 is now deprecated. Languages that use a JVM-based interpreter or compiler (and do not violate the sandbox restrictions), such as JRuby, can be run on App Engine as well.
For most people, the easiest way to develop Java App Engine apps is via the Eclipse IDE (http://www.eclipse.org/). Eclipse has sophisticated general support for Java development. Google provides a plugin that supports development of GWT+GAE apps, and adds many additional capabilities. The plugin helps to generate the necessary directories for a GAE (and GWT) project, creates key files, sets up classpaths, and can do some code generation. The plugin also allows "push-button" uploading and deployment of an App Engine application, including automatic compilation of the GWT Java code to JavaScript as required, and copying and packaging of all the necessary files for the deployment.
Furthermore, the plugin integrates the SDK's development support for simulating GAE on your local computer, giving you the ability to set breakpoints and use an interactive debugger.
This book will use Eclipse for development.
GAE apps are hosted at http://appspot.com (you can map the apps to your own domain names as well, if you like). You can host multiple apps, and multiple versions of each app. You can host apps for free, with relatively limited resource quotas, or enable billing for an app in order to increase its available resources. The App Engine billing model is tied to the resources actually used by an app so if you enable billing you will be charged more only as your app becomes busier— up to fixed thresholds that you specify. An application will still have some resource quotas, even with billing enabled; if you are running up against these thresholds, you can request to have them raised.
To administer your app versions, track their resource usage and statistics and view their logs, manage app billing, and so on, an App Engine Admin Console web application is provided.
Google has announced a new platform—Google App Engine for Business (http://code.google.com/appengine/business/). Its features include a Service Level Agreement (SLA), a per-user-per-app pricing structure (up to a maximum threshold), and support for SQL and dedicated relational databases in addition to the Datastore.
At the time of writing, App Engine for Business has not been rolled out, its details are still subject to change, and it will not be covered in this book.
Some aspects of the GAE model and App Engine development can be surprising to developers who are new to the platform. They stem in part from the constraints imposed by App Engine in order to ensure scalability and reliability. We will list some of these aspects here, and discuss all of them in more detail, later in the book.
GWT is a development toolkit for building complex AJAX-based web applications using Java, which is then compiled to highly optimized JavaScript. The current version as of this writing is 2.0.4.
GWT allows you to write your application frontend in Java, and then compile the source to highly optimized JavaScript that runs across all major browsers. It allows you to debug applications in an IDE as you would debug a "regular" Java app, interactively setting breakpoints, while at the same time you can access the app in the browser as if it were generated JavaScript. It does this via a browser developer plugin, which spans the gap between Java bytecode in the debugger and the browser's JavaScript.
Once you are ready to deploy, the GWT Java code is compiled to JavaScript. More exactly, a compiled application frontend consists of JavaScript, HTML, and XML.
There are several advantages to this compilation process in terms of efficiency, security, and browser compatibility. The compiled JavaScript is highly efficient and optimized, and helps protect against Cross-site scripting (XSS) attacks. (The GWT documentation provides more detail on the specifics of this; essentially, you are well protected as long as you do not add your own JavaScript, use the JSON API to parse untrusted strings, or write code to set "innerHTML" in GWT widgets).
In addition, compatible JavaScript is generated for all major browsers (a current list of supported browsers is here: http://code.google.com/webtoolkit/doc/latest/FAQ_GettingStarted.html#What_browsers_does_GWT_support?). Every web browser has its own idiosyncrasies, and GWT allows you to work at a level of abstraction above these differences. When the Java app is compiled into JavaScript, a different, efficient version is generated for each browser type. However, only the JavaScript required by a given client is downloaded when a request is made to the app. GWT uses a technique called deferred binding to accomplish this. A bootstrapping process at runtime loads the right version for the client.
GWT doesn't require you to write your app using any JavaScript—you can stick to Java, with its strict typing and powerful compile-time checks, to build your app. However, you can integrate raw JavaScript with a GWT app if you like, as when calling other native code using JNI. You can define an entire application frontend to use GWT, or you can integrate small amounts of GWT with an existing app.
GWT also allows developer-guided code splitting. Code splitting is useful when your application's compiled JavaScript code base is large enough to require significant startup time while downloading it. In this case, you can indicate places in your GWT program where you want the program to pause and download more code. Code will always be loaded automatically on initial download if it is part of a dependency chain for the application's initial entry point. So, code splitting can require some consideration of program design, using patterns that allow control of "modules" of functionality and their access. However, the process can provide a large payoff in terms of efficiency.
GWT provides many pre-defined widgets and panels that you can use as UI building blocks and also allows you to build your own widgets, either as composites or from scratch. It allows both programmatic and declarative definition of UIs, or a combination of both, and allows use of CSS for formatting and layout.
We will make use of many of the GWT widgets and panels in this book. For a full list of the GWT-provided widgets, see the GWT gallery: http://code.google.com/webtoolkit/doc/latest/RefWidgetGallery.html.
The GWT toolkit includes other support for building AJAX-based applications. It addresses the "browser back button" issue by providing support for history management. This issue stems from the fact that with AJAX web apps, it is possible for the page content to change without a change in the URL. In such cases, the back button won't work as the user expects; nor will bookmarking the URL necessarily return them to the UI state that they expect. GWT's history management provides a way to rewrite the URL upon important state changes, so that the back button and bookmarking work as expected.
GWT also provides strong support for event handling—both low-level browser events, and more abstract application-level events.
GWT has a Remote Procedure Call(RPC) API and framework that supports client-server communication. It allows you to pass objects between the client and server, instantiated from shared data classes, in a straightforward manner.
The RPC package runs on App Engine, making it easy to build GWT frontends to App Engine apps—exactly as we will do in this book. (The RPC package can also be used with other Servlet containers, such as Tomcat).
The RPC calls are all asynchronous, that is, non-blocking. They return immediately, and use a callback method, defined by the user, to handle the results later returned from the server.
GWT supports client-server communication via other means as well (for example, using JSON), but this book focuses on the RPC API.
GWT does not need to be paired with GAE/Java as the backend, but they are designed to be integrated, and work very effectively together. One goal of this book is to show how GWT combined with App Engine synergistically simplifies development and deployment in many ways.
A typical GWT+GAE code base will include client, server, and shared packages. The shared package includes classes that must be accessible to both the client GWT and server GAE code. Data Transfer Objects (those objects passed over GWT RPC) must be in the shared package and must not use any Java methods unavailable to GAE or GWT.
Third-party Java libraries cannot be used from client-side Java code unless their source code is available to the GWT compiler and this code uses only the supported subset of Java.
As indicated earlier, Google provides a GWT+GAE plugin for the Eclipse IDE, which provides a number of useful capabilities. The plugin includes the SDKs for both App Engine and GWT. The Eclipse plugin allows integrated use of the GWT SDK's developer browser plugin, letting you set Eclipse breakpoints in your frontend GWT Java code and debug and inspect interactively while accessing the app in the browser—a powerful development aid.
The Eclipse plugin also fully manages app deployment for you. It allows "push-button" uploading of an App Engine app, including automatic compilation of the GWT Java code, and copying and packaging of all the necessary files for the deployment.
GWT supports most but not all core Java capabilities and language semantics. Its scope is constrained by its purpose—the Java code must be compilable into JavaScript. As an example, Java threading is not supported by GWT. In some cases, the GWT emulation differs from the standard Java runtime, or provides a subset of the JRE functionality.
In general, GWT's JRE emulation supports the following packages (sometimes with only a subset of the methods): java.lang, java.lang.annotation, java.util, java.io, and java.sql. Use of an IDE such as Eclipse is helpful in indicating when a given Java method is not supported. The GWT documentation (http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsCompatibility.html, http://code.google.com/webtoolkit/doc/latest/RefJreEmulation.html) provides specifics on supported classes.
There are many online resources and books that introduce GWT. This book assumes some basic prior knowledge about how to use GWT. In particular, it would be helpful for you to be familiar with the following concepts when reading this book:
All of these topics are covered in Google's online GWT documentation (http://code.google.com/webtoolkit/).
This book is centered on a GWT + Google App Engine application, designed to show the power of combining these two technologies. The app is also designed to explore many of the important features and capabilities of both GAE/J and GWT, which will likely be used in building your own apps, and demonstrates a number of useful design patterns.
The example application is a social-media-oriented app named Connectr. The application includes social-media information gathering and aggregation activities, and incorporates the use of App Engine's services and APIs.
Connectr allows a user to create an account and log in by authenticating via the Google Accounts API (for example, with their Gmail account), or via OAuth using a Twitter or Facebook account.
Once logged in, the user can maintain and edit contact information for a set of friends, and can display a stream aggregating the friends' updates. Connectr supports login authentication via either the Google Accounts API (App Engine's support for authentication via a Google login), or via Twitter or Facebook using OAuth.
A user's customized information stream is based on an aggregation of feed data. As part of the information associated with each friend, a user can add any number of RSS or Atom feed URLs for that friend, for example, from services such as Twitter, Flickr, and blogs. Figure 1 shows the editing of a friend's details in Connectr, with the friend's associated URLs specified.
Figure 1: Editing Friend information in Connectr.
The feed data is fetched and integrated into a chronologically sorted and auto-updating "activity stream", supported by push from the App Engine's recently-introduced Channel API.
Figure 2: The main view of Connctr, showing a list of friends for a user, and a stream of items for selected friends.
The stream can show information from all of the user's friends' feeds, or be filtered to show the feeds for only a subset of friends. A user's activity stream can also be configured to include insertion of breaking news notifications, delivered to the app via XMPP.
Figure 2 shows a typical view of the main page of Connectr, with a user's friends listed on the left, and the activity stream for the selected users' associated feeds to the right (in this figure the feeds, for the purposes of the example, look suspiciously like news feeds).
Our use of the phrase "activity stream" is not to be confused with its more technical reference to an extension of the Atom feed format (http://activitystrea.ms/)—though use of Activity Stream-formatted feeds would certainly be consistent with the semantics of Connectr's stream.
Behind the scenes, the application makes use of App Engine services such as Cron jobs and task queues to update feeds asynchronously in the background, to push new content to the client, and to perform other administrative tasks.
The Connectr application serves as a running example throughout the book. It is introduced in several progressive stages, with simpler versions of the application introduced in the first few chapters, and subsequent chapters further developing the app as functionality is added. One of the goals of this book is to be a resource for a number of design patterns, approaches, and idioms, as developed within Connectr.
As we introduce the various features of Connectr, we will develop these patterns and approaches both for GWT and App Engine, as well as for communication between the client and server components of an app.
For GWT, we introduce a number of design patterns for keeping code modular and manageable, including the MVP, Event Bus, and related patterns; and describe the use of GWT's UI Binder for declarative layout specification.
For App Engine, Connectr illustrates a number of approaches for data class design and querying, including design to get around the lack of a relational database, instead leveraging the Datastore's features. We describe models for decoupled and asynchronous processing and task design, in order to make an application more robust and scalable, including use of task queues and Cron Jobs to support background processing; show how to use query cursors to break data access into manageable chunks; and introduce some useful Memcache patterns.
Additionally, Connectr illustrates some useful patterns in client-server communication, including use of App Engine's recently developed Channel API to push content from the server to the client, and use of OAuth and Google accounts to support multiple login methods, so that the app need not store user passwords.
Much GAE and GWT information is available online. Some of the most useful resources are listed in the following sections.
Google maintains galleries of sites using GAE and GWT, both independently and together. The GWT galleries in particular can be useful in providing UI design inspiration and demonstrating the different ways in which GWT widgets can be used.
Google's associated documentation sites and blogs may be the first sites you bookmark:
If you are a Twitter user, Google's Twitter accounts (for example, @app_engine, @GoogleCode) are good sources for announcements as well.
The Google Groups lists for GWT and App Engine are monitored by Google employees:
Many other blogs are good resources as well. For example:
http://gae-java-persistence.blogspot.com/, written by Google employee Max Ross, is an excellent source of GAE/Java Datastore-related information and tips.
In this chapter, we have introduced Google App Engine and GWT, and given a preview of how these two technologies combine in a powerful and useful manner.
We have also described the social-media-oriented application, Connectr, which we will develop in stages as a running example throughout the book, and taken a look ahead at some of the primary approaches and patterns that the application will be used to illustrate.
In Chapter 2, Using Eclipse and the Google Plugin, we will describe how to use the Eclipse IDE and the Google Plugin for easy Google App Engine plus GWT development and deployment, and become familiar with the different components of a GAE+GWT project.
Eclipse is an open source IDE written in Java. It employs plugins to provide support for development in various languages and frameworks, and provides particularly strong support for Java-based development.
Google makes an Eclipse plugin that provides highly useful support for Google App Engine Java (GAE/J) and Google Web Toolkit (GWT) development. When you use Eclipse and the Google plugin, you can take advantage of a number of features that make all of the stages of application development easier:
In this chapter, we will first describe the process of installing the Google Eclipse plugin, and installing its required supporting software—Java and Eclipse.
Then we'll generate a sample application, and take a first look at the anatomy of an App Engine application with GWT—what files are created where. Then, we'll explore the process of running and debugging your app locally.
Finally, we will describe the process of application deployment to the Google App Engine servers, and see how to access the App Engine Admin Console in order to manage your deployed app.
This section first describes the process of installing Java and Eclipse. Then, we will walk through the installation of the Google plugin for Eclipse, which provides support for GAE and GWT.
Google App Engine and GWT, as well as Eclipse itself, require Java. Both versions 5 and 6 of the Java Development Kit (JDK) will work with the App Engine and GWT SDKs. However, you may find that some of the example code you encounter online assumes Java 6.
If you already have a version of Java installed, then from the command line, you can type:
To check the version number. If you have Java 6 JDK installed, this will output a version number similar to "1.6.0". If you have Java 5 installed, you will see a version number like "1.5.0". The details depend upon the version that you have installed.
Macs should already have Java and its JDK installed. However, the specifics of what is installed depends upon your OS version and your machine hardware—in particular, whether you have a 32-bit or 64-bit processor.
Snow Leopard (OS X 10.6.x) includes both 32- and 64-bit versions of Java 6; your processor determines which it uses as the default. If you have a 64-bit processor, you will be using 64-bit Java by default. Leopard (OS X 10.5.x) only supports Java 6 with 64-bit processors. However, Leopard includes Java 5 as well. Thus, if you are running OS X 10.5 on a 32-bit machine, you must use Java 5.
If you want to change the version of Java that you are using by default, open the Java Preferences application, which you will find in the /Applications/Utilities directory. In the Java Applications
