client.close() would be useful like request.close()
See original GitHub issueThis program hangs, specifically on HTTP/2 connections unless you uncomment the lines. I think the second line is the one specific to HTTP/2.
//client.dispatcher().executorService().shutdown();
//client.connectionPool().evictAll();
Non-daemon threads
"OkHttp graph.facebook.com" #15 prio=5 os_prio=31 tid=0x00007fbf13d3a000 nid=0x6603 runnable [0x0000700001961000]
"OkHttp Dispatcher" prio=5 tid=0x00007fdda41e5000 nid=0x4a03 waiting on condition [0x0000700000f43000]
I suggest updating the guides, but client.close() seems to make a lot of sense.
package okhttp3;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import okhttp3.internal.framed.Http2;
public class TestAsync {
private static final int CALLS = 1;
public static void main(String[] args) throws InterruptedException, IOException {
Logger activeLogger = addFrameLogging();
final CountDownLatch l = new CountDownLatch(CALLS);
OkHttpClient client =
new OkHttpClient.Builder().protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1))
.build();
Request robotsReq = new Request.Builder().url("https://graph.facebook.com/robots.txt").build();
Callback callback = new Callback() {
@Override public void onFailure(Call call, IOException e) {
l.countDown();
e.printStackTrace();
}
@Override public void onResponse(Call call, Response response) throws IOException {
processResponse(response);
l.countDown();
}
};
//client.newCall(robotsReq).execute();
for (int i = 0; i < CALLS; i++) {
client.newCall(robotsReq).enqueue(callback);
}
l.await();
System.out.println("Finished");
//client.dispatcher().executorService().shutdown();
//client.connectionPool().evictAll();
}
private static void processResponse(Response response) {
ResponseBody body = response.body();
try {
String bs = body.string();
System.out.println(bs.substring(Math.max(0, bs.length() - 10)));
} catch (IOException e) {
e.printStackTrace();
} finally {
body.close();
}
}
public static Logger addFrameLogging() {
Logger activeLogger = Logger.getLogger(Http2.class.getName() + "$FrameLogger");
activeLogger.setLevel(Level.FINE);
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(Level.FINE);
handler.setFormatter(new SimpleFormatter() {
@Override
public String format(LogRecord record) {
return String.format("%s%n", record.getMessage());
}
});
activeLogger.addHandler(handler);
return activeLogger;
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
What does "Connection: close" mean when used in the ...
When the client uses the Connection: close header in the request message, this means that it wants the server to close the connection...
Read more >What could happen if I don't close response.Body?
Close() immediately after getting the response object? No, follow the example provided in the documentation and close it immediately after ...
Read more >response.close() - Python requests - GeeksforGeeks
Python requests are generally used to fetch the content from a particular resource ... How to use response.close() using Python requests?
Read more >4.7. The Mysteries of Connection Close - HTTP - O'Reilly
Any HTTP client, server, or proxy can close a TCP transport connection at any time. ... Clients shouldn't pipeline nonidempotent requests (such as...
Read more >HTTP | Node.js v19.3.0 Documentation
When a connection is closed by the client or the server, it is removed from ... globalAgent that is used by http.request() has...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Adding documentation is a simple, flexible solution.
I’m currently stumbling over a strange, but easy to reproduce issue, where connectionPool.evictAll seems to fix it.
Is it still expected behaviour that the user of OkHttpClient has to call this in certain situation although we do not create a shared connection pool like in #3160? (We are using okhttp 3.8.0 but 3.11.0 does not fix it too)
And do you know why adding the same code as shutdownhook does not help?