Java Code Examples for org.apache.commons.httpclient.params.HttpConnectionManagerParams#setConnectionTimeout()

The following examples show how to use org.apache.commons.httpclient.params.HttpConnectionManagerParams#setConnectionTimeout() . 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: HttpClientFactory.java    From alfresco-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected HttpClient constructHttpClient()
{
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpClient httpClient = new HttpClient(connectionManager);
    HttpClientParams params = httpClient.getParams();
    params.setBooleanParameter(HttpConnectionParams.TCP_NODELAY, true);
    params.setBooleanParameter(HttpConnectionParams.STALE_CONNECTION_CHECK, true);
    if (socketTimeout != null) 
    {
        params.setSoTimeout(socketTimeout);
    }
    HttpConnectionManagerParams connectionManagerParams = httpClient.getHttpConnectionManager().getParams();
    connectionManagerParams.setMaxTotalConnections(maxTotalConnections);
    connectionManagerParams.setDefaultMaxConnectionsPerHost(maxHostConnections);
    connectionManagerParams.setConnectionTimeout(connectionTimeout);

    return httpClient;
}
 
Example 2
Source File: DefaultDiamondSubscriber.java    From diamond with Apache License 2.0 6 votes vote down vote up
protected void initHttpClient() {
    if (MockServer.isTestMode()) {
        return;
    }
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
        diamondConfigure.getPort());

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.closeIdleConnections(diamondConfigure.getPollingIntervalTime() * 4000);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setMaxConnectionsPerHost(hostConfiguration, diamondConfigure.getMaxHostConnections());
    params.setMaxTotalConnections(diamondConfigure.getMaxTotalConnections());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    // 设置读超时为1分钟,
    // [email protected]
    params.setSoTimeout(60 * 1000);

    connectionManager.setParams(params);
    httpClient = new HttpClient(connectionManager);
    httpClient.setHostConfiguration(hostConfiguration);
}
 
Example 3
Source File: HttpClientUtil.java    From javabase with Apache License 2.0 6 votes vote down vote up
private static byte[] executeMethod(HttpMethodBase method, int timeout) throws Exception {
	InputStream in = null;
	try {
		method.addRequestHeader("Connection", "close");
		HttpClient client = new HttpClient();
		HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
		params.setConnectionTimeout(timeout);
		params.setSoTimeout(timeout);
		params.setStaleCheckingEnabled(false);
		ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFFER_SIZE);
		client.executeMethod(method);
		in = method.getResponseBodyAsStream();
		byte[] buffer = new byte[BUFFER_SIZE];
		int len;
		while( (len = in.read(buffer)) > 0) {
			baos.write(buffer, 0, len);
		}
		return baos.toByteArray();
	} finally {
		if (in != null) {
			in.close();
		}
	}
}
 
Example 4
Source File: DefaultDiamondSubscriber.java    From diamond with Apache License 2.0 6 votes vote down vote up
protected void initHttpClient() {
    if (MockServer.isTestMode()) {
        return;
    }
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(diamondConfigure.getDomainNameList().get(this.domainNamePos.get()),
        diamondConfigure.getPort());

    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.closeIdleConnections(diamondConfigure.getPollingIntervalTime() * 4000);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setMaxConnectionsPerHost(hostConfiguration, diamondConfigure.getMaxHostConnections());
    params.setMaxTotalConnections(diamondConfigure.getMaxTotalConnections());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    // 设置读超时为1分钟,
    // [email protected]
    params.setSoTimeout(60 * 1000);

    connectionManager.setParams(params);
    httpClient = new HttpClient(connectionManager);
    httpClient.setHostConfiguration(hostConfiguration);
}
 
Example 5
Source File: ServerAddressProcessor.java    From diamond with Apache License 2.0 5 votes vote down vote up
private void initHttpClient() {
    HostConfiguration hostConfiguration = new HostConfiguration();

    SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
    connectionManager.closeIdleConnections(5000L);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    connectionManager.setParams(params);

    configHttpClient = new HttpClient(connectionManager);
    configHttpClient.setHostConfiguration(hostConfiguration);
}
 
Example 6
Source File: HttpClientBuilder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Builds an HTTP client with the given settings. Settings are NOT reset to their default values after a client has
 * been created.
 * 
 * @return the created client.
 */
