Java HttpURLConnection和池 [英] Java HttpURLConnection and pooling

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

问题描述

在阅读了有关Java HttpURLConnection的所有文档之后,我仍然很困惑,因为它执行的是哪种池化以及如何处理连接.

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.

例如,以下代码

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. osis是否都需要刷新并关闭以使基础套接字可重复使用?

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

connection.disconnect()是否会关闭基础套接字(从而使其无法重用)? keep-alive是否会影响此行为?

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

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

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?

何时会破坏池中的连接?

When will pooled connections be destroyed?

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

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

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

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

谢谢

推荐答案

  1. osis是否都需要刷新并关闭以使基础套接字可重复使用?
  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. connection.disconnect()是否会关闭底层套接字(从而使其无法重用)?
  1. Will connection.disconnect() close the underlying socket (and hence make it unreusable)?

它提示"关闭基础连接.

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

keep-alive是否会影响此行为?

Does keep-alive affect this behavior?

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

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

  1. 如果我使用不同的URL对象,但具有相同的URL,从它们创建的connection是否共享底层套接字?
  1. If I use different URL objects, but with the same URL, will the connections created from them share the underlying sockets?

是的

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

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

是的

  1. 何时会破坏池中的连接?

空闲超时后.

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

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

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.

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

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

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

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天全站免登陆