当前位置:
首页 >
前端技术
> javascript
>内容正文
javascript
httpwebrequest超时时间timeout设置无效_【SpringBoot WEB 系列】RestTemplate 之超时设置...
生活随笔
收集整理的这篇文章主要介绍了
httpwebrequest超时时间timeout设置无效_【SpringBoot WEB 系列】RestTemplate 之超时设置...
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
【SpringBoot WEB 系列】RestTemplate 之超时设置
一般来讲我们访问外部资源时,需要做一个保护,比如最常见的添加一个超时设置,避免一直被阻塞,RestTemplate 可以通过SimpleClientHttpRequestFactory来处理超时设置
I. RestTemplate 超时设置
博文测试项目完全基于【WEB 系列】RestTemplate 基础用法小结的项目环境,建议配合查看
基本环境:IDEA + maven + SpringBoot 2.2.1.RELEASE
1. 超时端点
添加一个超时模拟的端点如下
private String getHeaders(HttpServletRequest request) {Enumeration headerNames = request.getHeaderNames();
String name;
JSONObject headers = new JSONObject();while (headerNames.hasMoreElements()) {
name = headerNames.nextElement();
headers.put(name, request.getHeader(name));
}return headers.toJSONString();
}private String getParams(HttpServletRequest request) {return JSONObject.toJSONString(request.getParameterMap());
}private String getCookies(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();if (cookies == null || cookies.length == 0) {return "";
}
JSONObject ck = new JSONObject();for (Cookie cookie : cookies) {
ck.put(cookie.getName(), cookie.getValue());
}return ck.toJSONString();
}private String buildResult(HttpServletRequest request) {return buildResult(request, null);
}private String buildResult(HttpServletRequest request, Object obj) {
String params = getParams(request);
String headers = getHeaders(request);
String cookies = getCookies(request);if (obj != null) {
params += " | " + obj;
}return "params: " + params + "\nheaders: " + headers + "\ncookies: " + cookies;
}@GetMapping(path = "timeout")public String timeOut(HttpServletRequest request) throws InterruptedException {
Thread.sleep(60_000L);return buildResult(request);
}
2. 超时设置
主要是通过设置SimpleClientHttpRequestFactory来设置超时
/*** 设置超时时间
*/
public void timeOut() {
RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(1000);
requestFactory.setReadTimeout(1000);
restTemplate.setRequestFactory(requestFactory);
long start = System.currentTimeMillis();
try {
log.info("timeOut start: {}", start);
HttpEntity response =
restTemplate.getForEntity("http://127.0.0.1:8080/timeout?name=一灰灰&age=20", String.class);
log.info("timeOut cost:{} response: {}", System.currentTimeMillis() - start, response);
} catch (Exception e) {
log.info("timeOut cost:{} exception: {}", System.currentTimeMillis() - start, e.getMessage());
}
}
输出如下:
(timeOut start: 1593420406204(timeOut cost:1014 exception: I/O error on GET request for "http://127.0.0.1:8080/timeout": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out
II. 其他
1. 源码&系列博文
博文
- 【WEB 系列】RestTemplate 之中文乱码问题 fix
- 【WEB 系列】RestTemplate 之自定义请求头
- 【WEB 系列】RestTemplate 基础用法小结
源码
- 工程:https://github.com/liuyueyi/spring-boot-demo
- 源码: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/221-web-resttemplate
1. 一灰灰 Blog
尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现 bug 或者有更好的建议,欢迎批评指正,不吝感激
下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛
- 一灰灰 Blog 个人博客 https://blog.hhui.top
- 一灰灰 Blog-Spring 专题博客 http://spring.hhui.top
总结
以上是生活随笔为你收集整理的httpwebrequest超时时间timeout设置无效_【SpringBoot WEB 系列】RestTemplate 之超时设置...的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: bat贪吃蛇游戏代码_C语言写个贪吃蛇游
- 下一篇: springboot整合oracle_S