Using the Modules API

The Modules API provides functions that return information about the current operating environment (module, version, and instance).

The Modules API also has functions that retrieve the address of a module, a version, or an instance. This allows an application to send requests from one instance to another, in both the development and production environments.

The following code sample shows how to get the module name and instance id for a request:

import com.google.appengine.api.modules.ModulesService;
import com.google.appengine.api.modules.ModulesServiceFactory;

ModulesService modulesApi = ModulesServiceFactory.getModulesService();

// Get the service name handling the current request.
String currentModuleName = modulesApi.getCurrentModule();
// Get the instance handling the current request.
int currentInstance = modulesApi.getCurrentInstance();

The instance ID of an automatic scaled module will be returned as a unique base64 encoded value, e.g. e4b565394caa.

You can communicate between modules in the same app by fetching the hostname of the target module:

The following code sample shows how to get the module name and instance id for a request:

import com.google.appengine.api.modules.ModulesService;
import com.google.appengine.api.modules.ModulesServiceFactory;

import java.net.MalformedURLException;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

// ...

ModulesService modulesApi = ModulesServiceFactory.getModulesService();

// ...
    try {
        URL url = new URL("http://" +
            modulesApi.getVersionHostname("my-backend-service","v1") +
            "/fetch-stats");
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(url.openStream()));
        String line;

        while ((line = reader.readLine()) != null) {
            // Do something...
        }
        reader.close();

    } catch (MalformedURLException e) {
        // ...
    } catch (IOException e) {
        // ...
    }

You can also use the URL Fetch service.

Send feedback about...