查找线程安全弹簧控制器中有多少请求在队列中

时间:2016-11-15 08:27:27

标签: java spring-mvc

我使用的是一个弹簧控制器,它从threadsafe方法返回一个字符串。 所以我让控制器也是线程安全的。 我想知道有多少请求同时在队列中调用弹簧控制器

2 个答案:

答案 0 :(得分:1)

以下是我建议如何解决您的问题。

想象一下,你有这个@Controller

@Controller
public class MyController {

    @Autowired
    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
    @ResponseBody
    @RequestMapping(value = "/queueStatus", method = RequestMethod.GET)
    public String home(HttpServletRequest request) {

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

现在,您可以定义interceptor并在执行前后计算请求:

public class RequestsInWorkInterceptor extends HandlerInterceptorAdapter {

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

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String methodURI = request.getRequestURI();
        long current;
        if("myPathName".equals(methodURI){
            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);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        String methodURI = request.getRequestURI();
        if("myPathName".equals(methodURI){
            counter.decrementAndGet();
        }
        super.postHandle(request, response, handler, modelAndView);
    }
}

答案 1 :(得分:0)

您可以使用Jetty的StatisticsHandler和JMX获取有关活动请求数的信息。

如果您将Jetty用作嵌入式容器(推荐方法),则可以使用EmbeddedServletContainerCustomizer进行设置:

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return new EmbeddedServletContainerCustomizer() {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        ((JettyEmbeddedServletContainerFactory) container)
                .addServerCustomizers(new JettyServerCustomizer() {

            @Override
            public void customize(Server server) {
                MBeanContainer mbContainer = new MBeanContainer(
                        ManagementFactory.getPlatformMBeanServer());
                server.addEventListener(mbContainer);
                server.addBean(mbContainer);

                StatisticsHandler statisticsHandler = new   StatisticsHandler();
                statisticsHandler.setHandler(server.getHandler());
                server.setHandler(statisticsHandler);
            }
        });
    }

    };
    }

您需要在org.eclipse.jetty:jetty-jmx上添加依赖项才能访问MBeanContainer。

  1. 您可以尝试使用弹簧套筒执行器。在端点/指标上,您应该有字段" httpsessions.active"

  2. 公共类RequesInterceptor扩展HandlerInterceptorAdapter {  private static Object lock = new Object();  private static int count = 0

    @覆盖 public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler)抛出ServletException {     sinchronize(锁){                 计数++;             }  }

    }