Skip to content

Improve HTTP support for JDK client and HttpInvoker server - #1017

Merged
bclozel merged 2 commits into
spring-projects:masterfrom
bclozel:SPR-14040
Mar 29, 2016
Merged

bclozel merged 2 commits into
spring-projects:masterfrom
bclozel:SPR-14040

Conversation

@bclozel

@bclozel bclozel commented Mar 25, 2016

Copy link
Copy Markdown
Member

Two commits that improve HTTP support when using the native JDK HTTP architecture.

The first commit prevents the RestTemplate from closing the underlying HTTP connection when closing an HTTP response and completely drains the content of the response. Those are the two conditions to really leverage HTTP persistent connections support in HTTP 1.1.

The second commit works around a JDK API that seems to artificially prevent gathered writes when using the HttpInvoker server implementation.

Issue: SPR-14040

@bclozel bclozel self-assigned this Mar 25, 2016
bclozel added 2 commits March 29, 2016 17:31
Prior to this commit, HTTP clients relying on the JDK HTTP client would
not properly reuse existing TCP connections (i.e. HTTP 1.1 persisten
connection). The SimpleClientHttpResponse would close the actual connection once the
response is handled.

As explained in the JDK documentation
(http://docs.oracle.com/javase/8/docs/technotes/guides/net/http-keepalive.html)
HTTP clients should do the following to allow resource reuse:

* consume the whole HTTP response content
* close the response inputstream once done

This commit makes sure that the response content is
totally drained and then the stream closed (and not the connection).

Issue: SPR-14040
Prior to this commit, the `HttpInvokerServiceExporter` would close
its `ObjectOutputStream`, which itself issues duplicate flushes on the
underlying `OutputStream`. Duplicate flushes can lead to multiple,
separate TCP packets where those should be gathered writes.

This commit wraps the underying stream with a decorator that guards
against flush calls that are duplicated with the one done in `close`.

Issue: SPR-14040
@bclozel bclozel changed the title Support of HTTP persistent connections for JDK client Improve HTTP support for JDK client and HttpInvoker server Mar 29, 2016
@bclozel
bclozel merged commit eec22f5 into spring-projects:master Mar 29, 2016
@bclozel
bclozel deleted the SPR-14040 branch March 29, 2016 16:14
@hadrienk

hadrienk commented Nov 4, 2016 •

Copy link
Copy Markdown

This render InputStreamResource created by ResourceHttpMessageConverter unusable. A solution could be to skip InputStreamResource altogether, I don't know if it would have side effects.

    @Override
    public void close() {           public void close() {
 -      this.connection.disconnect();        +      if (this.responseStream != null) {
 +          try {
 +              StreamUtils.drain(this.responseStream);
 +              this.responseStream.close();
 +          }
 +          catch (IOException e) { }
 +      }
    }           }

@bclozel

bclozel commented Nov 6, 2016

Copy link
Copy Markdown
Member Author

@hadrienk Could you create a new issue on jira.spring.io and reference SPR-14040?
Can you also describe how that change makes that InputStreamResource unusable with a code sample?

Thanks!

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