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.