2

Glassfish 3 Web プロファイルを使用していますが、サーブレットで同時に要求を実行する 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 秒ごとに 1 つのトレース)。

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() が他のすべてのワーカースレッドをブロックする理由が本当にわかりません。どんな洞察も大歓迎です!

4

2 に答える 2

2

クリスはコメントでそれを明確にしました。私はあなたのサーブレットをコピーし、次のようにテストしました:

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

}

そして、サーバー側での結果は次のとおりです。

情報: 開始: http-thread-pool-8181-(2)
(10秒)
情報: 終了: http-thread-pool-8181-(2)
情報: 開始: http-thread-pool-8181-(1)
(10秒)
情報: 終了: http-thread-pool-8181-(1)
情報: 開始: http-thread-pool-8181-(2)
(10秒)
情報: 終了: http-thread-pool-8181-(2)

情報: 開始: http-thread-pool-8181-(1)
情報: 開始: http-thread-pool-8181-(2)
情報: 開始: http-thread-pool-8181-(3)
(10秒)
情報: 終了: http-thread-pool-8181-(1)
情報: 終了: http-thread-pool-8181-(2)
情報: 終了: http-thread-pool-8181-(3)
于 2010-05-02T23:28:11.973 に答える
1

サーブレットをシングルスレッド モードで実行していますか?
これは web.xml にあります

于 2010-05-02T23:31:01.760 に答える