Java Code Examples for org.eclipse.jetty.client.HttpClient#setConnectTimeout()

The following examples show how to use org.eclipse.jetty.client.HttpClient#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: InjectionModule.java    From ja-micro with Apache License 2.0 6 votes vote down vote up
private HttpClient createHttpClient() {
    //Allow ssl by default
    SslContextFactory sslContextFactory = new SslContextFactory();
    //Don't exclude RSA because Sixt needs them, dammit!
    sslContextFactory.setExcludeCipherSuites("");
    HttpClient client = new HttpClient(sslContextFactory);
    client.setFollowRedirects(false);
    client.setMaxConnectionsPerDestination(16);
    client.setRequestBufferSize(65536);
    client.setConnectTimeout(FeatureFlags.getHttpConnectTimeout(serviceProperties));
    client.setAddressResolutionTimeout(FeatureFlags.getHttpAddressResolutionTimeout(serviceProperties));
    //You can set more restrictive timeouts per request, but not less, so
    //  we set the maximum timeout of 1 hour here.
    client.setIdleTimeout(60 * 60 * 1000);
    try {
        client.start();
    } catch (Exception e) {
        logger.error("Error building http client", e);
    }
    return client;
}
 
Example 2
Source File: TestInjectionModule.java    From ja-micro with Apache License 2.0 6 votes vote down vote up
@Provides
public HttpClient getHttpClient() {
	HttpClient client = new HttpClient();
	client.setFollowRedirects(false);
	client.setMaxConnectionsPerDestination(32);
	client.setConnectTimeout(100);
	client.setAddressResolutionTimeout(100);
	//You can set more restrictive timeouts per request, but not less, so
	//  we set the maximum timeout of 1 hour here.
	client.setIdleTimeout(60 * 60 * 1000);
	try {
		client.start();
	} catch (Exception e) {
		logger.error("Error building http client", e);
	}
	return client;
}
 
Example 3
Source File: StreamClientImpl.java    From TVRemoteIME with GNU General Public License v2.0 5 votes vote down vote up
public StreamClientImpl(StreamClientConfigurationImpl configuration) throws InitializationException {
    this.configuration = configuration;

    log.info("Starting Jetty HttpClient...");
    client = new HttpClient();

    // Jetty client needs threads for its internal expiration routines, which we don't need but
    // can't disable, so let's abuse the request executor service for this
    client.setThreadPool(
        new ExecutorThreadPool(getConfiguration().getRequestExecutorService()) {
            @Override
            protected void doStop() throws Exception {
                // Do nothing, don't shut down the Cling ExecutorService when Jetty stops!
            }
        }
    );

    // These are some safety settings, we should never run into these timeouts as we
    // do our own expiration checking
    client.setTimeout((configuration.getTimeoutSeconds()+5) * 1000);
    client.setConnectTimeout((configuration.getTimeoutSeconds()+5) * 1000);

    client.setMaxRetries(configuration.getRequestRetryCount());

    try {
        client.start();
    } catch (Exception ex) {
        throw new InitializationException(
            "Could not start Jetty HTTP client: " + ex, ex
        );
    }
}
 
Example 4
Source File: HttpRpcEndpoint.java    From nutzcloud with Apache License 2.0 5 votes vote down vote up
public void init() throws Exception {
    client = new HttpClient(new SslContextFactory(true));
    client.setFollowRedirects(false);
    client.setCookieStore(new HttpCookieStore.Empty());

    executor = new QueuedThreadPool(conf.getInt(PRE + ".maxThreads", 256));
    client.setExecutor(executor);
    client.setMaxConnectionsPerDestination(conf.getInt(PRE + ".maxConnections", 256));
    client.setIdleTimeout(conf.getLong(PRE + ".idleTimeout", 30000));

    client.setConnectTimeout(conf.getLong(PRE + ".connectTime", 1000));

    if (conf.has(PRE + "requestBufferSize"))
        client.setRequestBufferSize(conf.getInt(PRE + "requestBufferSize"));

    if (conf.has(PRE + "responseBufferSize"))
        client.setResponseBufferSize(conf.getInt(PRE + "responseBufferSize"));

    client.start();

    // Content must not be decoded, otherwise the client gets confused.
    client.getContentDecoderFactories().clear();

    // Pass traffic to the client, only intercept what's necessary.
    ProtocolHandlers protocolHandlers = client.getProtocolHandlers();
    protocolHandlers.clear();
}
 
