Determine AppEngine for Java environment programma

2019-02-09 05:15发布

Is there a way to tell programmatically at run-time whether a Google App Engine application is running locally vs. hosted? I'm looking for a way to call some custom stub code when running in a local development environment and make different calls when running hosted.

2条回答
女痞
2楼-- · 2019-02-09 05:41

For the newer flex variant of AppEngine, you query an env-var that Google sets:

String gaeAppId = System.getenv("GCLOUD_PROJECT"));
// gaeAppId contains either the name of app/project, or null (not running on App Engine)
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-09 05:45

You can use com.google.appengine.api.utils.SystemProperty in AppEngine 1.3.

import com.google.appengine.api.utils.SystemProperty;
import static com.google.appengine.api.utils.SystemProperty.environment;
import static com.google.appengine.api.utils.SystemProperty.Environment.Value.Development;
import static com.google.appengine.api.utils.SystemProperty.Environment.Value.Production;

SystemProperty.Environment.Value env = environment.value();
if (env  == Production) {
      //prod only code
      ...
} else if(env == Development) {
      //dev only code
      ...
}
查看更多
登录 后发表回答