Java 在Glassfish v3中,Servlet请求无明显原因地按顺序执行
我使用的是GlassFish3Web配置文件,无法让http工作程序在servlet上并发执行请求 这就是我观察问题的方式。我制作了一个非常简单的servlet,它将当前线程名称写入标准输出并休眠10秒:Java 在Glassfish v3中,Servlet请求无明显原因地按顺序执行,java,servlets,multithreading,jakarta-ee,glassfish-3,Java,Servlets,Multithreading,Jakarta Ee,Glassfish 3,我使用的是GlassFish3Web配置文件,无法让http工作程序在servlet上并发执行请求 这就是我观察问题的方式。我制作了一个非常简单的servlet,它将当前线程名称写入标准输出并休眠10秒: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(Thread
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秒一次跟踪)
等等
我所有的GF设置都没有改变-这是开箱即用的配置(默认的线程池最小为2个线程,如果我调用正确,最大为5个)
我真的不明白为什么sleep()会阻止所有其他工作线程。如有任何见解,将不胜感激 克里斯在他的评论中明确了这一点。我复制了您的servlet,并对其进行了如下测试:
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)
信息:开始: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)
servlet是否以单线程模式运行?
这将出现在web.xml中,最好看看在测试中调用它的客户机代码。您的代码在这里看起来很好。可能是测试客户机出错了,而不是您的服务器代码。这肯定是客户机,这是绝对正常的:一旦达到最大连接数,您的日志将看起来像是按顺序运行的。顺便说一句,我不是在猜测,我有完全相同的设置:GFv3,默认设置,甚至是相同的servlet(在我的情况下睡眠5秒而不是10秒);我(愚蠢地)用浏览器发送手动请求。糟糕的测试实践。谢谢:)我建议使用更专业的webapp性能测试。我正在使用浏览器(chrome)手动发送请求。。。如果其中一个请求被阻塞,那么它实际上不会发送多个请求?在任何情况下,你的客户端脚本都适合我。谢谢,不客气。不要错过另一条评论中我的JMeter建议;)是的,我一定会调查的。这是我项目的开始,但是,嘿,早期基准测试,经常基准测试:) 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)