0

Premise: this question is related to a previous longer one, I think it's better posting a new short question rather than making the other one even longer. I have already read this, this, this, this and many others, and they didn't help. My question is not a duplicate of any of these, please don't vote to close, just don't answer if not interested.

I have this doGet() method in a servlet

protected void doGet(/*params*/)/*exceptions*/{

    System.out.print("Thread:"+Thread.currentThread().getName()+"    ");
    System.out.print("Session:"+request.getSession().getId()+"    ");
    System.out.println("start...");

    try {Thread.sleep(4000);} 
    catch (InterruptedException e) {}

    System.out.print("Thread:"+Thread.currentThread().getName()+"    ");
    System.out.print("Session:"+request.getSession().getId()+"    ");
    System.out.println("...end");

}

This is my output when I invoke the servlet from 3 tabs of the same browser (almost) simultaneously

18:09:17,080 [...] Thread:default task-15    Session:_5axg3aG4vaOf-5qxWJ5TWYk    start...
18:09:21,081 [...] Thread:default task-15    Session:_5axg3aG4vaOf-5qxWJ5TWYk    ...end
18:09:21,088 [...] Thread:default task-16    Session:KYQf66vtc4ezaUD1vrGIMQje    start...
18:09:25,090 [...] Thread:default task-16    Session:KYQf66vtc4ezaUD1vrGIMQje    ...end
18:09:25,101 [...] Thread:default task-17    Session:wyViZoHMGL1Mb8f9BCXO8aJK    start...
18:09:29,102 [...] Thread:default task-17    Session:wyViZoHMGL1Mb8f9BCXO8aJK    ...end

My understanding is that the 3 requests are processed sequentially because the client is using the same connection for multiple requests and the server is using a Thread Per Connection policy.

But it seems like a different thread is being used every time (15, 16 and 17). Why is this happening? What am I getting wrong? (Wildfly 8 + Chrome)

Community
  • 1
  • 1
Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48
  • so if you use different browsers (chrome + firefox), does it run simultaneously? – Jags Mar 02 '16 at 18:57
  • How do you know what the client is doing? Where's your evidence? What exactly *is* the client doing? It doesn't make sense that a single connection is used for three different HTTP sessions. – user207421 Mar 02 '16 at 21:07
  • @Jags two requests launched (almost) simultaneously from different browsers are processed in parallel by the browser – Luigi Cortese Mar 02 '16 at 22:47
  • @EJP if I would have waited more than four seconds before sending the second request, the client would have gotten his jsessionid value back, and the following requests would have had the same session value. I reckon that the requests are sent immediately by the client (so, no jsessionid associated yet) and then queued at server side, but I don't get what's the point of a sequential processing – Luigi Cortese Mar 02 '16 at 22:59
  • @EJP I'm not really able to tell what the client or the server is actually doing behind the scenes, this is why I'm hoping for someone to help me to understand – Luigi Cortese Mar 03 '16 at 09:35

1 Answers1

1

Chrome creates a new process per tab. It will create a new TCP connection from each tab, to the servlet. You can validate this by printing the clients port in your servlet code.

Since you have 3 TCP connections, and wildfly with a Thread Per Connection policy, you are witnessing expected results. One thread per connection :)

Manish Maheshwari
  • 4,045
  • 2
  • 15
  • 25
  • but then, shouldn't they be processed in parallel? – Luigi Cortese Mar 02 '16 at 21:07
  • hmm. Something seems wrong. Can you validate the behaviour of concurrent requests from curl client? – Manish Maheshwari Mar 02 '16 at 21:13
  • I mean can you also try sending the HTTP request using 3 parallel cURL requests (like curl -I http://your-request-url ), and see if those as well get processed serially at the server? – Manish Maheshwari Mar 03 '16 at 06:04
  • I've monitored my network, and it's happening exactly what you said: one TCP connection per tab. If I just "F5" the tabs I get a sequential execution, if I "Ctrl+F5" them I get a parallel processing. As per my previous (longer) question, this is what I cannot understand: why parallel execution? Moreover on different threads! – Luigi Cortese Mar 03 '16 at 12:20
  • So one thing is clear, that the behaviour is related to the client (Chrome). Looks like "Ctrl+F5" sends all requests at once. Being different tabs, these are different processes and are executed in parallel by the server using different threads. This part makes sense to me. However, it seems that Chrome, queues up requests to same host when you just "F5". Thus, the requests are received at the server serially, and are executed serially. Packet capture (aka tcpdump) could help you validate this. – Manish Maheshwari Mar 03 '16 at 12:42
  • Ok, "CTRL+F5" creates one new connection per tab/request, while "F5" reuses always the same TCP connection. But at server side, each request is processed by different threads, even when multiple requests share the same connection. This is where my confusion comes from, why having multiple threads per connection if it is "one-thread-per-connection"? I'm not even sure that it's chrome that is queueing the requests, maybe it's the server itself... what do you think? – Luigi Cortese Mar 03 '16 at 14:34
  • Could it be that the HTTP pipelining is not active, so the client (given the same underlying TCP connection) waits for a response to be received before sending the next request? In this case, the server would be using **one-thread-per-request** policy instead, this would explain multiple threads and sequential processing. Does it make any sense? – Luigi Cortese Mar 03 '16 at 18:30