NamespaceManager.#set("a-namespace");
MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();
// Store record in namespace "a-namespace"
memcache.put("key1", "value1");
NamespaceManager.#set("other-namespace");
// Store record in namespace "other-namespace"
memcache.put("key2", "value2");
MemcacheService boundMemcache =
MemcacheServiceFactory.getMemcacheService("specific-namespace");
NamespaceManager.#set("whatever-namespace");
// The record is still stored in namespace "specific-namespace".
boundMemcache.put("key3", "value3");
MemcacheService memcache (in the above example) uses the current
namespace and key1 will be stored in namespace "a-namespace",
while key2 is stored in namespace "other-namespace". It is
possible to override the current namespace and store data in specific
namespace. In the above example key3 is stored in namespace
"specific-namespace".
The Task Queue com.google.appengine.api.taskqueue.Queue#add
methods will forward the NamespaceManager settings into the task
being added causing the added task to be executed with the same current
namespace as the task creator. The exception is that an unset current
namespace (i.e. NamespaceManager#get() returns null) will be
forwarded as an empty ("") namespace to the created task's requests.
See Also: Multitenancy and the Namespaces Java API. In Google App Engine Developer's Guide.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThis content details the Google App Engine API's capabilities for server lifecycle management, threading, and namespaces/multitenancy.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eNamespaceManager\u003c/code\u003e class enables manipulation of the current namespace, affecting various APIs like Datastore, Memcache, and Task Queue.\u003c/p\u003e\n"],["\u003cp\u003eWhen a namespace-aware class is created, it determines the namespace via \u003ccode\u003eNamespaceManager#get()\u003c/code\u003e, using the empty namespace if the current namespace is unset.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eThreadManager\u003c/code\u003e provides a \u003ccode\u003eThreadFactory\u003c/code\u003e for creating new threads within App Engine applications, but thread usage has drawbacks that should be considered.\u003c/p\u003e\n"],["\u003cp\u003eTask Queue operations forward \u003ccode\u003eNamespaceManager\u003c/code\u003e settings to new tasks, ensuring they execute within the same namespace as their creator, with an unset namespace being passed as empty.\u003c/p\u003e\n"]]],[],null,["# Package com.google.appengine.api (2.0.0)\n\nProvides facilities for server lifecycle management, threading and namespaces/multitenancy. These\nfacilities, along with the services defined in subpackages, make up the Google App Engine API.\nSee Also: [Multitenancy and the Namespaces Java API in the *Google App Engine Developer's Guide*](http://cloud.google.com/appengine/docs/java/multitenancy/).\n\nClasses\n-------\n\n### [LifecycleManager](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.LifecycleManager)\n\n### [NamespaceManager](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager)\n\nProvides functions for manipulating the current namespace used for\nApp Engine APIs.\n\nThe \"current namespace\" is the string that is returned by\n[#get()](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager#com_google_appengine_api_NamespaceManager_get__) and used by a number of APIs including Datatore,\nMemcache and Task Queue.\n\nWhen a namespace aware class (e.g.,\n[com.google.appengine.api.datastore.Key](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Key),\n[com.google.appengine.api.datastore.Query](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.datastore.Query) and\n[com.google.appengine.api.memcache.MemcacheService](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.memcache.MemcacheService)) is constructed, it\ndetermines which namespace will be used by calling\n[NamespaceManager#get()](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager#com_google_appengine_api_NamespaceManager_get__) if it is otherwise unspecified. If\n[NamespaceManager#get()](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager#com_google_appengine_api_NamespaceManager_get__) returns null, the current namespace is unset\nand these APIs will use the empty (\"\") namespace in its place.\n\nExample: \n\n```\n NamespaceManager.#set(\"a-namespace\");\n MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();\n // Store record in namespace \"a-namespace\"\n memcache.put(\"key1\", \"value1\");\n /appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager./appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager#com_google_appengine_api_NamespaceManager_set_(\"other-namespace\");\n // Store record in namespace \"other-namespace\"\n memcache.put(\"key2\", \"value2\");\n MemcacheService boundMemcache =\n MemcacheServiceFactory.getMemcacheService(\"specific-namespace\");\n /appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager./appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager#com_google_appengine_api_NamespaceManager_set_(\"whatever-namespace\");\n // The record is still stored in namespace \"specific-namespace\".\n boundMemcache.put(\"key3\", \"value3\");\n \n```\n\n\u003cbr /\u003e\n\nMemcacheService `memcache` (in the above example) uses the current\nnamespace and `key1` will be stored in namespace `\"a-namespace\"`,\nwhile `key2` is stored in namespace `\"other-namespace\"`. It is\npossible to override the current namespace and store data in specific\nnamespace. In the above example `key3` is stored in namespace\n`\"specific-namespace\"`.\n\nThe Task Queue com.google.appengine.api.taskqueue.Queue#add\nmethods will forward the [NamespaceManager](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager) settings into the task\nbeing added causing the added task to be executed with the same current\nnamespace as the task creator. The exception is that an unset current\nnamespace (i.e. [NamespaceManager#get()](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.NamespaceManager#com_google_appengine_api_NamespaceManager_get__) returns null) will be\nforwarded as an empty (\"\") namespace to the created task's requests.\nSee Also: [Multitenancy and the Namespaces Java API. In *Google App Engine Developer's Guide*](http://cloud.google.com/appengine/docs/java/multitenancy/).\n\n### [ThreadManager](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.ThreadManager)\n\n`ThreadManager` exposes a [ThreadFactory](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadFactory.html) that allows\nApp Engine applications to spawn new threads.\n\nRefer to [this discussion of threads](https://cloud.google.com/appengine/docs/java/runtime#threads) for drawbacks of thread usage and possible\nalternatives.\n\nInterfaces\n----------\n\n### [LifecycleManager.ShutdownHook](/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.LifecycleManager.ShutdownHook)"]]