为什么Spring Boot Web服务响应计数与请求计数不匹配

时间:2018-11-11 01:13:16

标签: web-services spring-boot

我完成了一个简单的spring boot webservice,这是控制器的代码

package com.example.ly.FirstWebService;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@RestController
public class HelloController {
   Logger logger = LoggerFactory.getLogger(HelloController.class);

@RequestMapping("/")
public String index() {
    //logger.trace("This is a TRACE message.");
    //logger.debug("This is a DEBUG message.");
    //logger.info("This is an INFO message.");
    //logger.warn("This is a WARN message.");
    //logger.error("You guessed it, an ERROR message.");

    return "Greetings from Spring Boot!";
}

@RequestMapping(value = { "/ocs_new" })
@ResponseBody
public Map<String, Object> getRoute(@RequestParam("sid") String id,@RequestParam("calling") String calling,@RequestParam("called") String called,@RequestParam("trunk_in") String trunk_in) {
    logger.info("ocs_new command income with sid ." + id);
    Map<String, Object> mRet = new HashMap<String, Object>();
    mRet.put("duration", 120);

    Map<String,Object> map1 = new HashMap<String,Object>();
    map1.put("nap","nap256");
    map1.put("src","60133878300");
    map1.put("dst","800113803088900");

    Map<String,Object> map2 = new HashMap<String,Object>();
    map2.put("nap","nap369");
    map2.put("src","60133878399");
    map2.put("dst","800213803088999");

    List<Map> jsonObjects = new ArrayList<Map>();
    jsonObjects.add(map1);
    jsonObjects.add(map2);
    mRet.put("routetable",jsonObjects);       

    mRet.put("next_update_time", 120);  
    mRet.put("cmd", "ocs_new");        
    mRet.put("sid", id);           
    mRet.put("result", 0);
    mRet.put("msg", null);


    return mRet;
}



@RequestMapping(value = { "/ocs_start" })
@ResponseBody
public Map<String, Object> callstart(@RequestParam("sid") String id,@RequestParam("a_setup_time") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime a_setup_time,@RequestParam("a_connect_time") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime a_connect_time,@RequestParam("b_setup_time") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime b_setup_time,@RequestParam("b_connect_time") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime b_connect_time,@RequestParam("connected_trunk") String connected_trunk) {
    logger.info("ocs_start command income with sid ." + id);
    Map<String, Object> mRet = new HashMap<String, Object>();
    mRet.put("cmd", "ocs_start");  

    mRet.put("result", 0);      
    mRet.put("sid", id);           
    mRet.put("msg", "queued for processing");


    return mRet;
}




@RequestMapping(value = { "/ocs_end" })
@ResponseBody
public Map<String, Object> callend(@RequestParam("sid") String id,@RequestParam("duration_a") int duration_a,@RequestParam("duration_b") int duration_b,@RequestParam("a_hangup_time") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime a_hangup_time,@RequestParam("b_hangup_time") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime b_hangup_time) {
    logger.info("ocs_end command income with sid ." + id);
    Map<String, Object> mRet = new HashMap<String, Object>();
    mRet.put("cmd", "ocs_end");  

    mRet.put("result", 0);      
    mRet.put("sid", id);           
    mRet.put("msg", "queued for processing");


    return mRet;
}

}

对于客户端,我使用一个c#程序,该程序使用多线程将http请求发送到服务器,因为我在收入时记录了每个请求

logger.info("ocs_new command income with sid ." + id);

响应计数应该与请求计数相同,但是我发现有时响应大于request。例如,如果我发送5000个请求,则响应数可能是5003。

我的控制器有问题吗?

当请求发送到服务器时,我还在客户端记录请求计数,我确定客户端发送的请求恰好为5000。

0 个答案:

没有答案