简体   繁体   English

如何使用Java监视来自/到servlet的HTTP请求和响应

[英]How to monitoring HTTP requests and responses from/to servlets using Java

I'm testing a Web application written in Java EE using servlets (version 7). 我正在使用servlet(版本7)测试用Java EE编写的Web应用程序。 I'm sending a lot of HTTP requests to my servlets and I want to know when all requests are completed. 我正在向我的servlet发送大量HTTP请求,我想知道所有请求何时完成。

To send requests I'm using an executor. 发送请求我正在使用遗嘱执行人。 Also I don't know if it is the most efficient way to do this. 此外,我不知道这是否是最有效的方法。

 for (int i=0; i < numRequests; i++) {
     ExecutorService executor = Executors.newFixedThreadPool(1); 
     Future<util.Response> responseFromServlet = null;
        responseFromServlet = executor.submit(new util.Request(new URL(url)));
     if ( i !=  numRequests -1 ) {
        executor.shutdown();
     } else {
        responseFromServlet.get().getBody(); // suspensive call for last invocation
        executor.shutdown();
     }
  }

Actually the executor waits the end of the last invoked HTTP request but it usually is not the last one that completes. 实际上,执行程序等待最后一次调用的HTTP请求的结束,但它通常不是最后一个完成的。

I think that creating a new thread waiting for response of each HTTP servlet is crazy. 我认为创建一个等待每个HTTP servlet响应的新线程是疯狂的。 I can't generate 100-200-300 threads, one for each request! 我无法生成100-200-300个线程,每个请求一个!

So is there a way to understand when all servlets end their execution? 那么有没有办法了解所有servlet何时结束执行? If needed, I can modify my servlets. 如果需要,我可以修改我的servlet。

=== Edit === ===编辑===

To be more precise, here is the Request class implementation: 更确切地说,这是Request类的实现:

public class Request implements Callable<Response> {
  private URL url;

  public Request(URL url) {
      this.url = url;
  }

  @Override
  public Response call() throws Exception {
      return new Response(url.openStream());
  }
}

And this it the Response class: 这就是Response类:

public class Response {
  private InputStream body;

  public Response(InputStream body) {
      this.body = body;
  }

  public InputStream getBody() {
      return body;
  }
}

Using an executor is fine, you may want to increase the size of the ThreadPool though to have more concurrent threads performing your requests. 使用执行程序很好,您可能希望增加ThreadPool的大小,但要有更多的并发线程来执行请求。

Use a CoutnDownLatch initialised with numRequests which sits waiting for all the threads to complete. 使用用numRequests初始化的numRequests ,它等待所有线程完成。

util.Request must call latch.countDown() in its run method util.Request必须在其run方法中调用latch.countDown()

The code would look like this (handwritten - not tested) 代码看起来像这样(手写 - 未经测试)

ExecutorService executor = Executors.newFixedThreadPool(n);
final CountDownLatch latch = new CountDownLatch(numRequests); 
for (int i=0; i < numRequests; i++) {

     executor.submit(new util.Request(new URL(url), latch));
}
latch.await(someValue, TimeUnit.SECONDS)

` Edit ` 编辑

Re-implement util.Request doing something like 重新实现util.Request做类似的事情

 public class Request implements Callable<Response> {
  final private URL url;
  final private CountDownLatch latch;

  public Request(URL url, CountDownLatch latch) {
      this.url = url;
      this.latch = latch;
  }

  @Override
  public Response call() throws Exception {

       try {
          return new Response(url.openStream());
       } 
       catch (Exception e) {

          //do something useful
       }
       finally {
          latch.countDown();
       }
  }
}

You may want to consume the stream of you response before you countDown the latch to verify that you get what you expect as a response from your server. 在计算锁定时,您可能希望使用响应流,以验证您是否从服务器获得了预期的响应。

If you are using this program to perform a load test, or even otherwise, I'd highly recommend that you use Jmeter instead. 如果您使用此程序执行负载测试,或者甚至是其他方式,我强烈建议您使用Jmeter代替。 Jmeter already does what you are attempting to do and there are many plugins that will allow you to schedule the load / number of thread / time period etc. You can also monitor all HTTP requests through a variety of graphs. Jmeter已经完成了您正在尝试的操作,并且有许多插件可以让您安排负载/线程数/时间段等。您还可以通过各种图表监控所有HTTP请求。

Writing a test for your servlet should take you less than 5 minutes . 为servlet编写测试应该花费不到5分钟 The graphs are also easy to generate. 图表也很容易生成。

jmeter图

If you'd still like to use your custom program to contact the servlet, you can always limit the number of requests and back them up with a blocking queue through a threadpool executor . 如果您仍希望使用自定义程序联系servlet,则可以始终限制请求数,并通过线程池执行程序使用阻塞队列对其进行备份。

Lastly, do not modify the servlet. 最后,不要修改servlet。 You should be able to monitor it as a black box. 您应该能够将其监视为黑盒子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM