Java Code Examples for org.springframework.http.client.SimpleClientHttpRequestFactory#setConnectTimeout()

The following examples show how to use org.springframework.http.client.SimpleClientHttpRequestFactory#setConnectTimeout() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XxxTest.java    From x7 with Apache License 2.0 6 votes vote down vote up
public ViewEntity testRestTemplate(){

        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(60000);
        requestFactory.setReadTimeout(60000);

        String url = "http://127.0.0.1:8868/xxx/test/rest";

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("TX_XID", "eg564ssasdd");

        CatTest cat = new CatTest();
        cat.setType("TEST_CAT");
        HttpEntity<CatTest> requestEntity = new HttpEntity<CatTest>(cat,httpHeaders);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        ResponseEntity<String> result = restTemplate.postForEntity(url, requestEntity,String.class);

        return ViewEntity.ok(result);
    }
 
Example 2
Source File: EurekaRestTemplateBuilder.java    From oneplatform with Apache License 2.0 6 votes vote down vote up
public static synchronized RestTemplate build(String name,int readTimeout,ClientHttpRequestInterceptor ...interceptors ){
	
	if(restTemplates.containsKey(name))return restTemplates.get(name);
	
	SimpleClientHttpRequestFactory factory = new EurekaClientHttpRequestFactory();  
       factory.setReadTimeout(readTimeout);//ms  
       factory.setConnectTimeout(5000);//ms 
       
       RestTemplate restTemplate = new RestTemplate(factory);
       List<ClientHttpRequestInterceptor> interceptorList = new ArrayList<>();
       interceptorList.add(new RestTemplateAutoHeaderInterceptor());
       interceptorList.add(new LoggingRequestInterceptor());
       if(interceptors != null && interceptors.length > 0){
       	for (ClientHttpRequestInterceptor interceptor : interceptors) {
       		interceptorList.add(interceptor);
		}
       }
       restTemplate.setInterceptors(interceptorList);
       //
       restTemplate.setErrorHandler(new CustomResponseErrorHandler());
       //
       restTemplates.put(name, restTemplate);
       
	return restTemplate;
}
 
Example 3
Source File: ClientHttpRequestFactoryFactory.java    From spring-credhub with Apache License 2.0 6 votes vote down vote up
static ClientHttpRequestFactory usingJdk(ClientOptions options) {
	if (usingCustomCerts(options)) {
		logger.warn("Trust material will not be configured when using "
				+ "java.net.HttpUrlConnection. Use an alternate HTTP Client "
				+ "(Apache HttpComponents HttpClient, OkHttp3, or Netty) when "
				+ "configuring CA certificates.");
	}

	SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();

	if (options.getConnectionTimeout() != null) {
		factory.setConnectTimeout(options.getConnectionTimeoutMillis());
	}
	if (options.getReadTimeout() != null) {
		factory.setReadTimeout(options.getReadTimeoutMillis());
	}

	return factory;
}
 
Example 4
Source File: RestTemplateConfig.java    From SpringBootLearn with Apache License 2.0 6 votes vote down vote up
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    factory.setConnectTimeout(15000);
    factory.setReadTimeout(5000);
    return factory;
}
 
Example 5
Source File: RestTemplateWithTimeoutConfig.java    From hellokoding-courses with MIT License 5 votes vote down vote up
@Bean
RestTemplate restTemplateTimeoutConfigWithRequestFactory() {
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    requestFactory.setConnectTimeout(TIMEOUT);
    requestFactory.setReadTimeout(TIMEOUT);

    return new RestTemplate(requestFactory);
}
 
Example 6
Source File: ApiConfig.java    From springboot-learning-experience with Apache License 2.0 5 votes vote down vote up
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    factory.setReadTimeout(5000);//单位为ms
    factory.setConnectTimeout(5000);//单位为ms
    return factory;
}
 
Example 7
Source File: RestTemplateConfig.java    From withme3.0 with MIT License 5 votes vote down vote up
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    factory.setReadTimeout(5000);
    factory.setConnectTimeout(5000);
    return factory;
}
 
Example 8
Source File: RestTemplateConfig.java    From order-charge-notify with Apache License 2.0 5 votes vote down vote up
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    /**读超时单位为ms*/
    factory.setReadTimeout(10000);
    /**连接超时单位为ms*/
    factory.setConnectTimeout(10000);
    return factory;
}
 