public HttpClient buildClient() {
    if (httpsProtocolSocketFactory != null) {
        Protocol.registerProtocol("https", new Protocol("https", httpsProtocolSocketFactory, 443));
    }

    HttpClientParams clientParams = new HttpClientParams();
    clientParams.setAuthenticationPreemptive(isPreemptiveAuthentication());
    clientParams.setContentCharset(getContentCharSet());
    clientParams.setParameter(HttpClientParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(
            connectionRetryAttempts, false));

    HttpConnectionManagerParams connMgrParams = new HttpConnectionManagerParams();
    connMgrParams.setConnectionTimeout(getConnectionTimeout());
    connMgrParams.setDefaultMaxConnectionsPerHost(getMaxConnectionsPerHost());
    connMgrParams.setMaxTotalConnections(getMaxTotalConnections());
    connMgrParams.setReceiveBufferSize(getReceiveBufferSize());
    connMgrParams.setSendBufferSize(getSendBufferSize());
    connMgrParams.setTcpNoDelay(isTcpNoDelay());

    MultiThreadedHttpConnectionManager connMgr = new MultiThreadedHttpConnectionManager();
    connMgr.setParams(connMgrParams);

    HttpClient httpClient = new HttpClient(clientParams, connMgr);

    if (proxyHost != null) {
        HostConfiguration hostConfig = new HostConfiguration();
        hostConfig.setProxy(proxyHost, proxyPort);
        httpClient.setHostConfiguration(hostConfig);

        if (proxyUsername != null) {
            AuthScope proxyAuthScope = new AuthScope(proxyHost, proxyPort);
            UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(proxyUsername,
                    proxyPassword);
            httpClient.getState().setProxyCredentials(proxyAuthScope, proxyCredentials);
        }
    }

    return httpClient;
}
 
Example 7
Source File: OldHttpClientApi.java    From javabase with Apache License 2.0 5 votes vote down vote up
private static byte[] executeMethod(HttpMethodBase method, int timeout) throws Exception {
    InputStream in = null;
    try {
        method.addRequestHeader("Connection", "close");
        HttpClient client = new HttpClient();
        HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
        //设置连接时候一些参数
        params.setConnectionTimeout(timeout);
        params.setSoTimeout(timeout);
        params.setStaleCheckingEnabled(false);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFFER_SIZE);

        int stat =  client.executeMethod(method);
        if (stat != HttpStatus.SC_OK)
            log.error("get失败!");

        //method.getResponseBody()
        in = method.getResponseBodyAsStream();
        byte[] buffer = new byte[BUFFER_SIZE];
        int len;
        while ((len = in.read(buffer)) > 0) {
            baos.write(buffer, 0, len);
        }
        return baos.toByteArray();
    }
    finally {
        if (in != null) {
            in.close();
        }
    }
}
 
Example 8
Source File: CFActivator.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
private HttpClient createHttpClient() {
	//see http://hc.apache.org/httpclient-3.x/threading.html
	MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
	HttpConnectionManagerParams params = connectionManager.getParams();
	params.setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, PreferenceHelper.getInt(ServerConstants.HTTP_MAX_CONN_HOST_CONF_KEY, 50));
	params.setMaxTotalConnections(PreferenceHelper.getInt(ServerConstants.HTTP_MAX_CONN_TOTAL_CONF_KEY, 150));
	params.setConnectionTimeout(PreferenceHelper.getInt(ServerConstants.HTTP_CONN_TIMEOUT_CONF_KEY, 15000)); //15s
	params.setSoTimeout(PreferenceHelper.getInt(ServerConstants.HTTP_SO_TIMEOUT_CONF_KEY, 30000)); //30s
	connectionManager.setParams(params);

	HttpClientParams clientParams = new HttpClientParams();
	clientParams.setConnectionManagerTimeout(PreferenceHelper.getInt(ServerConstants.HTTP_CONN_MGR_TIMEOUT_CONF_KEY, 300000)); // 5 minutes

	return new HttpClient(clientParams, connectionManager);
}
 
Example 9
Source File: AuthUtil.java    From roncoo-pay with Apache License 2.0 5 votes vote down vote up
/**
 * post请求
 *
 * @param paramMap   请求参数
 * @param requestUrl 请求地址
 * @return
 */
