I would suggest writing a simple Filter servlet. Customize it in web.xml to apply to the path you want to limit the number of simultaneous requests. The code will look something like this:
public class LimitFilter implements Filter { private int limit = 5; private int count; private Object lock = new Object(); public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { boolean ok; synchronized (lock) { ok = count++ < limit; } if (ok) {
Or, alternatively, you can simply put this logic in your HttpServlet . It is a little cleaner and reused as a Filter . You might want to make the limit customizable using web.xml , rather than hard coding it.
Ref :.
Check the definition of the HTTP status code 429 .
source share