在GAE /标准环境中确定服务名称

时间:2018-04-16 06:47:06

标签: java google-app-engine

有没有办法以编程方式确定代码在Google App Engine上运行的服务名称?我唯一的解决方案是在servlet中使用http请求对象并检查所请求的URL并从中提取它。但我更希望在整个服务中更全面地访问更多内容。

1 个答案:

答案 0 :(得分:0)

您可以在Api Proxy中使用getModuleId()方法。

所以,一个Servlet示例:

import com.google.apphosting.api.ApiProxy;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class HelloServlet extends HttpServlet {
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/plain");
    ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
    resp.getWriter().print("Service: ");
    resp.getWriter().println(env.getModuleId());
  }
}

这将打印服务的名称(服务以前称为模块,这就是为什么这个方法的命名方式)

相关问题