• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HttpUrlConnection called by multiple threads

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Is HttpUrlConnection thread safe? I.e. if I have an HttpConnection instance connected to a server and this instance is used by different threads (e.g.try to send a POST concurrently) how will this situation be handled by HttpUrlConnection? a) Will they send the POSTs serially, or b) the first thread sends the POST, gets the response and then the second thread will send the POST? If they send the POSTs serially, this means multiple active POSTs to the same tcp connection. Is this allowed? Can it be handled by a server?

Thanks
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The javadocs state "Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server may be transparently shared by other instances." While that's not quite clear on the matter of thread-safety, it suggests to me that it should not be shared between threads.
 
Jim Akmer
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Yes I have read this and could not make much of this statement also.
My main problem is whether it is possible to "push" multiple POST requests over the same tcp connection for performance reasons.
I was thinking maybe the httpurlconnection already handles this
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, sending several requests over the same connection, and using an HttpURLConnection multi-threaded are two different questions. The former is possible through the HTTP keep-alive feature.
 
Jim Akmer
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keep alive is just an indication to the server not to close the connection.
My problem is whether it is possible to "push" multiple active POSTs (i.e. without waiting for the response for each POST, eg POST1,POST2 and then get response for POST1, response from POST2) over the same tcp connection.
I was wondering if this is effectively the outcome of sharing an httpurlconnection among threads
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that's not possible. Before the first response has come back you can't send the next request. That's a feature of HTTP, and not any particular implementation.

What prompted this question - trying to maximize throughput, i.e., the number for requests per time unit?
 
Jim Akmer
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to implement a web service client with high performance requirements. I was thinking to keep persistent connections to avoid 3-way handshake overhead and was wondering if it would be possible to multiplex the requests over the same connection. The reason for this was that if i opened a new connections for each request, I would hit a limit at the server and if this limit was low (e.g. 2 connections for the same client) then the threads would block.
I suspected that it is not possible, could not found it stated explicitly somewhere though. Do you have an explicit reference Ulf?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Section 1.4 of the HTTP spec talks about the request/response semantics of HTTP; that may be as close as it gets to an explicit statement.

Of course, that only applies to single connections. Servlet containers -on which most WS implementations are based- can handle parallel independent connections, so if you were to use several HttpUrlConnections for the same URL, you should be able to increase throughput. The "the underlying network connection to the HTTP server may be transparently shared by other instances" part sounds a bit weird in that context, but I doubt that's what will happens if you have multiple threads. A test will tell, though.
 
Jim Akmer
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems that HTTP1.1 allows pipelining (8.1.2.2 Pipelining from RFC2616). Clients SHOULD not send POSTs pipelined
I am not sure if the shared use of httpurlconnection though will have the desired result
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As an aside, please BeForthrightWhenCrossPostingToOtherSites, on java.net or elsewhere; it's the polite thing to do.
 
Jim Akmer
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok ulf. Well I apologise to you since you were interested in helping me. As an aside, I really needed info on this. Your responses were fine.
I will close this thread as answered.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're looking for a high performance socket framework, you might be interested in JBoss Netty. They have some well worked examples of both HTTP servers and clients.

HTH
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic