Google App Engine Java Capabilities and Namespaces API

Capabilities API

With the Capabilities API, your application can detect outages and scheduled downtime for specific API capabilities. You can use this API to reduce downtime in your application by detecting when a capability is unavailable and then bypassing it.

How do we handle this, what’s the tradeoff?

1. Elegantly: create an aspect that cross cuts all data store writes, and checks for capability. Fail with friendly error message.
2. Not so elegantly: add capability checking into the repository code before every write. Fail with friendly error message.
3. Quickly: write filter that wraps the entire transaction and checks for data store writes.

How can we test this on a local environment?

1. No API hooks. Currently, I have not been able to find or use GAE Java proxies to test the capability call.
2. The solution would be to create different builds one with a Mocked Capability Service that is able to return the CapabilityStatus.DISABLED flags and test.

Due to time reasons, I went for the quick approach. I will try to revisit this later and incorporate AspectJ or Google Juice to leverage aspects for this obvious cross-cutting concern.

Map<Capability, Boolean> apis = new HashMap<Capability, Boolean>();

public void init(FilterConfig filterConfig) throws ServletException {
    apis.put(Capability.DATASTORE, true); 
    apis.put(Capability.MEMCACHE,  false);  
}

public void doFilter(ServletRequest req, ServletResponse res, 
        FilterChain chain) throws IOException, ServletException {

    HttpServletResponse httpResponse = (HttpServletResponse)res;
    HttpServletRequest httpRequest = (HttpServletRequest)req;

    CapabilitiesService service = CapabilitiesServiceFactory.getCapabilitiesService();

    for(Map.Entry api : apis.entrySet()) {

        if(api.getValue()) {
            CapabilityStatus status =  service.getStatus(api.getKey()).getStatus();

            if (status != CapabilityStatus.ENABLED) {
                log.warn("API " + api.getKey().getPackageName() + ":" + 
                        api.getKey().getName() + " has been disabled!");
                httpResponse.sendRedirect(NEPAL_RES_HOST + "/html/read-only.html");
                return; 
            }
        }   
    }  
    chain.doFilter(req, res);  
}

Namespaces API

Before we can talk about this API, let’s introduce the concept of Multitenancy.

One of the most compelling uses of Namespaces API is multitenancy. Multitenancy is the name given to a software architecture in which one instance of an application, running on a remote server, serves many client organizations (also known as tenants).

Multitenancy simplifies administration and provisioning of tenants. You can provide a more streamlined, customized user experience, and also aggregate different silos of data under a single database schema. As a result, your applications become more scalable as well as more cost-effective as you scale

Data becomes easier to segregate and analyze across tenants because all tenants share the same database schema.

Multitenancy is different than multi-instance:

  • In multi-instance different software instances are set up for different client organizations.
  • In multi-instance the application is typically not aware of the difference of customers, whereas in multitenancy the application is reposible for segregating and partitioning the data.

The Namespaces API in Google App Engine makes it easy to create silos of your Google App Engine data. This API is implemented via a new package called the Namespace Manager.

When you set a namespace in the namespace manager, these APIs get the current namespace and use it globally.

Any App Engine request can access any namespace, leaving the application to enforce an access control policy across namespaces. In this approach, it is the application which is responsible for setting the working namespace.

Namespace-aware APIs:

1. Datastore
2. Memcache
3. Task Queue

Using the Namespaces API, you can easily partition data across tenants simply by specifying a unique namespace string for each tenant. You simply set the namespace for each tenant globally using the Namespace Manager. The namespace-enabled APIs always use the current namespace by default.

In the datastore, the Namespace Manager will apply the namespace to every Key and Query object.

There is no Java API to explicitly set the namespace of a Key or Query, all happens through the Namespace Manager.

Reference : Google App Engine Java Capabilities and Namespaces API from our JCG partner at ReflectiveThought.

Related Articles:

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


2 × = eighteen



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books