Java HttpURLConnection 和池化 [英] Java HttpURLConnection and pooling

查看:38
本文介绍了Java HttpURLConnection 和池化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了有关 Java HttpURLConnection 的各种文档后,我仍然对它执行哪种池化以及如何处理连接感到困惑.

例如下面的代码

URL url = new URL(someUrl);HttpURLConnection 连接 = (HttpURLConnection)url.openConnection();OutputStream os = connection.getOutputStream();InputStream is = connection.getInputStream();/** 向 os 写一些东西并刷新 *//** 读取的是 */os.close();is.close();connection.disconnect();

  1. osis 是否都需要刷新和关闭才能重用底层套接字?

  2. connection.disconnect() 是否会关闭底层套接字(从而使其无法重用)?keep-alive 会影响这种行为吗?

  3. 如果我使用不同的 URL 对象,但使用相同的 URL,从它们创建的 connection 是否会共享底层套接字?当 URL 的主机部分相同但路径不同时如何?

  4. 池连接何时会被销毁?

  5. 控制池大小的系统属性是什么?

此外,如果您也可以将 Android 版本与 Java 进行比较,那就太好了.

谢谢

解决方案

  1. osis 是否都需要刷新和关闭才能重用底层套接字?

关闭输入流就足够了.您无法刷新输入流,并且在关闭之前刷新输出流是多余的.

<块引用>

  1. connection.disconnect() 是否会关闭底层套接字(从而使其无法重用)?

它作为提示"关闭底层连接.

<块引用>

keep-alive 会影响这种行为吗?

是的.如果它不存在,则必须关闭连接.

<块引用>

  1. 如果我使用不同的 URL 对象,但使用相同的 URL,从它们创建的 connection 是否会共享底层套接字?

是的.

<块引用>

当 URL 的主机部分相同但路径不同时如何?

是的.

<块引用>

  1. 池连接何时会被销毁?

空闲超时后.

<块引用>

  1. 控制池大小的系统属性是什么?

我不知道有没有,但如果有,它将在网络属性页面中定义,您可以通过 Javadoc 找到该页面.

<块引用>

此外,如果您也可以将 Android 版本与 Java 进行比较,那就太好了.

我相信 Android 根本不做池化,但是当他们切换到 OpenJDK 源代码时,这应该会改变.

After reading all sort of docs on Java HttpURLConnection, I'm still quite confused as what kind of pooling it does and how it hands connections.

For example, the following code

URL url = new URL(someUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
OutputStream os = connection.getOutputStream();
InputStream is = connection.getInputStream();

/** Write something to os and flush */
/** Read from is */

os.close();
is.close();
connection.disconnect();

  1. Do both os and is need to be flushed and closed for the underlying socket to be reusable?

  2. Will connection.disconnect() close the underlying socket (and hence make it unreusable)? Does keep-alive affect this behavior?

  3. If I use different URL objects, but with the same URL, will the connections created from them share the underlying sockets? How about when the host part of the URL is the same, but paths are different?

  4. When will pooled connections be destroyed?

  5. What's the system property that controls the size of the pool?

Additionally, if you could also compare the Android version with Java it would be great.

Thanks

解决方案

  1. Do both os and is need to be flushed and closed for the underlying socket to be reusable?

Closing the input stream is sufficient. You can't flush an input stream, and flushing an output stream before close is redundant.

  1. Will connection.disconnect() close the underlying socket (and hence make it unreusable)?

It 'acts as a hint' to close the underlying connection.

Does keep-alive affect this behavior?

Yes. If it isn't present, the connection must be closed.

  1. If I use different URL objects, but with the same URL, will the connections created from them share the underlying sockets?

Yes.

How about when the host part of the URL is the same, but paths are different?

Yes.

  1. When will pooled connections be destroyed?

After an idle timeout.

  1. What's the system property that controls the size of the pool?

I'm not aware that there is one, but if there is it will be defined in the Networking Properties page which you can find via the Javadoc.

Additionally, if you could also compare the Android version with Java it would be great.

I believe that Android doesn't do pooling at all, but this should change when they switch to the OpenJDK source.

这篇关于Java HttpURLConnection 和池化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