在 Google App Engine JAVA 中创建多个实例

发布于 2024-12-02 22:12:24 字数 1569 浏览 2 评论 0原文

我正在使用 JAVA 测试 Google App Engine,我想测试并行运行多个实例。但是,我不知道如何激活多个实例。

我尝试在不同的浏览器中运行此 Servlet(我也尝试在不同的计算机中运行并发调用 - 使用不同的 IP)

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import java.math.*;
public class SimpleServlet extends HttpServlet
{
  //A variable that is NOT thread-safe!

  public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
  {
    doPost(req, resp);
  }
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
  {
     int counter = 0;
    resp.getWriter().println("<HTML><BODY>");
    resp.getWriter().println(this + ": <br>");
    for (int c = 0; c < 10; c++)
    {
      resp.getWriter().println("Counter = " + counter + "<BR>");
      try
      {
        //Thread.currentThread().sleep( 500);
          for (int e=0;e<9999;e++) {
          }
        Thread.sleep(500);

        counter++;
      }
      catch (InterruptedException exc) {
        resp.getWriter().println("I can't sleep<BR>");
      }
    }
    resp.getWriter().println("</BODY></HTML>");
  }
}

每个 Servlet 需要 5 秒来处理,但请求会集中在单个实例中,例如,如果我运行此 Servlet 10 次然后花了50秒来处理最后一个。

我尝试使用:

<threadsafe>true</threadsafe>

但它什么也没做。

我尝试更改设置

settings

但没有运气。

在此处输入图像描述

那么,我能做什么?

I am testing Google App Engine using JAVA and I want to test to run multiples instances in parallel. However, I don't know how to activate multiples instances.

I tried running this Servlet in different browser (also I tried running concurrent calls in a different machine - with different IP)

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import java.math.*;
public class SimpleServlet extends HttpServlet
{
  //A variable that is NOT thread-safe!

  public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
  {
    doPost(req, resp);
  }
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
  {
     int counter = 0;
    resp.getWriter().println("<HTML><BODY>");
    resp.getWriter().println(this + ": <br>");
    for (int c = 0; c < 10; c++)
    {
      resp.getWriter().println("Counter = " + counter + "<BR>");
      try
      {
        //Thread.currentThread().sleep( 500);
          for (int e=0;e<9999;e++) {
          }
        Thread.sleep(500);

        counter++;
      }
      catch (InterruptedException exc) {
        resp.getWriter().println("I can't sleep<BR>");
      }
    }
    resp.getWriter().println("</BODY></HTML>");
  }
}

Every Servlet took 5 seconds to process but the requests are pooled in a single instance, for example if I run 10 times this servlet then it took 50 seconds to process the last one.

I tried using:

<threadsafe>true</threadsafe>

but it do nothing.

I tried changing the settings

settings

without luck.

enter image description here

So, what can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弱骨蛰伏 2024-12-09 22:12:24

通过设置 true,使您的应用程序能够处理同一实例内的并发请求。因此,如果您需要测试应用程序在多个活动实例下的行为方式,最好禁用此选项。

此外,您还可以创建流量生成器来向您的应用程序发出大量请求,从而导致“唤醒”多个实例。

By setting <threadsafe>true</threadsafe>, enables your application to handle concurrent requests inside the same instance. So, if you need to test how your application behaves with multiple instances active, you better disable this option.

Also, you can create traffic generator to make lots of requests to your app and thus cause the "wake up" of more that one instances.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文