fix: restTemplate设置超时时间

This commit is contained in:
chenjianxing 2021-12-23 14:23:45 +08:00 committed by jianxing
parent 24bf606766
commit f8fb374474
2 changed files with 8 additions and 7 deletions

View File

@ -11,16 +11,20 @@ public class WebConfig implements WebMvcConfigurer {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
return getTimeOutTemplate(10 * 1000, 10 * 1000, 30 * 1000);
}
@Bean
public RestTemplate restTemplateWithTimeOut() {
return getTimeOutTemplate(4000, 4000, 10 * 1000);
}
private RestTemplate getTimeOutTemplate(int requestTimeout, int connectTimeout, int readTimeout) {
RestTemplate restTemplate = new RestTemplate();
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setConnectionRequestTimeout(4000);
httpRequestFactory.setConnectTimeout(4000);
httpRequestFactory.setReadTimeout(10 * 1000);
httpRequestFactory.setConnectionRequestTimeout(requestTimeout);
httpRequestFactory.setConnectTimeout(connectTimeout);
httpRequestFactory.setReadTimeout(readTimeout);
restTemplate.setRequestFactory(httpRequestFactory);
return restTemplate;
}

View File

@ -15,7 +15,6 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public abstract class ZentaoClient extends BaseClient {
@ -74,7 +73,6 @@ public abstract class ZentaoClient extends BaseClient {
public AddIssueResponse.Issue addIssue(MultiValueMap<String, Object> paramMap) {
String sessionId = login();
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(paramMap, new HttpHeaders());
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = null;
try {
String bugCreate = requestUrl.getBugCreate();
@ -91,7 +89,6 @@ public abstract class ZentaoClient extends BaseClient {
public void updateIssue(String id, MultiValueMap<String, Object> paramMap) {
String sessionId = login();
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(paramMap, new HttpHeaders());
RestTemplate restTemplate = new RestTemplate();
try {
restTemplate.exchange(requestUrl.getBugUpdate(),
HttpMethod.POST, requestEntity, String.class, id, sessionId);