本文整理汇总了Java中org.springframework.http.client.HttpComponentsClientHttpRequestFactory.setConnectionRequestTimeout方法的典型用法代码示例。如果您正苦于以下问题:Java HttpComponentsClientHttpRequestFactory.setConnectionRequestTimeout方法的具体用法?Java HttpComponentsClientHttpRequestFactory.setConnectionRequestTimeout怎么用?Java HttpComponentsClientHttpRequestFactory.setConnectionRequestTimeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.http.client.HttpComponentsClientHttpRequestFactory
的用法示例。
在下文中一共展示了HttpComponentsClientHttpRequestFactory.setConnectionRequestTimeout方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: 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;
}
示例3: 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;
}
示例4: 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;
}
示例5: 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);
}
示例6: getHttpComponentsClientHttpRequestFactory
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
@Bean(name = "httpRequestFactory")
public HttpComponentsClientHttpRequestFactory getHttpComponentsClientHttpRequestFactory(
@Value("${ping.http.connectionRequestTimeout:60000}") final int connectionRequestTimeout,
@Value("${ping.http.readTimeout:600000}") final int readTimeout) {
final HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setConnectionRequestTimeout(connectionRequestTimeout);
httpRequestFactory.setReadTimeout(readTimeout);
return httpRequestFactory;
}
示例7: 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;
}
示例8: pdfServerClient
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
@Bean
public RestTemplate pdfServerClient() {
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setConnectionRequestTimeout(CONNECT_TIMEOUT);
httpRequestFactory.setReadTimeout(READ_TIMEOUT);
httpRequestFactory.setConnectTimeout(CONNECT_TIMEOUT);
return new RestTemplate(httpRequestFactory);
}
示例9: RestTemplateWorker
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
public RestTemplateWorker(final String url, final MetricRegistry registry, final String name,
final int connectTimeout, final int readTimeout) {
final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
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;
}
示例10: HttpClientTemplate
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; //导入方法依赖的package包/类
private HttpClientTemplate() {
// 长连接保持30秒
PoolingHttpClientConnectionManager pollingConnectionManager = new PoolingHttpClientConnectionManager(timeToLive, TimeUnit.SECONDS);
// 总连接数
pollingConnectionManager.setMaxTotal(maxTotal);
// 同路由的并发数
pollingConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setConnectionManager(pollingConnectionManager);
// 重试次数,默认是3次,没有开启
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, requestSentRetryEnabled));
// 保持长连接配置,需要在头添加Keep-Alive
httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
List<Header> headers = new ArrayList<>();
// 参考:http://www.cnblogs.com/sink_cup/archive/2011/03/15/http_user_agent.html
// 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"));
// 不需要设置,可能会乱码,请参考:http://www.crifan.com/set_accept_encoding_header_to_gzip_deflate_return_messy_code/
// 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(connectTimeout);
// 数据读取超时时间,即SocketTimeout
clientHttpRequestFactory.setReadTimeout(readTimeout);
// 连接不够用的等待时间,不宜过长,必须设置,比如连接不够用时,时间过长将是灾难性的
clientHttpRequestFactory.setConnectionRequestTimeout(connectionRequestTimeout);
// 缓冲请求数据,默认值是true。通过POST或者PUT大量发送数据时,建议将此属性更改为false,以免耗尽内存。
if (!bufferRequestBody) {
clientHttpRequestFactory.setBufferRequestBody(false);
}
// 添加内容转换器
// List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new FastJsonHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter(Charset.forName(charset)));
messageConverters.add(new ByteArrayHttpMessageConverter());
super.setMessageConverters(messageConverters);
super.setRequestFactory(clientHttpRequestFactory);
super.setErrorHandler(new DefaultResponseErrorHandler());
}