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:
importcom.google.appengine.api.modules.ModulesService ;
importcom.google.appengine.api.modules.ModulesServiceFactory ;
ModulesService modulesApi=ModulesServiceFactory .getModulesService();
// Get the service name handling the current request.
StringcurrentModuleName=modulesApi.getCurrentModule ();
// Get the instance handling the current request.
intcurrentInstance=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:
importcom.google.appengine.api.modules.ModulesService ;
importcom.google.appengine.api.modules.ModulesServiceFactory ;
importjava.net.MalformedURLException;
importjava.net.URL;
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.io.IOException;
// ...
ModulesService modulesApi=ModulesServiceFactory .getModulesService();
// ...
try{
URLurl=newURL("http://"+
modulesApi.getVersionHostname ("my-backend-service","v1")+
"/fetch-stats");
BufferedReaderreader=newBufferedReader(
newInputStreamReader(url.openStream()));
Stringline;
while((line=reader.readLine())!=null){
// Do something...
}
reader.close();
}catch(MalformedURLExceptione){
// ...
}catch(IOExceptione){
// ...
}
You can also use the URL Fetch service.