Abhilash Abhilash - 2 months ago 18
Java Question

find how many requests are on queue in thread safe spring controller

I am using a spring controller which returns a string from a threadsafe method.
so i have made the controller also thread safe.
I want to know how many request are there in queue simultaneously which are calling to the spring controller

dit dit

Here is my suggestion how you can solve your issue.

Imagine you have this @Controller:

public class MyController {

    private IMyService service;

    @RequestMapping(value = "/myPathName", method = RequestMethod.GET)
    public String home(HttpServletRequest request, Model model) {

        // you synchronized call
        service.callSynchronized(request, model); 

        return "someJsp";

    // +edit
    @RequestMapping(value = "/queueStatus", method = RequestMethod.GET)
    public String home(HttpServletRequest request) {

        return "inQueue: " + request.getAttribute("inQueue");

Now you can define an interceptor and count the requests before and after execution:

public class RequestsInWorkInterceptor extends HandlerInterceptorAdapter {

    private static final Logger _logger  = LoggerFactory.getLogger(RequestsInWorkInterceptor.class);
    private final AtomicLong counter     = new AtomicLong();

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String methodURI = request.getRequestURI();
        long current;
            current = counter.incrementAndGet();
            _logger.debug("current {} clients in a queue", current);
        } else {
            current = counter.get(); // just get, no increment

        // +edit: put the count in the request so you can get it in you controller
        request.setAttribute("inQueue", current);
        return super.preHandle(request, response, handler);

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        String methodURI = request.getRequestURI();
        super.postHandle(request, response, handler, modelAndView);