Reputation: 2378
I am new in Servlet, I used the following code to read some inputStream,
class MyServlet implements Servlet{
void service(ServletRequest req, ServletResponse res){
InputStream inA, inB, inC;
//...
inA.read(); // May block
inB.read(); // May block
inC.read(); // May block
// ...
}
}
How to let the servlet container (Tomcat) interrupts/destroys MyServlet after some configurable time. And in this case which method(s) will it call?
thanks in advance,,,
Upvotes: 0
Views: 874
Reputation: 17577
This is perhaps the best approximation without using your own threads: The service method can throw javax.servlet.UnavailableException which will signal container that the servlet is not available temporarily or permanently.
Upvotes: 0
Reputation: 272277
I don't believe you can do that using Tomcat (or another servlet engine).
The simplest way may be to spawn off the time-consuming process in a separate thread, invoke that and time out on that invocation. You can do that easily by using a FutureTask object and calling get() on it, specifying a timeout. You'll get a TimeoutException if the task takes too long, and you can use the servlet to report this (nicely) to the user.
e.g. (very simple)
FutureTask f = new FutureTask(new Runnable{...});
try {
Object o = f.get(TIMEOUT, UNITS)
// report success
}
catch (TimeoutException e) {
// report failure
}
Upvotes: 2
Reputation: 308763
You don't call those methods, the container does.
I'd wonder why you would do this. Do you really want to re-read those files with every request? If you need the contents, I'd prefer to see you read them in the init method and cache them.
Upvotes: 1