Servlet requests are executed sequentially for no apparent reason in Glassfish v3
I am using Glassfish 3 web profile and cannot get http workers to execute concurrent servlet requests.
This is how I noticed the problem. I made a very simple servlet that writes the current stream name to stdout and hibernates for 10 seconds:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(10000); // 10 sec
}
catch (InterruptedException ex) {
}
}
And when I run multiple concurrent requests, I can clearly see in the logs that the requests are being executed sequentially (one trace every 10 seconds).
INFO: http-thread-pool-8080-(2)
(10 seconds later...)
INFO: http-thread-pool-8080-(1)
(10 seconds later...)
INFO: http-thread-pool-8080-(2)
and etc.
All my GF settings are intact - this is the out-of-the-box configuration (default thread pool is 2 threads min, 5 max if I remember correctly).
I really don't understand why sleep () is blocking all other worker threads. Any understanding would be greatly appreciated!
a source to share
Chris nailed it in his comment. I copied your servlet, tested it like this:
package com.stackoverflow.q2755338;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
public static void main(String... args) throws Exception {
// Those are indeed called sequentially.
System.out.println("Starting to fire 3 requests in current thread...");
new TestURL().run();
new TestURL().run();
new TestURL().run();
System.out.println("Finished firing 3 requests in current thread!");
// But those are called three at once.
System.out.println("Starting to fire 3 requests in each its own thread...");
ExecutorService executor = Executors.newFixedThreadPool(3);
executor.submit(new TestURL());
executor.submit(new TestURL());
executor.submit(new TestURL());
System.out.println("Finished firing 3 requests in each its own thread!");
executor.shutdown();
}
}
class TestURL implements Runnable {
@Override
public void run() {
try {
System.out.println("Firing request...");
new URL("http://localhost:8181/JavaEE6/test").openStream();
System.out.println("Request finished!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
And the server side results were:
INFO: start: http-thread-pool-8181- (2) (10 seconds) INFO: end: http-thread-pool-8181- (2) INFO: start: http-thread-pool-8181- (1) (10 seconds) INFO: end: http-thread-pool-8181- (1) INFO: start: http-thread-pool-8181- (2) (10 seconds) INFO: end: http-thread-pool-8181- (2) INFO: start: http-thread-pool-8181- (1) INFO: start: http-thread-pool-8181- (2) INFO: start: http-thread-pool-8181- (3) (10 seconds) INFO: end: http-thread-pool-8181- (1) INFO: end: http-thread-pool-8181- (2) INFO: end: http-thread-pool-8181- (3)
a source to share