Запросы сервлета выполняются последовательно без видимой причины в Glassfish v3 - PullRequest
2 голосов
/ 03 мая 2010

Я использую веб-профиль Glassfish 3 и не могу заставить работников http одновременно выполнять запросы к сервлету.

Вот как я заметил проблему. Я сделал очень простой сервлет, который записывает текущее имя потока в стандартный вывод и спит в течение 10 секунд:

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) {
    }
}

И когда я запускаю несколько одновременных запросов, я ясно вижу в журналах, что запросы выполняются последовательно (одна трассировка каждые 10 секунд).

INFO: http-thread-pool-8080-(2)
(10 seconds later...)
INFO: http-thread-pool-8080-(1)
(10 seconds later...)
INFO: http-thread-pool-8080-(2)

и т.д.

Все мои настройки GF не затронуты - это готовая конфигурация (пул потоков по умолчанию составляет 2 потока, минимум 5, если я правильно помню).

Я действительно не понимаю, почему sleep () блокирует все остальные рабочие потоки. Любая идея будет принята с благодарностью!

Ответы [ 2 ]

2 голосов
/ 03 мая 2010

Крис прибил это в своем комментарии. Я скопировал ваш сервлет, проверил его следующим образом:

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();
        }
    }

}

И результаты на стороне сервера были:

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)
1 голос
/ 03 мая 2010

Работает ли сервлет в однопоточном режиме?
Это будет в вашем web.xml

...