Running a Web service on Google App Engine
Google's App Engine for building Web applications in the cloud is beginning to support Java servlet-based Web services. It's still under development, but you can experiment using Eclipse support or command-line utilities in the SDK.
App Engine is Google's service for building Web applications in the cloud. The initial version only supported Python, but Java servlet support has recently entered early release. Developers can experiment with a quota of free resources, so naturally I was interested. I downloaded the software development kit (SDK) Version 1.2.1 and got the Google plug-in for Eclipse 3.4.2. Documentation for the Java version has not quite caught up with the quality of the Python documentation, but you'll find what you need with a little digging.
App Engine Java servlet environment
The runtime environment for the App Engine is Java 6 with the standard libraries. The structure of a Java servlet in the App Engine environment follows normal Java practice for Servlet API 2.5 with certain restrictions. All of the static resources normally served directly by the servlet container -- such as HTML files, CSS files and images -- are placed according to convention under the root directory of the application. For example, you might have a logo image addressed as "/graphics/mylogo.gif."
Resources that are private to the application and are only read, never written, such as jar libraries, class files and configuration files, are stored under the usual "/WEB-INF" directory. For obvious security reasons, Google can't let an application wander unrestrained through the file system, so if your application reads and writes files, you will have to make some major adjustments to use the persistent data-storage methods that App Engine supports. Furthermore, your servlet must complete a response within 30 seconds and may not start any new processes.
Scaling to an increased user-request load is handled automatically by creating new instances of an application and removing them when the load decreases. The App Engine environment ensures that all instances will have access to correctly updated common resources. Here are some of the Google specific tools your application can make use of.
- Google account integration. By default, your application will be open to the internet, but you can use Google accounts to control access by authenticated users.
- Mail service. Your application can send normal email using the standard JavaMail application programming interface (API), subject to security restrictions on mime type and volume limits.
- Memcache for performance. The Memcache API lets you cache data that may be expensive to compute. Cached data is available to all instances of your application. The current maximum size of a cached value is 1 MB.
- Internet communication. You can use standard Java APIs for connecting to other Internet locations, but the actual connection will be made by Google's "URL Fetch" networking proxy, which imposes security and size limits.
- Java Datastore service. The Google BigTable database/distributed storage system is not provided directly. Instead, the Datastore system for storing and retrieving Java objects is built on top of BigTable. Datastore supports two popular APIs for Java object persistence: Java Data Objects (JDO) and the Java Persistence API. Datastore is supposed to be able to support very large data collections.
- Application monitoring. Google provides a convenient Web "dashboard" for monitoring your application. You can monitor usage, consumption of resources and error logging.
The Eclipse development environment for App Engine
Currently, plug-ins are available only for Eclipse 3.3 and 3.4 -- as of this writing, there are no plug-ins for the newest 3.5 "Galileo" Eclipse. If you are downloading and installing Eclipse for the first time, go for the "Java EE Developers" package. The Google plug-ins provide extensive support for simulating the App Engine environment. Selecting the "New Web Application Project" option automatically creates the proper directory structure with the correct Java library files.
Creating an example service
As an example, I decided to try porting a servlet-based Web service to App Engine. The service is a simple one I have used for several example coding projects. It lets you look up lists of words that sound alike using the "metaphone" phonetic code algorithm. It uses some serialized Java objects to hold the lookup structures for thousands of precomputed phonetic codes. Since these are read-only objects, they can be stored with other application resources.
Getting a Google App Engine account
Since I've had an account with Google for years, I figured this should be the simplest part of the process, but I was wrong. For some reason, Google wanted to verify my account by means of a SMS message to my cell phone -- I don't have a cell phone! With help from a cell phone owner, I got my verification code, and I was able to proceed. I suspect that this SMS capability will find its way into the API soon as Google integrates App Engine with other Google projects. With the verification accomplished, my normal Google account let me request an application name. I chose "phonetic01" so the full URL to the service is "phonetic01.appspot.com."
Installing to Google's system
The Eclipse App Engine plug-in guides you through configuration of your application and uploads all of the components. In addition to the usual web.xml deployment descriptor, App Engine uses an AppEngine-web.xml file to associate a name and version number with the code. Eclipse makes modification of components and redeployment quite easy and fast.
After the application had been inactive for a while, I found that response time to a request took a few seconds, presumably because App Engine does not keep every application loaded all the time. Subsequent requests were quite fast.
Conclusions
Although it is still under development, Google's App Engine support for Java servlet-based Web services is very usable. With good Eclipse support, it is easy to start experimenting. If you don't have Eclipse, the SDK has command-line utilities to support development and deployment with your normal tools.
Resources
- Google's starting page for App Engine
- The Phonetic lookup service on App Engine
- Overview of the Java Data Object (JDO) API
- The Java Persistance API
- Eclipse 3.4 download starting page