返回博主主页

超时异常

org.springframework.web.client.ResourceAccessException

I/O error on POST request for "http://xxxxx/yyyy": Read timed out

restClient 主动设置的超时时间或者服务器设置的超时时间到了,都会返回 ResourceAccessException

 private ResponseEntity<String> call(JSONObject jsonObj) {
        String jsonStr = jsonObj.toJSONString();
        RestTemplate restClient = new RestTemplate(HttpsUtils.getInstance().getUnsafeClientHttpRequestFactory());
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setConnectTimeout(10000);
        factory.setReadTimeout(30000);
        restClient.setRequestFactory(factory);
        restClient.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpEntity<Object> entity = new HttpEntity<>(jsonStr, new HttpHeaders());
        ResponseEntity<String> pyResponse = null;
        pyResponse = restClient.postForEntity(url, entity, String.class);
        return pyResponse;
    }

java.util.concurrent.ExectutionException


final CompletableFuture<ResponseEntity<String>> future = CompletableFuture.supplyAsync(
                () -> call(jsonObj), callExecutor);
        try {
            int to = 10;
            final ResponseEntity<String> entity = future.get(to, TimeUnit.SECONDS);
            resultObj = JSON.parseObject(entity.getBody());
            processCallPythonResult(tagId, resultObj);
        } catch (InterruptedException | ExecutionException e) {
		//ResourceAccessException 会通过 ExecutionException 报出来
		//org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://xxxxx/yyyy": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out
            throw new WimException(e);
        } catch (TimeoutException e) {
		//future get 超时时间到了会通过 TimeoutException 报出来
        }
posted @ 2024-04-24 15:41  懒惰的星期六  阅读(31)  评论(0编辑  收藏  举报

Welcome to here

主页