本文整理匯總了Java中org.apache.http.params.HttpConnectionParams.setSoKeepalive方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpConnectionParams.setSoKeepalive方法的具體用法?Java HttpConnectionParams.setSoKeepalive怎麽用?Java HttpConnectionParams.setSoKeepalive使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.params.HttpConnectionParams
的用法示例。
在下文中一共展示了HttpConnectionParams.setSoKeepalive方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: LongPollingEventsManager
import org.apache.http.params.HttpConnectionParams; //導入方法依賴的package包/類
public LongPollingEventsManager(CachedBoxClient boxClient,
Map<String, Object> httpParams, ExecutorService executorService) {
this.cachedBoxClient = boxClient;
this.executorService = executorService;
this.httpParams = new BasicHttpParams();
HttpConnectionParams.setSoKeepalive(this.httpParams, true);
if (httpParams != null) {
for (Map.Entry<String, Object> entry : httpParams.entrySet()) {
this.httpParams.setParameter(entry.getKey(), entry.getValue());
}
}
}
示例2: doRefer
import org.apache.http.params.HttpConnectionParams; //導入方法依賴的package包/類
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
if (connectionMonitor == null) {
connectionMonitor = new ConnectionMonitor();
}
// TODO more configs to add
PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
// 20 is the default maxTotal of current PoolingClientConnectionManager
connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20));
connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20));
connectionMonitor.addConnectionManager(connectionManager);
// BasicHttpContext localContext = new BasicHttpContext();
DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000;
}
}
// TODO constant
return 30 * 1000;
}
});
HttpParams params = httpClient.getParams();
// TODO currently no xml config for Constants.CONNECT_TIMEOUT_KEY so we directly reuse Constants.TIMEOUT_KEY for now
HttpConnectionParams.setConnectionTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
HttpConnectionParams.setSoTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
HttpConnectionParams.setTcpNoDelay(params, true);
HttpConnectionParams.setSoKeepalive(params, true);
ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient/*, localContext*/);
ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
clients.add(client);
client.register(RpcContextFilter.class);
for (String clazz : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, ""))) {
if (!StringUtils.isEmpty(clazz)) {
try {
client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim()));
} catch (ClassNotFoundException e) {
throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e);
}
}
}
// TODO protocol
ResteasyWebTarget target = client.target("http://" + url.getHost() + ":" + url.getPort() + "/" + getContextPath(url));
return target.proxy(serviceType);
}
示例3: doRefer
import org.apache.http.params.HttpConnectionParams; //導入方法依賴的package包/類
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
if (connectionMonitor == null) {
connectionMonitor = new ConnectionMonitor();
}
// TODO more configs to add
PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager();
// 20 is the default maxTotal of current PoolingClientConnectionManager
connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20));
connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20));
connectionMonitor.addConnectionManager(connectionManager);
// BasicHttpContext localContext = new BasicHttpContext();
DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
return Long.parseLong(value) * 1000;
}
}
// TODO constant
return 30 * 1000;
}
});
HttpParams params = httpClient.getParams();
// TODO currently no xml config for Constants.CONNECT_TIMEOUT_KEY so we directly reuse Constants.TIMEOUT_KEY for now
HttpConnectionParams.setConnectionTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
HttpConnectionParams.setSoTimeout(params, url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
HttpConnectionParams.setTcpNoDelay(params, true);
HttpConnectionParams.setSoKeepalive(params, true);
ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient/* , localContext */);
ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
clients.add(client);
client.register(RpcContextFilter.class);
for (String clazz : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, ""))) {
if (!StringUtils.isEmpty(clazz)) {
try {
client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim()));
} catch (ClassNotFoundException e) {
throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e);
}
}
}
// dubbo 服務多版本
String version = url.getParameter(Constants.VERSION_KEY);
String versionPath = "";
if (StringUtils.isNotEmpty(version)) {
versionPath = version + "/";
}
// TODO protocol
ResteasyWebTarget target = client.target("http://" + url.getHost() + ":" + url.getPort() + "/" + versionPath + getContextPath(url));
return target.proxy(serviceType);
}