package com.timing.servlet;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.*;
/**
* 异步servlet
* @author hardneedl
*/
@WebServlet(asyncSupported = true)
abstract public class AsyncServlet extends HttpServlet{
final protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.service(req, resp);
}
final public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
super.service(req, res);
}
final protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
startAsync(req,resp);
}
final protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
startAsync(req,resp);
}
final protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
startAsync(req,resp);
}
final protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
startAsync(req,resp);
}
final protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
startAsync(req,resp);
}
final protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
startAsync(req,resp);
}
final protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
startAsync(req,resp);
}
/**
* 处理异常。缺省实现只是设置Http状态码为500,并且记录到servlet 容器的日志
* @param e 异常
* @param req 请求
* @param res 响应
*/
protected void exceptionHandle(Exception e, HttpServletRequest req, HttpServletResponse res){
res.setStatus(500);
req.getServletContext().log(e.getLocalizedMessage(),e);
}
/**
* 异步处置时候的上下文环境的监听器。可以依据请求参数的不同而得到不同的监听器实例
* @param req 请求
* @param res 响应
* @return 监听器
*/
protected AsyncListener getAsyncListener(HttpServletRequest req, HttpServletResponse res) {return null;}
/**
* 异步处理的超时。可以依据请求参数的不同而得到不同的超时数值
* @param req 请求
* @param res 响应
* @return 异步处理的超时 。缺省值是5分钟
*/
protected long getAsyncTimeout(HttpServletRequest req,HttpServletResponse res){
return 5 * 60 * 1000;
}
/**
* 启动异步处理
* @param req 请求
* @param res 响应
* @throws ServletException 异常
* @throws IOException
*/
private void startAsync(HttpServletRequest req, HttpServletResponse res){
AsyncContext asyncContext = req.startAsync(req,res);
asyncContext.setTimeout(getAsyncTimeout(req,res));
AsyncListener asyncListener = getAsyncListener(req, res);
if (asyncListener != null)
asyncContext.addListener(asyncListener,req,res);
asyncContext.start(() -> {
try {
switch (req.getMethod()) {
case "GET":
responseForGet(doAsyncGet(req, res), req, res);
break;
case "POST":
responseForPost(doAsyncPost(req, res), req, res);
break;
case "OPTIONS":
responseForOptions(doAsyncOptions(req, res), req, res);
break;
case "PUT":
responseForPut(doAsyncPut(req, res), req, res);
break;
case "DELETE":
responseForDelete(doAsyncDelete(req, res), req, res);
break;
case "TRACE":
responseForTrace(doAsyncTrace(req, res), req, res);
break;
case "HEAD":
responseForHead(doAsyncHead(req, res), req, res);
break;
}
} catch (Exception e) {
exceptionHandle(e, req, res);
}
asyncContext.complete();
});
}
protected Object doAsyncGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{return null;}
protected void responseForGet(Object value,HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException{}
protected Object doAsyncPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{return null;}
protected void responseForPost(Object value,HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException{}
protected Object doAsyncOptions(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{return null;}
protected void responseForOptions(Object value, HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{}
protected Object doAsyncPut(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{return null;}
protected void responseForPut(Object value,HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException{}
protected Object doAsyncDelete(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{return null;}
protected void responseForDelete(Object value,HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException{}
protected Object doAsyncHead(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{return null;}
protected void responseForHead(Object value,HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException{}
protected Object doAsyncTrace(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{return null;}
protected void responseForTrace(Object value,HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException{}
}