0

我尝试在 glassfish 3.1.2 上运行非常简单的 restful 服务,然后同时在浏览器的 2 个选项卡中调用它。不幸的是,似乎第二个请求被第一个阻止了。只看服务:

@Path("/task")
public class TaskService {


    static Logger log = Logger.getLogger(TaskService.class.getName())       ;


    @GET
    @Produces("text/plain")
    public String startTask(){

        log.info("REST called  -> "  + this);


        for(int k =0; k<10000; k++){
            log.info("REST called  " + k + " -> "  + this);
        }

        return "ok: ";
    }
}

第一个请求被执行,完成后,第二个请求开始:(。我也尝试过 Thread.sleep。

有任何想法吗?这是请求范围服务,因此这 2 个请求在不同的 TaskService 实例上运行。我需要一些线程池配置吗?

4

1 回答 1

1

GlassFish 负责处理对同一个@Path. 实际上,建议在使用 Java EE 编写的代码中使用线程。

这种方法

@Path("/task")
@GET
@Produces("text/plain")
public String startTask() throws InterruptedException {
    Thread.sleep(1000);  // sleep for one second
    return "ok: ";
}

睡眠一秒钟可以这样进行基准测试ab

% ab -c 20 -n 20  http://localhost:8080/WebApplication1/rest/console/task
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:        GlassFish
Server Hostname:        localhost
Server Port:            8080

Document Path:          /WebApplication1/rest/console/task
Document Length:        4 bytes

Concurrency Level:      20
Time taken for tests:   4.014 seconds
Complete requests:      20
Failed requests:        0
Write errors:           0
Total transferred:      5380 bytes
HTML transferred:       80 bytes
Requests per second:    4.98 [#/sec] (mean)
Time per request:       4014.363 [ms] (mean)
Time per request:       200.718 [ms] (mean, across all concurrent requests)
Transfer rate:          1.31 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.2      1       1
Processing:  1004 2509 1150.0   3009    4013
Waiting:     1003 2507 1150.3   3008    4012
Total:       1004 2509 1149.8   3010    4013

Percentage of the requests served within a certain time (ms)
  50%   3010
  66%   3010
  75%   4012
  80%   4012
  90%   4013
  95%   4013
  98%   4013
  99%   4013
 100%   4013 (longest request)

ab并行执行 20 个请求。如您所见,这只需要 4.014 秒。

于 2012-11-12T07:56:10.807 回答