在Glassfish v3中,没有明显原因依次执行Servlet请求

编程入门 行业动态 更新时间:2024-10-28 06:32:27
本文介绍了在Glassfish v3中,没有明显原因依次执行Servlet请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Glassfish 3 Web配置文件,无法让http worker在servlet上执行并发请求。

I'm using Glassfish 3 Web profile and can't get http workers to execute concurrently requests on a servlet.

这就是我观察问题的方法。我做了一个非常简单的servlet,它将当前线程名称写入标准输出并休眠10秒:

This is how i observed the problem. I've made a very simple servlet, that writes the current thread name to the standard output and sleep 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) { } }

当我同时运行多个请求时,我清楚地在日志中看到请求是按顺序执行的(每10秒一个跟踪)。

And when i'm running several simultaneous requests, I clearly see in the logs that the requests are sequentially executed (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)

等。

我所有的GF设置都不受影响 - 这是开箱即用的配置(默认线程池最少2个线程,如果我没记错的话,最多5个)。

All my GF settings are untouched - it's the out-of-the-box config (the default thread pool is 2 threads min, 5 max if I recall properly).

我真的不明白为什么睡觉()阻止所有其他工作线程。非常感谢任何见解!

I really don't understand why the sleep() block all the others worker threads. Any insight would be greatly appreciated !

推荐答案

克里斯在评论中将其钉死了。我复制了你的servlet,按如下方式测试:

Chris nailed it down in his comment. I copied your servlet, tested it as follows:

package com.stackoverflow.q2755338; import java.io.IOException; import java.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("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)

更多推荐

在Glassfish v3中,没有明显原因依次执行Servlet请求

本文发布于:2023-11-24 01:21:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623461.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:原因   Glassfish   Servlet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!