简体   繁体   中英

How does URLConnection in java reuses the connection from the pool

Can any one help me with a sample code of how the URLConnection reuses the connection?

URLConnection con = new URL("http://www.someurl.com").openConnection();

I am using the above code to make a URLConnection. For the first time, the URLConnection will open a new connection to the specified URL. But after that it will reuse the existing connection from the pool. Is there any to prove this (I mean through println()) ?

Actually is there any way to retrieve connection reused status from the pool?

From the java.net.URL javadoc:

Returns a URLConnection object that represents a connection to the remote object referred to by the URL.

A new connection is opened every time by calling the openConnection method of the protocol handler for this URL.

If for the URL's protocol (such as HTTP or JAR), there exists a public, specialized URLConnection subclass belonging to one of the following packages or one of their subpackages: java.lang, java.io, java.util, java.net, the connection returned will be of that subclass. For example, for HTTP an HttpURLConnection will be returned , and for JAR a JarURLConnection will be returned.

So for a http protocol url it will return a HttpURLConnection

From the java.net.HttpURLConnection javadoc:

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

So the underlining tcp connection might be pooled

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM