Reputation: 41
I run the following code on my machine and sample.jsp runs on my tomcat server. sample.jsp has code running in an infinite loop.
public static void main(String[] args) throws Exception {
URL obj = new URL("http://localhost:8080/bank/sample.jsp");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) { // success
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try(BufferedInputStream stream = new BufferedInputStream(con.getInputStream()) ) {
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ( (bytesRead = stream.read(buffer)) > 0 && byteArrayOutputStream.size() <= 1024 * 100) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
}
}
if(byteArrayOutputStream.size() > 1024 * 100){
stream.close();
con.disconnect();
throw new Exception("content too big. hence terminating...");
}
}
}
Following is the code in my jsp :
<%@page import = "java.io.File" %>
<%@page import = "java.io.FileWriter" %>
<%
for(long i=0;i < 10;)
{
out.println("HelloHelloHelloHelloHelloHello");
}
%>
I close the connection as well as the stream that I opened when the output stream exceeds a particular size. This doesn't stop the process started on my tomcat server. But I want to stop the server side program when connection is closed from client side. I donot get IOException either and the thread seems to be running indefinitely. Can anyone help me on how to stop the thread.
Upvotes: 2
Views: 489
Reputation: 20862
The thread on the server will continue to run until it completes, unless it tries to write output to the client and the client has terminated the connection. In that case, an IOException
will be thrown and the request-processing thread will likely stop what your application was doing and be returned to the container.
If you have an infinite loop, then the code on the server will run indefinitely.
There is no good way to terminate a stuck thread in an application server; this is not specific to Tomcat.
Upvotes: 1