Example 9
Source File: CtripMQService.java    From apollo with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() {
  restTemplate = new RestTemplate();

  SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
  rf.setReadTimeout(portalConfig.readTimeout());
  rf.setConnectTimeout(portalConfig.connectTimeout());

  MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  converter.setSupportedMediaTypes(
      Arrays.asList(MediaType.APPLICATION_JSON_UTF8, MediaType.APPLICATION_OCTET_STREAM));

  restTemplate.setMessageConverters(Arrays.asList(converter, new FormHttpMessageConverter()));

}
 
Example 10
Source File: RestTemplateConfig.java    From jeecg-boot with Apache License 2.0 5 votes vote down vote up
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    factory.setReadTimeout(5000);//ms
    factory.setConnectTimeout(15000);//ms
    return factory;
}
 
Example 11
Source File: SystemConfig.java    From redis-manager with Apache License 2.0 5 votes vote down vote up
@Bean
public RestTemplate buildRestTemplate() {
    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    factory.setReadTimeout(10000);
    factory.setConnectTimeout(15000);
    return new RestTemplate(factory);
}
 
Example 12
Source File: SimpleHttpRequester.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance of the SimpleHttpRequester class with the specified connection timeout.
 * <p/>
 * @param connectTimeout an integer value specifying the timeout value in milliseconds for establishing the HTTP
 * connection to the HTTP server.
 */
public SimpleHttpRequester(final int connectTimeout) {
  final SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();

  clientHttpRequestFactory.setConnectTimeout(connectTimeout);

  this.restTemplate = new RestTemplate(clientHttpRequestFactory);
}
 
Example 13
Source File: RestTemplateConfig.java    From WeBASE-Transaction with Apache License 2.0 5 votes vote down vote up
/**
 * init httpRequestFactory.
 * 
 * @return
 */
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    factory.setReadTimeout(20000);
    factory.setConnectTimeout(5000);
    return factory;
}
 
Example 14
Source File: HttpUtils.java    From rqueue with Apache License 2.0 5 votes vote down vote up
public static <T> T readUrl(String url, Class<T> clazz) {
  try {
    RestTemplate restTemplate = new RestTemplate();
    SimpleClientHttpRequestFactory rf =
        (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
    rf.setReadTimeout(2 * Constants.ONE_MILLI_INT);
    rf.setConnectTimeout(2 * Constants.ONE_MILLI_INT);
    return restTemplate.getForObject(url, clazz);
  } catch (Exception e) {
    log.error("GET call failed for {}", url, e);
    return null;
  }
}
 
Example 15
Source File: GradleUpdateHandler.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
private static void setTimeout(RestTemplate restTemplate, int connectTimeout, int readTimeout) {
    restTemplate.setRequestFactory(new SimpleClientHttpRequestFactory());
    SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate
            .getRequestFactory();
    rf.setReadTimeout(readTimeout);
    rf.setConnectTimeout(connectTimeout);
}
 
Example 16
Source File: RestTemplateConfig.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    factory.setConnectTimeout(15000);
    factory.setReadTimeout(5000);
    return factory;
}
 
Example 17
Source File: CtripUserService.java    From apollo with Apache License 2.0 5 votes vote down vote up
private ClientHttpRequestFactory clientHttpRequestFactory() {
  SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
  factory.setConnectTimeout(portalConfig.connectTimeout());
  factory.setReadTimeout(portalConfig.readTimeout());

  return factory;
}
 
Example 18
Source File: BeanConfig.java    From WeBASE-Node-Manager with Apache License 2.0 5 votes vote down vote up
/**
 * resttemplate for generic http request.
 */
@Bean(name = "genericRestTemplate")
public RestTemplate getRestTemplate() {
    SimpleClientHttpRequestFactory factory = getHttpFactoryForDeploy();
    factory.setReadTimeout(constantProperties.getHttpTimeOut());// ms
    factory.setConnectTimeout(constantProperties.getHttpTimeOut());// ms
    return new RestTemplate(factory);
}
 
Example 19
Source File: Configurations.java    From txle with Apache License 2.0 4 votes vote down vote up
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
    SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
    factory.setConnectTimeout(15000);
    return factory;
}
 
Example 20
Source File: RestTemplateConfig.java    From RCT with Apache License 2.0 4 votes vote down vote up
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
  SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
  factory.setReadTimeout(10000);
  factory.setConnectTimeout(15000);
  return factory;
}