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,,,
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.
eg (very simple)
FutureTask f = new FutureTask(new Runnable{...});
try {
Object o = f.get(TIMEOUT, UNITS)
// report success
}
catch (TimeoutException e) {
// report failure
}
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.
这可能是不使用您自己的线程的最佳近似值:service方法可以抛出javax.servlet.UnavailableException ,这将向容器发出信号,表明该servlet暂时或永久不可用。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.