简体   繁体   中英

glassfish jax-rs multithreading

i try to run very simple restful service on glassfish 3.1.2 and then call it within 2 tabs in browser at the same time. Unfortunately it seems that second request is blocked by first. Just look at service:

@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: ";
    }
}

First request is executed and after it is finished, second request starts :(. I tried with Thread.sleep too.

Any ideas? This is reqest scope service, so these 2 request operate on diffrent TaskService instances. Do i need some thread pool configuration?

GlassFish takes care of multiple requests to the same @Path . In fact, it is not recommended to use threads in code written using Java EE.

This method

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

which sleeps for one second can be benchmarked with ab like this:

% 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 executes 20 requests all in parallel. As you can see, this takes only 4.014 seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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