2

I have a Servlet in Tomcat that reads binary file upload in the doPost method. The server code simplifies to:

protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("Received post request");

    int read;
    byte[] buffer = new byte[10*1024];
    try {
        InputStream inputStream = req.getInputStream();
        while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
            /* do stuff */
        }
        resp.setStatus(200);
    } catch (Exception e) {
        resp.setStatus(500);
    }
}

There are client applications that use this to servlet to upload a file. Very frequently, I'm getting a java.net.SocketTimeoutException on inputStream.read()

I would like to write a test client app to trigger this on the server side exception.

I tried:

public static void triggerTimeOut() {
    HttpURLConnection urlConn = null;
    try {
        URL me = new URL("http://localhost:8080/uploadData");
        urlConn = (HttpURLConnection)me.openConnection();
        urlConn.setDoOutput(true);
        urlConn.setDoInput(true);

        OutputStreamWriter sw = new OutputStreamWriter(urlConn.getOutputStream());

        String[] data = new String[] {"line 1", "line 2"};
        System.out.println(urlConn.getReadTimeout());
        while(true) {
            sw.write(data[0]);
            sw.flush();
            System.out.println(data[0]);
            Thread.sleep(120000);
            // break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            System.out.println(urlConn.getResponseCode());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        };
    }
}

But the timeout is never triggered, and in the server logs, I don't even get the "Received post request", even after i kill the client app. The debug line does get printed if I comment sleep, and uncomment break.

What kind of adjustments (on either tomcat or client) need to be done so I can get the server read to throw SocketTimeoutException? The HTTP Connector is set to connectionTimeout="5000" in server.xml. It's deliberately set shorter than the default 20000.

I'm trying to recreate the scenario for testing purposes.

Lost In Code
  • 494
  • 1
  • 5
  • 18
  • What version of Tomcat are you using? Also found a [comment from Dave](http://stackoverflow.com/a/7145184/3080094): "The connectionTimeout is how long Tomcat will wait for the http request line once a connection is established. It doesn't affect how long the server waits for the request to finish processing." – vanOekel Jul 01 '14 at 20:51
  • Tomcat 7.0.37. I tried putting the sleep before writing to the stream. But still no output in the server logs. – Lost In Code Jul 01 '14 at 22:46
  • That's odd. Looking at the [documentation](http://tomcat.apache.org/tomcat-7.0-doc/config/http.html) I can only suggest to try the `disableUploadTimeout` and the `socket.soTimeout` options (documentation says the latter is the same as `connectionTimeout` but I doubt that). But I think I'm missing something (maybe obvious to others) here. – vanOekel Jul 01 '14 at 23:13

0 Answers0