Thursday, February 21, 2013

Java 7 and Cloud Endpoints Preview

Today we are announcing two new preview features: Google App Engine Java 7 runtime support and Google Cloud Endpoints. Preview features are ‘experimental’ features on a path towards general availability.

Java 7 Runtime Support for App Engine

The App Engine Java 7 runtime allows App Engine developers to keep pace with innovations in the Java language and runtime. It is important that you begin testing your applications with the new Java 7 runtime before the complete rollout in a few months.

Some of the language features you now have access to include:

invokedynamic support, which allows developers, tools providers, and language implementations to take advantage of a new bytecode, invokedynamic, to handle method invocations efficiently even when there is no static type information. For example:

public static void invokeExample() {
   String s;
   MethodType mt; 
   MethodHandle mh;
   MethodHandles.Lookup lookup = MethodHandles.lookup();
   MethodType mt = MethodType.methodType(String.class, char.class,
   MethodHandle mh = lookup.findVirtual(String.class, "replace", mt);
   s = (String) mh.invokeExact("App Engine Java 6 runtime",'6','7');

Try-with-resources, which helps avoid memory leaks and related bugs by automatically closing resources that are used in a try-catch statement.

public static void viewTable(Connection con, String query) throws SQLException {
   try (
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery(query)
   ) {
      while ( {
         // process results
   } catch (SQLException e) {
      // con resource is auto-closed, no need to do anything here!

Flexible Type Creation when using generics, enabling you to create parameterized types more succinctly. For example, you can write:

Map> myMap = new HashMap<>();

instead of:

Map> myMap = new HashMap>();

In addition to the language features listed above, the App Engine Java 7 runtime also includes:
  • Use of String class in Switch statements.
  • Expression of binary literals using simple prefixes 0b or 0B.
  • Single catch blocks that can handle multiple exceptions.
  • Simplified varargs method invocation.
Want to get started now? Download the latest App Engine Java SDK and then check out the docs here.

Cloud Endpoints Preview

Have you ever wanted a simple way to get a cloud backend for that Android or iPhone app you are working on? Wish it was easier to expose RESTful APIs from your web application? Google Cloud Endpoints simplifies building highly scalable and flexible backends for your web and mobile applications. Use Cloud Endpoints to store application data in the cloud that spans both devices and users. You can now easily expose your own authenticated, reliable, REST-based APIs hosted from an App Engine application and consume them in your Android, iOS, or web clients. Deeply integrated authentication support allows your users to have a transparent yet secure experience accessing your services. You have access to strongly typed client libraries for your custom service optimized for Android and iOS.

To use Cloud Endpoints, you simply write a Java or Python class with methods you want to expose to web or mobile clients. You then annotate the methods with attributes that control exactly how they are represented in REST interfaces on the wire. Finally, use Cloud Endpoints to generate your strongly-typed client libraries for Android, iOS and a lightweight JavaScript library.
For example, you can create a simple class to list some important data:

public class SuperHeroes {
   public List listSuperHeroes() {
      List list = new ArrayList();
      list.add(new SuperHero ("Champion of the Obvious", "Brad Abrams"));
      list.add(new SuperHero ("Mr. Justice", "Chris Ramsdale"));

      return list;

Then, expose it over a standard REST interface with a simple attribute and a versioning pattern.

@Api(name = "superheroes", version = "v1")public class SuperHeroesV1 {   ...

Now you have a simple REST interface.

$ curl http://localhost:8888/_ah/api/superheroes/v1/superheroes
 "items": [
   "knownAs" : "Champion of the Obvious",
   "realName" : "Brad Abrams"
   "knownAs" : "Mr. Justice",
   "realName" : "Chris Ramsdale"
And you can make strongly typed calls from your Android clients:

Real result = superheroes.list().execute();

Or Objective-C iOS client:

GTLQuerySuperHeroesV1 *query = [GTLQuerySuperHeroesV1 queryForSuperHeroesList];
[service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, 
GTLSuperHeroes *object, NSError *error) {
 NSArray *items = [object items]; }];

Or the web client in JavaScript:

// ...
var ROOT = 'https://' + + '/_ah/api';
gapi.client.load('superheroes', 'v1',
                 loadSuperheroesCallback, ROOT);
// Get the list of superheroes
gapi.client.superheroes.superheroes.list().execute(function(resp) {

Read the documentation for Java or Python to discover how you can build a simple tic-tac-toe game using Cloud Endpoints.

To get started with Cloud Endpoints, download the App Engine 1.7.5 SDK and the latest Google Plugin for Eclipse. Be sure to look at the docs and follow along in the discussion forums on Stack Overflow.

For more on using Cloud Endpoints with Python, check out +Danny Hermes and +Dan Holevoet on Google Developers Live.