Example 5
Source File: JettyClientFactory.java    From moon-api-gateway with MIT License 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
    ExecutorService executor = Executors.newFixedThreadPool(jettyClientConfig.getThreadCount());
    httpClient = new HttpClient();
    try {
        httpClient.setMaxConnectionsPerDestination(jettyClientConfig.getMaxConnection());
        httpClient.setConnectTimeout(jettyClientConfig.getTimeout());
        httpClient.setExecutor(executor);
        httpClient.start();
    } catch(Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: HttpCertSigner.java    From athenz with Apache License 2.0 5 votes vote down vote up
void setupHttpClient(HttpClient client, long requestTimeout, long connectTimeout) {

        client.setFollowRedirects(false);
        client.setConnectTimeout(connectTimeout);
        client.setStopTimeout(TimeUnit.MILLISECONDS.convert(requestTimeout, TimeUnit.SECONDS));
        try {
            client.start();
        } catch (Exception ex) {
            LOGGER.error("HttpCertSigner: unable to start http client", ex);
            throw new ResourceException(ResourceException.INTERNAL_SERVER_ERROR,
                    "Http client not available");
        }
    }
 
Example 7
Source File: RestBufferManager.java    From incubator-retired-htrace with Apache License 2.0 5 votes vote down vote up
/**
 * Create an HttpClient instance.
 *
 * @param connTimeout         The timeout to use for connecting.
 * @param idleTimeout         The idle timeout to use.
 */
static HttpClient createHttpClient(long connTimeout, long idleTimeout) {
  HttpClient httpClient = new HttpClient();
  httpClient.setUserAgentField(
      new HttpField(HttpHeader.USER_AGENT, "HTracedSpanReceiver"));
  httpClient.setConnectTimeout(connTimeout);
  httpClient.setIdleTimeout(idleTimeout);
  return httpClient;
}
 
Example 8
Source File: StreamClientImpl.java    From DroidDLNA with GNU General Public License v3.0 5 votes vote down vote up
public StreamClientImpl(StreamClientConfigurationImpl configuration) throws InitializationException {
    this.configuration = configuration;

    log.info("Starting Jetty HttpClient...");
    client = new HttpClient();

    // Jetty client needs threads for its internal expiration routines, which we don't need but
    // can't disable, so let's abuse the request executor service for this
    client.setThreadPool(
        new ExecutorThreadPool(getConfiguration().getRequestExecutorService()) {
            @Override
            protected void doStop() throws Exception {
                // Do nothing, don't shut down the Cling ExecutorService when Jetty stops!
            }
        }
    );

    // These are some safety settings, we should never run into these timeouts as we
    // do our own expiration checking
    client.setTimeout((configuration.getTimeoutSeconds()+5) * 1000);
    client.setConnectTimeout((configuration.getTimeoutSeconds()+5) * 1000);

    client.setMaxRetries(configuration.getRequestRetryCount());

    try {
        client.start();
    } catch (Exception ex) {
        throw new InitializationException(
            "Could not start Jetty HTTP client: " + ex, ex
        );
    }
}
 
Example 9
Source File: ForceStreamConsumer.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private BayeuxClient makeClient() throws Exception {
  httpClient = new HttpClient(ForceUtils.makeSslContextFactory(conf));
  httpClient.setConnectTimeout(CONNECTION_TIMEOUT);
  httpClient.setIdleTimeout(READ_TIMEOUT);
  if (conf.useProxy) {
    ForceUtils.setProxy(httpClient, conf);
  }
  httpClient.start();

  final String sessionid = connection.getConfig().getSessionId();
  String soapEndpoint = connection.getConfig().getServiceEndpoint();
  String endpoint = soapEndpoint.substring(0, soapEndpoint.indexOf("/services/Soap/"));
  LOG.info("Server URL: {} ", endpoint);
  LOG.info("Session ID: {}", sessionid);

  Map<String, Object> options = new HashMap<>();
  options.put(ClientTransport.MAX_NETWORK_DELAY_OPTION, READ_TIMEOUT);
  options.put(LongPollingTransport.MAX_BUFFER_SIZE_OPTION, conf.streamingBufferSize);
  LongPollingTransport transport = new LongPollingTransport(options, httpClient) {

    @Override
    protected void customize(Request request) {
      super.customize(request);
      request.header(HttpHeader.AUTHORIZATION, "OAuth " + sessionid);
    }
  };

  String streamingEndpoint = salesforceStreamingEndpoint(endpoint);

  LOG.info("Streaming Endpoint: {}", streamingEndpoint);

  return new BayeuxClient(streamingEndpoint, transport);
}