JDK-8014032 : com.sun.net.httpserver.HttpServer slowdown in Java7
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 7u17
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • Submitted: 2013-04-04
  • Updated: 2014-11-17
  • Resolved: 2013-05-09
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version  " 1.7.0_17 " 
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
When a resource is published with the internal HttpServer, and if this resource is accessed, then in Java7 there is a delay of approx 1000ms. In Java6 the access to the data over a stream used 5ms approx only.

REGRESSION.  Last worked in version 6u31

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the program both with Java6 and with Java7.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
If the server is running with Java6, then a Java6 client prints the following result:

% java EchoClient
<
1362515635677
<
1362515635682
<
1362515635687
<
1362515635691
 
i.e. every approx 5ms a result is printed
ACTUAL -
If the server is running with Java7, then a Java7 client prints the following result:

% java EchoClient
<
1362517297845
<
1362517298844
<
1362517299845
<
1362517300845
 
i.e. every approx 1000ms a result is printed.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.InetSocketAddress;
    import java.util.Date;

    import com.sun.net.httpserver.HttpExchange;
    import com.sun.net.httpserver.HttpHandler;
    import com.sun.net.httpserver.HttpServer;

    public class EchoServer {
        
        public static void main(String[] args) throws IOException {
            HttpServer server = HttpServer.create(new InetSocketAddress(80), 0);
            server.createContext( " /echo " , new EchoHandler());
            server.start();
        }

        static class EchoHandler implements HttpHandler {
            public void handle(HttpExchange httpExchange) throws IOException {
                httpExchange.getResponseHeaders().add( " Content-type " ,  " text/html " );
                String response =  " <b> "  + new Date() +  " </b> for  "     + httpExchange.getRequestURI();
                httpExchange.sendResponseHeaders(200, response.length());
                OutputStream os = httpExchange.getResponseBody();
                os.write(response.getBytes());
                os.close();
            }
        }
    }


    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;

    public class EchoClient {
        
        public static void main(String[] args) throws Exception{
            while(true) {
                URL url = new URL( " http://localhost:80/echo " );

                BufferedReader rd = new BufferedReader(new InputStreamReader(url.openStream()));
                int res = rd.read();
                System.out.println((char)res);
                System.out.println(System.currentTimeMillis());
            }
        }
    }

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
I have found no workaround with HttpServer.