1
回答
JFinalFilter扩展问题
终于搞明白,存储TCO原来是这样算的>>>   

@JFinal 你好,想跟你请教个问题:项目使用JFinal2.2,要使用Google的Guava的RateLimiter加一个访问限流器,但JFinalFilter没办法扩展,请问有什么建议。

RateLimiter的一个使用方式参考

public class RateLimiterFilter implements Filter {

    private static Logger logger = Logger.getLogger(RateLimiterFilter.class);

    private RateLimiter limiter = null;

    public void init(FilterConfig config) throws ServletException {
        limiter = RateLimiter.create(100); //100 request per second
    }

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        if(limiter.tryAcquire()) {
             if(logger.isTraceEnabled()){
                 logger.trace("get access: ");
             }
             chain.doFilter(request, response)
        } else {
             logger.info("system limitation reached!");
             req.getRequestDispatcher("/WEB-INF/jsp/error/429.jsp").forward(req,res);
        }
    }
}



举报
DeanHere
发帖于11个月前 1回/110阅
顶部