Spring MVC控制器陷入困境而没有响应

时间:2016-03-30 04:10:00

标签: java spring spring-mvc

我遇到了一些具体问题: 我的休息控制器工作了几个小时,比我不知道为什么我提出要求。它没有回应。在浏览器中加载进程一直等待响应。如果我使用POSTer发送请求,我可以看到该请求转到控制器,但没有收到响应。我试图调试应用程序,我有一些预定的处理 - 他们正在工作。但控制器没有。请求未到达控制器。它之前卡住了。我怎么知道这发生了什么?谢谢。这是我的控制器类:

@RestController
@RequestMapping("/quotes")
public class QuotesController {

private final QuotesService quotesService;

@Autowired
public QuotesController(QuotesService quotesService) {
    this.quotesService = quotesService;
}

@RequestMapping(value="", method= RequestMethod.GET, produces = "application/json; charset=utf-8")
public ResponseEntity<List<LoadedFileInfo>> getLoadedFilesData(){
    List<LoadedFileInfo> result = quotesService.getLoadedFilesData();
    return new ResponseEntity<List<LoadedFileInfo>>(result, HttpStatus.OK);
}

@RequestMapping(value="/currencyPairs", method= RequestMethod.GET, produces = "application/json; charset=utf-8")
public ResponseEntity<List<CurrencyPair>> getCurrencyPairs(){
    return new ResponseEntity<List<CurrencyPair>>(quotesService.getCurrencyPairs(), HttpStatus.OK);
}

@RequestMapping(value="/oneMinute", method= RequestMethod.GET, produces = "application/json; charset=utf-8")
public ResponseEntity<List<Number[]>> getOneMinuteQuotes(@RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime from, @RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime to, @RequestParam Integer currencyPair){
    return new ResponseEntity<List<Number[]>>(quotesService.getOneMinuteQuotes(from, to, currencyPair), HttpStatus.OK);
}

@RequestMapping(value="/fiveMinutes", method= RequestMethod.GET, produces = "application/json; charset=utf-8")
public ResponseEntity<List<Number[]>> getFiveMinutesQuotes(@RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime from, @RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime to, @RequestParam Integer currencyPair){
    return new ResponseEntity<List<Number[]>>(quotesService.getFiveMinuteQuotes(from, to, currencyPair), HttpStatus.OK);
}

@RequestMapping(value="/fifteenMinutes", method= RequestMethod.GET, produces = "application/json; charset=utf-8")
public ResponseEntity<List<Number[]>> getFifteenMinutesQuotes(@RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime from, @RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime to, @RequestParam Integer currencyPair){
    return new ResponseEntity<List<Number[]>>(quotesService.getFifteenMinuteQuotes(from, to, currencyPair), HttpStatus.OK);
}

@RequestMapping(value="/thirtyMinutes", method= RequestMethod.GET, produces = "application/json; charset=utf-8")
public ResponseEntity<List<Number[]>> getThirtyMinutesQuotes(@RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime from, @RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime to, @RequestParam Integer currencyPair){
    return new ResponseEntity<List<Number[]>>(quotesService.getThirtyMinuteQuotes(from, to, currencyPair), HttpStatus.OK);
}

@RequestMapping(value="/oneHour", method= RequestMethod.GET, produces = "application/json; charset=utf-8")
public ResponseEntity<List<Number[]>> getOneHourQuotes(@RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime from, @RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime to, @RequestParam Integer currencyPair){
    return new ResponseEntity<List<Number[]>>(quotesService.getOneHourQuotes(from, to, currencyPair), HttpStatus.OK);
}

@RequestMapping(value="/fourHours", method= RequestMethod.GET, produces = "application/json; charset=utf-8")
public ResponseEntity<List<Number[]>> getFourHourQuotes(@RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime from, @RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime to, @RequestParam Integer currencyPair){
    return new ResponseEntity<List<Number[]>>(quotesService.getFourHourQuotes(from, to, currencyPair), HttpStatus.OK);
}

@RequestMapping(value="/oneDay", method= RequestMethod.GET, produces = "application/json; charset=utf-8")
public ResponseEntity<List<Number[]>> getOneDayQuotes(@RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime from, @RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime to, @RequestParam Integer currencyPair){
    return new ResponseEntity<List<Number[]>>(quotesService.getOneDayQuotes(from, to, currencyPair), HttpStatus.OK);
}

@RequestMapping(value="/oneWeek", method= RequestMethod.GET, produces = "application/json; charset=utf-8")
public ResponseEntity<List<Number[]>> getOneWeekQuotes(@RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime from, @RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime to, @RequestParam Integer currencyPair){
    return new ResponseEntity<List<Number[]>>(quotesService.getOneWeekQuotes(from, to, currencyPair), HttpStatus.OK);
}

@RequestMapping(value="/oneMonth", method= RequestMethod.GET, produces = "application/json; charset=utf-8")
public ResponseEntity<List<Number[]>> getOneMonthQuotes(@RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime from, @RequestParam @DateTimeFormat(iso= DateTimeFormat.ISO.DATE_TIME) LocalDateTime to, @RequestParam Integer currencyPair){
    return new ResponseEntity<List<Number[]>>(quotesService.getOneMonthQuotes(from, to, currencyPair), HttpStatus.OK);
}

}

1 个答案:

答案 0 :(得分:0)

我不认为你的控制器有问题,至少在提供的代码中没有证据。 这里有很多可能出错的原因,仅举几例:

  • HTTP连接池已满 运行应用程序的Web服务器具有连接池 - 许多可用的HTTP连接。 除非您使用异步内容(我假设您不这样做),否则可能会因为并行运行的众多连接并尝试访问控制器而导致池不堪重负。

    首先检查连接池是否正常,通常连接池会暴露某种管理。例如,在Tomcat中有一个JMX,所以请以jconsole为例进行登录,并确保有许多空闲连接。

  • 偶尔出现网络问题 这个很容易搞清楚。 只需在控制器的最开头添加一些跟踪消息并记录它们。然后检查日志,看看是否在每个请求上看到新消息,如果这样做,代码被正确调用。

  • 缺陷在应用程序内部 在这种情况下,您应该看到控制器方法需要很长时间才能完成流程(同样,在控制器方法的开头和结尾跟踪日志可以提供帮助)。 因此,为了证明这一点,请考虑进行线程转储。有很多方法可以做到这一点,例如:如果你在Linux上运行,在你的java进程上运行 kill -3 ,或者你可以使用jstack,那么找到最适合你的方法。 无论如何,一个线程转储应该显示你的应用程序在你接受它的时间点卡在哪里。 所以,如果你接受它,说两次,仍然看到负责控制器执行的线程卡在同一个地方 - 你很幸运:)

通过这种方式,您可以对应用程序进行概要分析,并了解确切花费的时间,但这应该在&#34;离线&#34;我相信而不是生产机器。

很抱歉,如果这个答案听起来太基础,但是因为你还没有尝试过任何类型的分析,我认为这样的解释可以帮助