Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized resource management of HTTP response messages #1

Closed
wants to merge 1 commit into from
Closed

Optimized resource management of HTTP response messages #1

wants to merge 1 commit into from

Conversation

ok2c
Copy link

@ok2c ok2c commented Aug 19, 2013

Eugen, I came across your tutorial on Java Lobby. I thought I should let you know that you could make resource management of HTTP response messages less verbose and likely more optimal.

@eugenp
Copy link
Owner

eugenp commented Aug 19, 2013

I missed the fact that EntityUtils.consume also closes the content stream, so that doesn't need to be done by hand - thanks. The problem with getting the EntityUtils.consume out of finally is that it will not always be executed this way. For example, when the response is a 200 OK, the method returns before consuming the http entity.
My thinking is - get rid of the explicit closing of the stream, but keep the consume in finally, to make sure it's executed regardless.
Hope this makes sense.
Thanks for the pull request.
Eugen.

@ok2c
Copy link
Author

ok2c commented Aug 19, 2013

You really need only either EntityUtils#consume or HttpUriRequest#releaseConnection in the finally clause. Both methods will eventually invoke the same method of the connection manager that manages the response stream. The difference between the two is that EntityUtils#consume will attempt to read until the end of message stream in order to ensure the underlying connection could be kept alive and re-used, while HttpUriRequest#releaseConnection will just shut down the connection without further ado. Depending on how aggressive one might want to be about connection re-use it is generally sufficient to attempt to re-use the connection in the source of normal request execution while using HttpUriRequest#releaseConnection as the last safeguard for abnormal situations.

@eugenp
Copy link
Owner

eugenp commented Aug 19, 2013

That's was my understanding of consume as well - that it fully reads the stream and then it closes it. Also, it looks like a side goal of releaseConnection is to allow the request itself to be reused (which I am not doing - the request object is throw away). My only concern is - I wasn't 100% sure that release connection means the stream will be closed as well.
But, if it does and only one call is enough, wouldn't it make sense to get rid of the other (instead of having it execute only for half of the cases)?

@ok2c
Copy link
Author

ok2c commented Aug 19, 2013

As I said either is sufficient. The choice of one depends on how conservative you want to be with connection re-use. I personally would not bother trying to salvage connections in case of an exceptional situation.

@eugenp eugenp closed this Oct 11, 2015
davidmorley pushed a commit that referenced this pull request Mar 3, 2016
Performance of GSON and Jackson Code Changes
davidmorley pushed a commit that referenced this pull request Mar 17, 2016
atheedom added a commit that referenced this pull request May 31, 2016
Merging latest changes
slavisa-baeldung pushed a commit that referenced this pull request Jul 6, 2016
atheedom pushed a commit that referenced this pull request Aug 2, 2016
atheedom added a commit that referenced this pull request Aug 15, 2016
"Running Logic on Startup in Spring" Examples #1
jkerak pushed a commit that referenced this pull request Oct 15, 2016
slavisa-baeldung pushed a commit that referenced this pull request Feb 20, 2017
Pulling from master fork
slavisa-baeldung pushed a commit that referenced this pull request Jul 5, 2017
evandor pushed a commit that referenced this pull request Oct 29, 2017
lor6 pushed a commit that referenced this pull request Nov 29, 2017
Updating from original
CalamarBicefalo pushed a commit that referenced this pull request Dec 3, 2017
tarangbhalodia pushed a commit to tarangbhalodia/tutorials that referenced this pull request Jan 2, 2018
evandor pushed a commit that referenced this pull request Jan 29, 2018
CalamarBicefalo pushed a commit that referenced this pull request Feb 7, 2018
thombergs pushed a commit that referenced this pull request Feb 24, 2018
CalamarBicefalo pushed a commit that referenced this pull request Mar 26, 2018
merge from master changes into fork
lor6 pushed a commit that referenced this pull request Jul 4, 2018
merging eugenp/tutorials/master
lor6 pushed a commit that referenced this pull request Jul 19, 2018
eric-martin pushed a commit that referenced this pull request Jul 21, 2018
Merging base into fork
lor6 pushed a commit that referenced this pull request Jul 29, 2018
lor6 pushed a commit that referenced this pull request Aug 4, 2018
lor6 pushed a commit that referenced this pull request Aug 26, 2018
BAEL-8398 Upgrade vulnerable dependencies in tutorials project
studiofu added a commit to studiofu/tutorials that referenced this pull request Aug 15, 2019
eric-martin pushed a commit that referenced this pull request Oct 1, 2019
sjmillington pushed a commit that referenced this pull request Nov 17, 2019
JonCook pushed a commit that referenced this pull request Feb 17, 2020
jzheaux pushed a commit that referenced this pull request Feb 28, 2020
rpvilao pushed a commit that referenced this pull request Mar 20, 2020
jzheaux pushed a commit that referenced this pull request Apr 27, 2020
JonCook pushed a commit that referenced this pull request May 10, 2020
rpvilao pushed a commit that referenced this pull request Jun 28, 2020
davidmartinezbarua pushed a commit that referenced this pull request Aug 8, 2020
JonCook pushed a commit that referenced this pull request Aug 8, 2020
bfontana pushed a commit that referenced this pull request Sep 11, 2020
eric-martin pushed a commit that referenced this pull request Nov 29, 2020
eric-martin pushed a commit that referenced this pull request Mar 9, 2021
glmartin pushed a commit that referenced this pull request Apr 4, 2021
glmartin pushed a commit that referenced this pull request Apr 4, 2021
glmartin pushed a commit that referenced this pull request May 11, 2021
JonCook pushed a commit that referenced this pull request May 26, 2021
kwoyke pushed a commit that referenced this pull request Aug 21, 2021
lor6 pushed a commit that referenced this pull request Nov 8, 2021
eric-martin pushed a commit that referenced this pull request Apr 15, 2022
* Spring Boot Properties Migrator Demo

* updated demo properties (#1)

Co-authored-by: s9m33r <no-reply>

* Article/bael 5446 (#2)

* updated demo properties

* formatting

Co-authored-by: s9m33r <no-reply>

Co-authored-by: s9m33r <no-reply>
lor6 pushed a commit that referenced this pull request Jun 30, 2022
davidmartinezbarua pushed a commit that referenced this pull request Mar 6, 2023
eugenp pushed a commit that referenced this pull request Mar 21, 2023
anastasiosioannidis pushed a commit to anastasiosioannidis/tutorials that referenced this pull request Oct 2, 2023
lor6 pushed a commit that referenced this pull request Oct 30, 2023
vinipx pushed a commit that referenced this pull request Dec 27, 2023
Tutorials merge latest version
ulisseslima pushed a commit that referenced this pull request Apr 16, 2024
theangrydev pushed a commit that referenced this pull request Jul 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants