JDK-8229235 : com.sun.net.httpserver.HttpExchange should implement AutoCloseable
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.net
  • Priority: P5
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 14
  • Submitted: 2019-08-07
  • Updated: 2019-09-04
  • Resolved: 2019-08-31
Related Reports
CSR :  
Description
Summary
-------
HttpExchange to implement AutoCloseable so that it can be used with try-with-resources statements.

Problem
-------
HttpExchange has a close() method but does not implement AutoCloseable for try-with-resources.

Solution
--------
Retrofit HttpExchange to implement AutoCloseable.

Specification
-------------

    --- a/src/jdk.httpserver/share/classes/com/sun/net/httpserver/HttpExchange.java Wed Aug 07 10:29:07 2019 +0200
    +++ b/src/jdk.httpserver/share/classes/com/sun/net/httpserver/HttpExchange.java Wed Aug 07 16:01:49 2019 +0100
    @@ -64,7 +64,7 @@
      * @since 1.6
      */
    
    -public abstract class HttpExchange {
    +public abstract class HttpExchange implements AutoCloseable {
    
         protected HttpExchange () {
         }



Comments
Note that as part of Project Coin in JDK 7, there were efforts to retrofit existing API with Closeable/AutoCloseable to work with try-with-resources (JDK-6911261, JDK-6963723). HttpExchange and its subclass HttpsExchange were identified as candidates. They were included as part of the writeup "Project Coin: Bringing it to a Close(able)", please excuse atrocious blog import of https://blogs.oracle.com/darcy/project-coin%3a-bringing-it-to-a-closeable. For completeness, full list from that blog entry attached above. I have not searched my records more fully to find a discussion of these particular type, but they were not included as part of the JDK 7 work. However, I think it is fine to retrofit HttpExchange and implicitly its subclass HttpsExchange at this time. Moving to Approved.
31-08-2019