【问题标题】:How to get the count of each http status code efficiently?如何有效地获取每个http状态码的计数?
【发布时间】:2014-09-10 21:50:20
【问题描述】:

我正在使用 RestTemplate 执行 URL,然后打印出它的 http 状态代码。

ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
System.out.println(response.getStatusCode());

现在我需要做的是,我需要获取每个状态码的计数并将其作为键和值存储在 map 中。意思是,每个状态代码来了多少次。如果 http 200 状态码大约出现 100 次,那么我想看看它的计数。

我可以通过为每个状态代码设置多个临时变量并相应地继续增加计数来做到这一点。但除此之外,还有其他简单的方法吗?

【问题讨论】:

    标签: java algorithm http-status-codes resttemplate


    【解决方案1】:

    使用Map 可能吗? 状态为键,值为计数器。

    Map<String,Integer> counters = new HashMap<>();
    ...
    synchronized (counters) {
    
      String code = response.getStatusCode();
      Integer counter = counters.get(code);
    
      if (counter == null) {
        counters.put(code, 1);
      } else {
        counters.put(code, counter + 1)
      }
    }
    

    【讨论】:

    • 这就是我在我的问题中提到的。我可以有几个临时变量,具体取决于 Http 状态代码的数量,然后相应地继续增加计数。但是有没有其他简单的方法。这就是我的问题。
    • 使用Map,您将只有一个“temp”变量。我不认为会比这更简单。我会尝试用一个小例子来更新我的答案。
    【解决方案2】:
    Map<Integer,Integer> statusMap = new HashMap<Integer,Integer>();
    
    public void store(int code)
    {
        if (statusMap.containsKey(code))
        {
            int value = statusMap.get(code);
            statusMap.put(code,value+1);
        }
        else
        {
            statusMap.put(code,1);      
        }
    }
    
    public void list()
    {
        Iterator<Integer> iter = statusMap.keySet().iterator();
        while(iter.hasNext())
        {
            int code = iter.next();
            System.out.println(code + " : " + statusMap.get(code));
        }
    }
    

    【讨论】:

      【解决方案3】:

      使用HashMap,然后:

      • 如果您的 httpcode 尚未在地图中,请将其插入 count = 1。
      • 如果你的 httpcode 已经存在于地图中,则增加它的计数器

        HashMap<Integer, Integer> mapCount = new HashMap<Integer, Integer>();
        
        // ...
        
        void updateMap(Integer httpCode) {
            if (!mapCount.containsKey(httpCode)) {
                mapCount.put(httpCode, 1);
            } else {
                // update counter
                int counter = mapCount.get(str).intValue() + 1;
                // overwrite existing with update counter
                mapCount.put(httpCode, counter + 1);
            }
        }
        
        // ...
        

      【讨论】:

        【解决方案4】:

        由于您实际上是在寻求另一种方式,因此您可以使用 int 数组,其中 int 数组的索引表示接收到的 HTTP 代码。

        类似:

        // initialization
        int[] responses = new int[600];
        
        // for each received response
        responses[response.getStatusCode().value()]++
        
        // retrieving the number of HTTP 200 received
        System.out.println("Number of HTTP 200 received : " + responses[HttpStatus.OK.value()] /* or simply responses[200] */);
        

        不确定这会带来什么:即使它更快一点,该数组中肯定有很多 int 最终会被浪费掉。其他答案详细介绍了Map 方法,恕我直言,因为更明确地说明了您要执行的操作(即计算特定HTTP状态代码的出现次数)。编写代码时,清晰是关键:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-07-22
          • 2011-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-13
          • 1970-01-01
          相关资源
          最近更新 更多