fix: restTemplate设置超时时间

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

View File

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

View File

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