private static Map<String, Object> request(SortedMap<String, String> paramMap, String requestUrl) {
    logger.info("鉴权请求地址:[{}],请求参数:[{}]", requestUrl, paramMap);
    HttpClient httpClient = new HttpClient();
    HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
    // 设置连接超时时间(单位毫秒)
    managerParams.setConnectionTimeout(9000);
    // 设置读数据超时时间(单位毫秒)
    managerParams.setSoTimeout(12000);
    PostMethod postMethod = new PostMethod(requestUrl);
    postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
    NameValuePair[] pairs = new NameValuePair[paramMap.size()];
    int i = 0;
    for (Map.Entry<String, String> entry : paramMap.entrySet()) {
        pairs[i++] = new NameValuePair(entry.getKey(), entry.getValue());
    }
    postMethod.setRequestBody(pairs);
    try {
        Integer code = httpClient.executeMethod(postMethod);
        if (code.compareTo(200) == 0) {
            String result = postMethod.getResponseBodyAsString();
            logger.info("鉴权请求成功,同步返回数据:{}", result);
            return JSON.parseObject(result);
        } else {
            logger.error("鉴权请求失败,返回状态码:[{}]", code);
        }
    } catch (IOException e) {
        logger.info("鉴权请求异常:{}", e);
        return null;
    }
    return null;
}
 
Example 10
Source File: ServerAddressProcessor.java    From diamond with Apache License 2.0 5 votes vote down vote up
private void initHttpClient() {
    HostConfiguration hostConfiguration = new HostConfiguration();

    SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
    connectionManager.closeIdleConnections(5000L);

    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setStaleCheckingEnabled(diamondConfigure.isConnectionStaleCheckingEnabled());
    params.setConnectionTimeout(diamondConfigure.getConnectionTimeout());
    connectionManager.setParams(params);

    configHttpClient = new HttpClient(connectionManager);
    configHttpClient.setHostConfiguration(hostConfiguration);
}
 
Example 11
Source File: CoreServerInitializer.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private ConfigurationContext getClientConfigurationContext() throws AxisFault {
    String clientRepositoryLocation = serverConfigurationService.getFirstProperty(CLIENT_REPOSITORY_LOCATION);
    String clientAxis2XmlLocationn = serverConfigurationService.getFirstProperty(CLIENT_AXIS2_XML_LOCATION);
    ConfigurationContext clientConfigContextToReturn = ConfigurationContextFactory
            .createConfigurationContextFromFileSystem(clientRepositoryLocation, clientAxis2XmlLocationn);
    MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();

    // Set the default max connections per host
    int defaultMaxConnPerHost = 500;
    Parameter defaultMaxConnPerHostParam = clientConfigContextToReturn.getAxisConfiguration()
            .getParameter("defaultMaxConnPerHost");
    if (defaultMaxConnPerHostParam != null) {
        defaultMaxConnPerHost = Integer.parseInt((String) defaultMaxConnPerHostParam.getValue());
    }
    params.setDefaultMaxConnectionsPerHost(defaultMaxConnPerHost);

    // Set the max total connections
    int maxTotalConnections = 15000;
    Parameter maxTotalConnectionsParam = clientConfigContextToReturn.getAxisConfiguration()
            .getParameter("maxTotalConnections");
    if (maxTotalConnectionsParam != null) {
        maxTotalConnections = Integer.parseInt((String) maxTotalConnectionsParam.getValue());
    }
    params.setMaxTotalConnections(maxTotalConnections);

    params.setSoTimeout(600000);
    params.setConnectionTimeout(600000);

    httpConnectionManager.setParams(params);
    clientConfigContextToReturn
            .setProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER, httpConnectionManager);

    clientConfigContextToReturn.setProperty(MicroIntegratorBaseConstants.WORK_DIR, serverWorkDir);
    return clientConfigContextToReturn;
}
 
Example 12
Source File: AuthUtilsTest.java    From httpclientAuthHelper with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    super.setUp();
    MultiThreadedHttpConnectionManager connManag =  new MultiThreadedHttpConnectionManager();

    HttpConnectionManagerParams managParams = connManag.getParams();

    managParams.setConnectionTimeout(10000); // 1
    managParams.setSoTimeout(10000); //2
    client = new HttpClient(connManag);
    client.getParams().setParameter("http.connection-manager.timeout", new Long(10000)); //3


}
 
