异步servlet的模板

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{}
}

 

转载于:https://my.oschina.net/u/103555/blog/1305251

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值