本文整理汇总了Java中org.springframework.http.client.HttpComponentsClientHttpRequestFactory.setConnectTimeout方法的典型用法代码示例。如果您正苦于以下问题:Java HttpComponentsClientHttpRequestFactory.setConnectTimeout方法的具体用法?Java HttpComponentsClientHttpRequestFactory.setConnectTimeout怎么用?Java HttpComponentsClientHttpRequestFactory.setConnectTimeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.http.client.HttpComponentsClientHttpRequestFactory
的用法示例。
在下文中一共展示了HttpComponentsClientHttpRequestFactory.setConnectTimeout方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: requestFactory
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
@Bean
public ClientHttpRequestFactory requestFactory() {
// Disable auto redirect on 3xx HTTP responses
CloseableHttpClient httpClient = HttpClientBuilder.create().disableRedirectHandling().build();
HttpComponentsClientHttpRequestFactory rf = new HttpComponentsClientHttpRequestFactory(httpClient);
// read timeout
if(!StringUtils.isEmpty(readTimeout)) {
rf.setReadTimeout(Integer.valueOf(readTimeout) * 1000);
}
// connect timeout
if(!StringUtils.isEmpty(connectTimeout)) {
rf.setConnectTimeout(Integer.valueOf(connectTimeout) * 1000);
}
return rf;
}
示例2: SmallTimeoutRestTemplateWorker
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
public SmallTimeoutRestTemplateWorker(final String url, final MetricRegistry registry, final String name,
final int timeout) {
final HttpClient httpClient = HttpClientBuilder.create()
.setMaxConnTotal(MAX_CONN_TOTAL)
// .setMaxConnPerRoute(MAX_CONN_PER_ROUTE)
.build();
final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
factory.setReadTimeout(timeout);
factory.setConnectionRequestTimeout(timeout);
factory.setConnectTimeout(timeout);
this.restTemplate = new RestTemplate(factory);
this.url = url;
this.registry = registry;
this.name = name;
this.timeout = timeout;
}
示例3: afterPropertiesSet
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
public void afterPropertiesSet() throws UnsupportedEncodingException {
Collection<Header> defaultHeaders = new ArrayList<Header>();
Header header = new BasicHeader("Authorization",
"Basic " + BaseEncoding.base64().encode("apollo:".getBytes("UTF-8")));
defaultHeaders.add(header);
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("apollo", ""));
CloseableHttpClient httpClient =
HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider)
.setDefaultHeaders(defaultHeaders).build();
restTemplate = new RestTemplate(httpMessageConverters.getConverters());
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory(httpClient);
requestFactory.setConnectTimeout(portalConfig.connectTimeout());
requestFactory.setReadTimeout(portalConfig.readTimeout());
restTemplate.setRequestFactory(requestFactory);
}
示例4: BigPoolRestTemplateWorker
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
public BigPoolRestTemplateWorker(final String url, final MetricRegistry registry, final String name,
final int connectTimeout, final int readTimeout) {
final HttpClient httpClient = HttpClientBuilder.create()
.setMaxConnTotal(MAX_CONN_TOTAL)
// .setMaxConnPerRoute(MAX_CONN_PER_ROUTE)
.build();
final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
factory.setConnectionRequestTimeout(connectTimeout); // Get from manager.
factory.setConnectTimeout(connectTimeout); // Create connection.
factory.setReadTimeout(readTimeout); // Socket.
this.restTemplate = new RestTemplate(factory);
this.url = url;
this.registry = registry;
this.name = name;
this.connectTimeout = connectTimeout;
this.readTimeout = readTimeout;
}
示例5: getFactoryDisabledSslChecks
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
/**
* WARNING!!! disabling is not a good idea.
*
* Support hardcode for ps team
*
* Their server is pretty busted. -
* http://stackoverflow.com/questions/7615645
* /ssl-handshake-alert-unrecognized-name-error-since-upgrade-to-java-1-7-0
* -- -Djsse.enableSNIExtension=false very ugly workaround
*
* @param restTemplate
* @throws Exception
*/
public static HttpComponentsClientHttpRequestFactory getFactoryDisabledSslChecks(
int connectTimeoutMs, int readTimeoutMs) throws Exception {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
// builder.loadTrustMaterial(null, new TrustStrategy() {
//
// @Override
// public boolean isTrusted(X509Certificate[] chain, String authType)
// throws CertificateException {
//
// return true;
// }
// });
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build(), new NoopHostnameVerifier());
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(
sslsf).build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(httpClient);
// factory.getHttpClient().getConnectionManager().getSchemeRegistry().register(scheme);
factory.setConnectTimeout(connectTimeoutMs);
factory.setReadTimeout(readTimeoutMs);
// restTemplate.setRequestFactory(factory);
return factory;
}
示例6: getRestTemplate
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
private RestTemplate getRestTemplate ( long maxConnectionInMs, JsonNode user, JsonNode pass, String desc ) {
logger.debug( "maxConnectionInMs: {} , user: {} , Pass: {} ", maxConnectionInMs, user, pass );
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
// "user" : "$csapUser1", "pass" : "$csapPass1"
if ( user != null && pass != null ) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope( null, -1 ),
new UsernamePasswordCredentials(
user.asText(),
csapApplication.decode( pass.asText(), desc ) ) );
HttpClient httpClient = HttpClients
.custom()
.setDefaultCredentialsProvider( credsProvider )
.build();
factory.setHttpClient( httpClient );
// factory = new HttpComponentsClientHttpRequestFactory(httpClient);
}
factory.setConnectTimeout( (int) maxConnectionInMs );
factory.setReadTimeout( (int) maxConnectionInMs );
RestTemplate restTemplate = new RestTemplate( factory );
return restTemplate;
}
示例7: createRestTemplate
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
protected RestTemplate createRestTemplate() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout(PACT_DOWNLOAD_TIMEOUT_MILLIS);
factory.setConnectTimeout(PACT_DOWNLOAD_TIMEOUT_MILLIS);
return new RestTemplate(factory);
}
示例8: initRestTemplate
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
/**
* springboot初始化restTemplate
*/
@Bean
public RestTemplate initRestTemplate() {
// 长连接保持30秒
PoolingHttpClientConnectionManager pollingConnectionManager = new PoolingHttpClientConnectionManager(30, TimeUnit.SECONDS);
// 总连接数
pollingConnectionManager.setMaxTotal(1000);
// 同路由的并发数
pollingConnectionManager.setDefaultMaxPerRoute(1000);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setConnectionManager(pollingConnectionManager);
// 重试次数,默认是3次,没有开启
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(2, true));
// 保持长连接配置,需要在头添加Keep-Alive
httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
RequestConfig.Builder builder = RequestConfig.custom();
builder.setConnectionRequestTimeout(200);
builder.setConnectTimeout(5000);
builder.setSocketTimeout(5000);
RequestConfig requestConfig = builder.build();
httpClientBuilder.setDefaultRequestConfig(requestConfig);
List<Header> headers = new ArrayList<>();
headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36"));
headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate"));
headers.add(new BasicHeader("Accept-Language", "zh-CN"));
headers.add(new BasicHeader("Connection", "Keep-Alive"));
httpClientBuilder.setDefaultHeaders(headers);
HttpClient httpClient = httpClientBuilder.build();
// httpClient连接配置,底层是配置RequestConfig
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
// 连接超时
clientHttpRequestFactory.setConnectTimeout(5000);
// 数据读取超时时间,即SocketTimeout
clientHttpRequestFactory.setReadTimeout(5000);
// 连接不够用的等待时间,不宜过长,必须设置,比如连接不够用时,时间过长将是灾难性的
clientHttpRequestFactory.setConnectionRequestTimeout(200);
// 缓冲请求数据,默认值是true。通过POST或者PUT大量发送数据时,建议将此属性更改为false,以免耗尽内存。
clientHttpRequestFactory.setBufferRequestBody(false);
// 添加内容转换器
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(initStringHttpMessageConverter());
messageConverters.add(new FormHttpMessageConverter());
messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
messageConverters.add(new MappingJackson2HttpMessageConverter());
RestTemplate restTemplate = new RestTemplate(messageConverters);
restTemplate.setRequestFactory(clientHttpRequestFactory);
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
return restTemplate;
}
示例9: clientHttpRequestFactory
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
@Bean
public ClientHttpRequestFactory clientHttpRequestFactory(HttpClient httpClient) {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
factory.setConnectTimeout(settings.getConnectionTimeout());
factory.setReadTimeout(settings.getReadTimeout());
factory.setConnectionRequestTimeout(settings.getConnectionRequestTimeout());
return factory;
}
示例10: setRequestFactory
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
private void setRequestFactory(final OAuth2RestTemplate restTemplate) {
CloseableHttpClient httpClient = HttpClientBuilder.create().useSystemProperties().build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
requestFactory.setReadTimeout(this.adapterTimeoutMillis);
requestFactory.setConnectTimeout(this.adapterTimeoutMillis);
requestFactory.setConnectionRequestTimeout(this.adapterTimeoutMillis);
restTemplate.setRequestFactory(requestFactory);
}
示例11: getClientHttpRequestFactory
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
/**
* 获取连接工厂
*
* @return dummy
*/
private ClientHttpRequestFactory getClientHttpRequestFactory() {
HttpClient httpClient = getHttpClient();
// httpClient连接配置,底层是配置RequestConfig
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
// 连接超时
clientHttpRequestFactory.setConnectTimeout(wxProperties.getInvoker().getConnectTimeout());
// 数据读取超时时间,即SocketTimeout
clientHttpRequestFactory.setReadTimeout(wxProperties.getInvoker().getReadTimeout());
// 连接不够用的等待时间,不宜过长,必须设置,比如连接不够用时,时间过长将是灾难性的
clientHttpRequestFactory.setConnectionRequestTimeout(wxProperties.getInvoker().getConnectionRequestTimeout());
return clientHttpRequestFactory;
}
示例12: init
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
@PostConstruct
private void init() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout(API_TIMEOUT_IN_MILLIS);
factory.setConnectTimeout(API_TIMEOUT_IN_MILLIS);
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy());
restTemplate = new RestTemplate(Collections.singletonList(new MappingJackson2HttpMessageConverter(mapper)));
restTemplate.setRequestFactory(factory);
}
示例13: afterPropertiesSet
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
public void afterPropertiesSet() throws UnsupportedEncodingException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
restTemplate = new RestTemplate(httpMessageConverters.getConverters());
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory(httpClient);
requestFactory.setConnectTimeout(portalConfig.connectTimeout());
requestFactory.setReadTimeout(portalConfig.readTimeout());
restTemplate.setRequestFactory(requestFactory);
}
示例14: getOAuthRestTemplate
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
@Override
public RestTemplate getOAuthRestTemplate(String key, String secret) {
BaseProtectedResourceDetails oauthCredentials = new BaseProtectedResourceDetails();
oauthCredentials.setConsumerKey(key);
oauthCredentials.setSharedSecret(new SharedConsumerSecretImpl(secret));
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
clientHttpRequestFactory.setReadTimeout(DEFAULT_READ_TIMEOUT);
clientHttpRequestFactory.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);
RestTemplate restTemplate = new OAuthRestTemplate(clientHttpRequestFactory, oauthCredentials);
restTemplate.setErrorHandler(new UserSyncApiExceptionHandler());
return restTemplate;
}
示例15: getOAuthRestTemplate
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
@Override
public RestTemplate getOAuthRestTemplate(String key, String secret) {
BaseProtectedResourceDetails oauthCredentials = new BaseProtectedResourceDetails();
oauthCredentials.setConsumerKey(key);
oauthCredentials.setSharedSecret(new SharedConsumerSecretImpl(secret));
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
clientHttpRequestFactory.setReadTimeout(DEFAULT_READ_TIMEOUT);
clientHttpRequestFactory.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT);
RestTemplate restTemplate = new OAuthRestTemplate(clientHttpRequestFactory, oauthCredentials);
restTemplate.setErrorHandler(new ReportUsageApiExceptionHandler());
return restTemplate;
}