Java 8 はサポートが終了しており、2026 年 1 月 31 日に
非推奨になります。非推奨になると、過去に組織のポリシーを使用して以前のランタイムのデプロイを再度有効にしていた場合でも、Java 8 アプリケーションをデプロイできなくなります。既存の Java 8 アプリケーションは、
非推奨になる日付以降も引き続き実行され、トラフィックを受信します。
サポートされている最新バージョンの Java に移行することをおすすめします。
Modules API の使用
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Modules API には、現在の動作環境に関する情報(モジュール、バージョン、インスタンス)を返す関数が用意されています。
Modules API には他にも、特定のモジュール、バージョン、インスタンスのアドレスを取得するための関数があります。これをアプリケーションで利用すると、リクエストをインスタンス間で送信することができ、これは開発と本番のどちらの環境でも可能です。
次のコードサンプルでは、リクエストのモジュール名とインスタンス ID を取得する方法を示します。
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();
自動スケーリングされるモジュールのインスタンス ID は、一意の base64 エンコード値として返されます(例: e4b565394caa
)。
同じアプリ内のモジュール間で通信するには、相手のモジュールのホスト名を取得します。
次のコードサンプルでは、リクエストのモジュール名とインスタンス ID を取得する方法を示します。
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) {
// ...
}
URL 取得サービスも利用できます。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-09-04 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-04 UTC。"],[[["\u003cp\u003eThe Modules API provides functions to retrieve information about the current operating environment, including module, version, and instance details.\u003c/p\u003e\n"],["\u003cp\u003eThis API enables applications to obtain the address of a specific module, version, or instance, facilitating communication between instances in both development and production.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eModulesServiceFactory\u003c/code\u003e class is used to access the Modules API, allowing developers to get the current module name and instance ID, among other information.\u003c/p\u003e\n"],["\u003cp\u003eModules can communicate with each other by fetching the hostname of the target module using \u003ccode\u003egetVersionHostname\u003c/code\u003e function, which will then require the use of a URL to retrieve it.\u003c/p\u003e\n"],["\u003cp\u003eFor those updating to the App Engine Java 11/17 runtime, there is a specific migration guide that outlines the migration options for legacy bundled services.\u003c/p\u003e\n"]]],[],null,["# Using the Modules API\n\n| This API is supported for first-generation runtimes and can be used when [upgrading to corresponding second-generation runtimes](/appengine/docs/standard/\n| java-gen2\n|\n| /services/access). If you are updating to the App Engine Java 11/17 runtime, refer to the [migration guide](/appengine/migration-center/standard/migrate-to-second-gen/java-differences) to learn about your migration options for legacy bundled services.\n\nThe Modules API provides functions that return information about the current\noperating environment (module, version, and instance).\n\nThe Modules API also has functions that retrieve the address of a module, a\nversion, or an instance. This allows an application to send requests from one\ninstance to another, in both the development and production environments.\n\nThe following code sample shows how to get the module name and instance id for\na request: \n\n import com.google.appengine.api.modules.https://cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.modules.ModulesService.html;\n import com.google.appengine.api.modules.https://cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.modules.ModulesServiceFactory.html;\n\n https://cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.modules.ModulesService.html modulesApi = https://cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.modules.ModulesServiceFactory.html.getModulesService();\n\n // Get the service name handling the current request.\n String currentModuleName = modulesApi.https://cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.modules.ModulesService.html#com_google_appengine_api_modules_ModulesService_getCurrentModule__();\n // Get the instance handling the current request.\n int currentInstance = modulesApi.getCurrentInstance();\n\nThe instance ID of an automatic scaled module will be returned as a unique\nbase64 encoded value, e.g. `e4b565394caa`.\n\nYou can communicate between modules in the same app by fetching the hostname of\nthe target module:\n\nThe following code sample shows how to get the module name and instance id for\na request: \n\n import com.google.appengine.api.modules.https://cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.modules.ModulesService.html;\n import com.google.appengine.api.modules.https://cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.modules.ModulesServiceFactory.html;\n\n import java.net.MalformedURLException;\n import java.net.URL;\n import java.io.BufferedReader;\n import java.io.InputStreamReader;\n import java.io.IOException;\n\n // ...\n\n https://cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.modules.ModulesService.html modulesApi = https://cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.modules.ModulesServiceFactory.html.getModulesService();\n\n // ...\n try {\n URL url = new URL(\"http://\" +\n modulesApi.https://cloud.google.com/appengine/docs/standard/java-gen2/reference/services/bundled/latest/com.google.appengine.api.modules.ModulesService.html#com_google_appengine_api_modules_ModulesService_getVersionHostname_java_lang_String_java_lang_String_(\"my-backend-service\",\"v1\") +\n \"/fetch-stats\");\n BufferedReader reader = new BufferedReader(\n new InputStreamReader(url.openStream()));\n String line;\n\n while ((line = reader.readLine()) != null) {\n // Do something...\n }\n reader.close();\n\n } catch (MalformedURLException e) {\n // ...\n } catch (IOException e) {\n // ...\n }\n\nYou can also use the [URL Fetch](/appengine/docs/legacy/standard/java/issue-requests) service."]]