Recently I was writing code like the following Sample.java
at home.
Sample.java
HttpURLConnection urlconn = null;
PurchaseItemInfo itemInfo = null;
try {
URL url = new URL([URL]);
urlconn = (HttpURLConnection) url.openConnection();
urlconn.setRequestMethod("GET");
urlconn.setInstanceFollowRedirects(false);
urlconn.connect();
try (InputStreamReader inReader = new InputStreamReader(urlconn.getInputStream());
BufferedReader reader = new BufferedReader(inReader);) {
doSomething();
}
} finally {
urlconn.disconnect();
}
As I wrote this, I was looking into HttpURLConnection, but at the end I found both a sample that called disconnect () and a sample that didn't. Normally, if you open () some resource (FileInputStream or Connection) and then try-with-resources to AutoClose it, or explicitly close () it in the finally clause, it will occupy the resource until it becomes the target of GC. I thought it was a rule of thumb to always close (). I thought it was the same for HttpURLConnection, but I can't use try-with-resources because I haven't implemented the AutoCloseable interface (* Because you pointed out, [at the end of this article](https: // qiita) .com / taumax / items / 1e3b03771fe73adfaf95 # 20191111-% E3% 81% 94% E6% 8C% 87% E6% 91% 98% E3% 82% 92% E3% 81% 84% E3% 81% 9F% E3% 81% A0% E3% 81% 84% E3% 81% 9F% E3% 81% AE% E3% 81% A7% E4% BF% AE% E6% AD% A3) Maybe it's a little different? I started thinking, so I looked it up.
HttpURLConnection (API specification) stated as follows.
Individual HttpURLConnection instances are used when making a single request, but the network connection to the HTTP server behind them can be transparently shared with other instances. Calling the close () method on the HttpURLConnection's InputStream or OutputStream after the request may free the network resources associated with that instance, but has no effect on the shared persistent connection. .. If you call the disconnect () method, the socket you were using could be closed if the persistent connection was idle at that time.
In addition to this, when I looked at the sites I wrote for reference, if I disconnect () HttpURLConnection, the socket may be closed even if it is idle with the KeepAlive setting. (HttpURLConnection is the default keepalive)
If you close () InputStream / OutputStream instead of disconnecting () HttpURLConnection, network resources will be released, but the socket will not be closed and will be reused. It seems that. If you are making a little thing by yourself like this time, you do not need to be aware of it, but if you have a system that repeatedly connects / disconnects because many people connect, you should be aware of it. If you do not code it, it may lead to the worst performance problem. I learned one thing.
・ HttpURLConnection # disconnect and Keep-Alive -HttpURLConnection (API specification) -HttpURLConnection is the default keepalive ・ Do we need to call HttpURLConnection.disconnect ()?
In the "Introduction" of this article, "I thought it was the same for HttpURLConnection, but I can't use try-with-resources because I haven't implemented the AutoCloseable interface." You pointed out that part.
AutoCloseable is a Functional interface, so it can also be implemented in a lambda expression.
HttpURLConnection urlconn = (HttpURLConnection) url.openConnection(); try (AutoCloseable c = () -> urlconn.disconnect()) { doSomething(); }
Sure, AutoCloseable only has a close () method, so you can use a lambda expression. .. .. So you can also write:
Sample.java
URL url = new URL([URL]);
HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();
urlconn.setRequestMethod("GET");
urlconn.setInstanceFollowRedirects(false);
PurchaseItemInfo itemInfo = null;
try (AutoCloseable c = () -> urlconn.disconnect();
InputStreamReader inReader = new InputStreamReader(urlconn.getInputStream());
BufferedReader reader = new BufferedReader(inReader);) {
urlconn.connect();
doSomething();
}
Thank you for pointing out. I learned a lot.
that's all.
Recommended Posts