Example 13
Source File: CrucibleSessionImpl.java    From Crucible4IDEA with MIT License 5 votes vote down vote up
private void executeHttpMethod(@NotNull HttpMethodBase method) throws IOException {
  adjustHttpHeader(method);

  final HttpClient client = new HttpClient();
  HttpConnectionManagerParams params = client.getHttpConnectionManager().getParams();
  params.setConnectionTimeout(CONNECTION_TIMEOUT); //set connection timeout (how long it takes to connect to remote host)
  params.setSoTimeout(CONNECTION_TIMEOUT); //set socket timeout (how long it takes to retrieve data from remote host)
  client.executeMethod(method);
}
 
Example 14
Source File: Http.java    From anthelion with Apache License 2.0 4 votes vote down vote up
/**
 * Configures the HTTP client
 */
private void configureClient() {

  // Set up an HTTPS socket factory that accepts self-signed certs.
  Protocol https = new Protocol("https",
      new DummySSLProtocolSocketFactory(), 443);
  Protocol.registerProtocol("https", https);

  HttpConnectionManagerParams params = connectionManager.getParams();
  params.setConnectionTimeout(timeout);
  params.setSoTimeout(timeout);
  params.setSendBufferSize(BUFFER_SIZE);
  params.setReceiveBufferSize(BUFFER_SIZE);
  params.setMaxTotalConnections(maxThreadsTotal);

  // executeMethod(HttpMethod) seems to ignore the connection timeout on the connection manager.
  // set it explicitly on the HttpClient.
  client.getParams().setConnectionManagerTimeout(timeout);

  HostConfiguration hostConf = client.getHostConfiguration();
  ArrayList headers = new ArrayList();
  // Set the User Agent in the header
  headers.add(new Header("User-Agent", userAgent));
  // prefer English
  headers.add(new Header("Accept-Language", acceptLanguage));
  // prefer UTF-8
  headers.add(new Header("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.7"));
  // prefer understandable formats
  headers.add(new Header("Accept",
          "text/html,application/xml;q=0.9,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"));
  // accept gzipped content
  headers.add(new Header("Accept-Encoding", "x-gzip, gzip, deflate"));
  hostConf.getParams().setParameter("http.default-headers", headers);

  // HTTP proxy server details
  if (useProxy) {
    hostConf.setProxy(proxyHost, proxyPort);

    if (proxyUsername.length() > 0) {

      AuthScope proxyAuthScope = getAuthScope(
          this.proxyHost, this.proxyPort, this.proxyRealm);

      NTCredentials proxyCredentials = new NTCredentials(
          this.proxyUsername, this.proxyPassword,
          this.agentHost, this.proxyRealm);

      client.getState().setProxyCredentials(
          proxyAuthScope, proxyCredentials);
    }
  }

}
 
Example 15
Source File: Http.java    From nutch-htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Configures the HTTP client
 */
private void configureClient() {

  // Set up an HTTPS socket factory that accepts self-signed certs.
  ProtocolSocketFactory factory = new SSLProtocolSocketFactory();
  Protocol https = new Protocol("https", factory, 443);
  Protocol.registerProtocol("https", https);

  HttpConnectionManagerParams params = connectionManager.getParams();
  params.setConnectionTimeout(timeout);
  params.setSoTimeout(timeout);
  params.setSendBufferSize(BUFFER_SIZE);
  params.setReceiveBufferSize(BUFFER_SIZE);
  params.setMaxTotalConnections(maxThreadsTotal);

  // executeMethod(HttpMethod) seems to ignore the connection timeout on the connection manager.
  // set it explicitly on the HttpClient.
  client.getParams().setConnectionManagerTimeout(timeout);

  HostConfiguration hostConf = client.getHostConfiguration();
  ArrayList<Header> headers = new ArrayList<Header>();
  // Set the User Agent in the header
  headers.add(new Header("User-Agent", userAgent));
  // prefer English
  headers.add(new Header("Accept-Language", acceptLanguage));
  // prefer UTF-8
  headers.add(new Header("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.7"));
  // prefer understandable formats
  headers.add(new Header("Accept",
          "text/html,application/xml;q=0.9,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"));
  // accept gzipped content
  headers.add(new Header("Accept-Encoding", "x-gzip, gzip, deflate"));
  hostConf.getParams().setParameter("http.default-headers", headers);

  // HTTP proxy server details
  if (useProxy) {
    hostConf.setProxy(proxyHost, proxyPort);

    if (proxyUsername.length() > 0) {

      AuthScope proxyAuthScope = getAuthScope(
          this.proxyHost, this.proxyPort, this.proxyRealm);

      NTCredentials proxyCredentials = new NTCredentials(
          this.proxyUsername, this.proxyPassword,
          Http.agentHost, this.proxyRealm);

      client.getState().setProxyCredentials(
          proxyAuthScope, proxyCredentials);
    }
  }

}
 
Example 16
Source File: CommonsHttpTransport.java    From elasticsearch-hadoop with Apache License 2.0 4 votes vote down vote up
public CommonsHttpTransport(Settings settings, SecureSettings secureSettings, String host) {
    if (log.isDebugEnabled()) {
        log.debug("Creating new CommonsHttpTransport");
    }
    this.settings = settings;
    this.secureSettings = secureSettings;
    this.clusterName = settings.getClusterInfoOrUnnamedLatest().getClusterName().getName(); // May be a bootstrap client.
    if (StringUtils.hasText(settings.getSecurityUserProviderClass())) {
        this.userProvider = UserProvider.create(settings);
    } else {
        this.userProvider = null;
    }
    httpInfo = host;
    sslEnabled = settings.getNetworkSSLEnabled();

    String pathPref = settings.getNodesPathPrefix();
    pathPrefix = (StringUtils.hasText(pathPref) ? addLeadingSlashIfNeeded(StringUtils.trimWhitespace(pathPref)) : StringUtils.trimWhitespace(pathPref));

    HttpClientParams params = new HttpClientParams();
    params.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(
            settings.getHttpRetries(), false) {

        @Override
        public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
            if (super.retryMethod(method, exception, executionCount)) {
                stats.netRetries++;
                return true;
            }
            return false;
        }
    });

    // Max time to wait for a connection from the connectionMgr pool
    params.setConnectionManagerTimeout(settings.getHttpTimeout());
    // Max time to wait for data from a connection.
    params.setSoTimeout((int) settings.getHttpTimeout());
    // explicitly set the charset
    params.setCredentialCharset(StringUtils.UTF_8.name());
    params.setContentCharset(StringUtils.UTF_8.name());

    HostConfiguration hostConfig = new HostConfiguration();

    hostConfig = setupSSLIfNeeded(settings, secureSettings, hostConfig);
    hostConfig = setupSocksProxy(settings, secureSettings, hostConfig);
    Object[] authSettings = setupHttpOrHttpsProxy(settings, secureSettings, hostConfig);
    hostConfig = (HostConfiguration) authSettings[0];

    try {
        hostConfig.setHost(new URI(escapeUri(host, sslEnabled), false));
    } catch (IOException ex) {
        throw new EsHadoopTransportException("Invalid target URI " + host, ex);
    }
    client = new HttpClient(params, new SocketTrackingConnectionManager());
    client.setHostConfiguration(hostConfig);

    addHttpAuth(settings, secureSettings, authSettings);
    completeAuth(authSettings);

    HttpConnectionManagerParams connectionParams = client.getHttpConnectionManager().getParams();
    // make sure to disable Nagle's protocol
    connectionParams.setTcpNoDelay(true);
    // Max time to wait to establish an initial HTTP connection
    connectionParams.setConnectionTimeout((int) settings.getHttpTimeout());

    this.headers = new HeaderProcessor(settings);

    if (log.isTraceEnabled()) {
        log.trace("Opening HTTP transport to " + httpInfo);
    }
}
 
Example 17
Source File: Session.java    From dacapobench with Apache License 2.0 2 votes vote down vote up
/**
 * Set connection manager parameters.
 * @param params
 */
protected void setConnectionManagerParams(HttpConnectionManagerParams params) {
  params.setConnectionTimeout(30000);
}