public static void main(String... args) throws Exception { Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(20)); dispatcher.setMaxRequests(20); dispatcher.setMaxRequestsPerHost(1); OkHttpClient okHttpClient = new OkHttpClient.Builder() .dispatcher(dispatcher) .connectionPool(new ConnectionPool(100, 30, TimeUnit.SECONDS)) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(HttpUrl.get("https://example.com/")) .addConverterFactory(PageAdapter.FACTORY) .client(okHttpClient) .build(); PageService pageService = retrofit.create(PageService.class); Crawler crawler = new Crawler(pageService); crawler.crawlPage(HttpUrl.get(args[0])); }
private OkHttpClient createOkHttpClient(final NiFiProperties properties) { final String connectionTimeout = properties.getClusterNodeConnectionTimeout(); final long connectionTimeoutMs = FormatUtils.getTimeDuration(connectionTimeout, TimeUnit.MILLISECONDS); final String readTimeout = properties.getClusterNodeReadTimeout(); final long readTimeoutMs = FormatUtils.getTimeDuration(readTimeout, TimeUnit.MILLISECONDS); OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient().newBuilder(); okHttpClientBuilder.connectTimeout(connectionTimeoutMs, TimeUnit.MILLISECONDS); okHttpClientBuilder.readTimeout(readTimeoutMs, TimeUnit.MILLISECONDS); okHttpClientBuilder.followRedirects(true); final int connectionPoolSize = properties.getClusterNodeMaxConcurrentRequests(); okHttpClientBuilder.connectionPool(new ConnectionPool(connectionPoolSize, 5, TimeUnit.MINUTES)); final Tuple<SSLSocketFactory, X509TrustManager> tuple = createSslSocketFactory(properties); if (tuple != null) { okHttpClientBuilder.sslSocketFactory(tuple.getKey(), tuple.getValue()); } return okHttpClientBuilder.build(); }
.connectionPool(new ConnectionPool(0, 1, TimeUnit.SECONDS)) .dns(hostname -> { if (hostname.endsWith(SOCKET_SUFFIX)) {
@Override public ConnectionPool create(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) { return new ConnectionPool(maxIdleConnections, keepAliveDuration, timeUnit); } }
private static void init() { //连接池 connectionPool = new ConnectionPool(DEFAULT_MAX_IDLE_CONNECTIONS , DEFAULT_KEEP_ALIVE_DURATION, TimeUnit.MINUTES); //客户端构建对象 defaultBuilder = new OkHttpClient.Builder(); //设置超时时间 defaultBuilder.connectTimeout(DEFAULT_CONNECT_TIMEOUT, TimeUnit.SECONDS) .writeTimeout(DEFAULT_WRITE_TIMEOUT, TimeUnit.SECONDS) .readTimeout(DEFAULT_READ_TIMEOUT, TimeUnit.SECONDS) .connectionPool(connectionPool) //失败重连 .retryOnConnectionFailure(true) .followRedirects(true) .followSslRedirects(true) ; defaultClient = defaultBuilder.build(); // Runtime.getRuntime().addShutdownHook(new Thread(() -> connectionPool.evictAll())); }
@VisibleForTesting static okhttp3.OkHttpClient newRawOkHttpClient( Optional<TrustContext> trustContext, Optional<ProxySelector> proxySelector, String userAgent) { // Don't allow retrying on connection failures - see ticket #2194 okhttp3.OkHttpClient.Builder builder = new okhttp3.OkHttpClient.Builder() .connectionSpecs(CONNECTION_SPEC_WITH_CYPHER_SUITES) .connectionPool(new ConnectionPool(CONNECTION_POOL_SIZE, KEEP_ALIVE_TIME_MILLIS, TimeUnit.MILLISECONDS)) .proxySelector(proxySelector.orElse(ProxySelector.getDefault())) .retryOnConnectionFailure(false); if (trustContext.isPresent()) { builder.sslSocketFactory(trustContext.get().sslSocketFactory(), trustContext.get().x509TrustManager()); } builder.interceptors().add(new UserAgentAddingInterceptor(userAgent)); globalClientSettings.accept(builder); return builder.build(); }
@Override public ConnectionPool create(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) { return new ConnectionPool(maxIdleConnections, keepAliveDuration, timeUnit); } }
(new Runnable() { @Override public void run() { try { connection = new ConnectionPool("jdbc:mysql://127.0.0.0/comm", "root", "pass"); } catch (Exception e) { } } }).run();
/** * Set the maximum idle connections for the HTTP client. Default is 5. * * @param maxIdleConnections the maximum idle connections * @return the builder itself for chaining * @deprecated use {@link #withConnectionPool(ConnectionPool)} instead */ @Deprecated public Builder withMaxIdleConnections(int maxIdleConnections) { this.connectionPool = new ConnectionPool(maxIdleConnections, 5, TimeUnit.MINUTES); return this; }
Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("DbConnection"); ConnectionPool connectionPool = new ConnectionPool(ds)
@Bean public okhttp3.OkHttpClient okHttpClient () { return new okhttp3.OkHttpClient.Builder() .readTimeout( readTimeout , TimeUnit.SECONDS ) .connectTimeout( connectTimeout , TimeUnit.SECONDS ) .writeTimeout( writeTimeout , TimeUnit.SECONDS ) .connectionPool( new ConnectionPool() ) .build(); }
@Bean OkHttpClient okHttpClient(@Value("${client.connection.pool.size}") int connectionPoolSize) { return new OkHttpClient.Builder() .connectionPool(new ConnectionPool(connectionPoolSize, 5, TimeUnit.MINUTES)) .build(); }
/** * Creates a default OkHttp client builder that can be customized later and * then passed to one of the constructors taking a builder instance. The * constructors that don't take builders themselves use this method * internally to create their client builders. Note: The returned Builder * also has a default connection pool configured. You can replace that pool * by calling {@link Builder#connectionPool(okhttp3.ConnectionPool) }. * * @return a new OkHttp client builder, intialized with default settings. */ public static OkHttpClient.Builder createDefaultOkHttpClientBuilder() { OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.connectTimeout(10, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS); builder.connectionPool(new ConnectionPool(10, 10, TimeUnit.MINUTES)); return builder; }
OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setConnectionPool(new ConnectionPool());
private static OkHttpClient createOkHttpClient(String userAgent) { return new OkHttpClient.Builder() .addInterceptor(UserAgentInterceptor.of(userAgent)) .connectionPool(new ConnectionPool(100, 10, TimeUnit.MINUTES)) .build(); }
@Bean @ConditionalOnMissingBean public ConnectionPool connectionPool(OkHttpClientProperties okHttpClientProperties) { Connection connection = okHttpClientProperties.getConnection(); return new ConnectionPool(connection.getMaxIdleConnections(), connection.getKeepAliveDuration(), TimeUnit.MILLISECONDS); }
/** * Mark this as a bean for user to easy monitor the connection status */ @Bean @ConditionalOnMissingBean public ConnectionPool connectionPool(RetrofitProperties properties) { Connection connection = properties.getConnection(); return new ConnectionPool(connection.getMaxIdleConnections(), connection.getKeepAliveDuration(), MINUTES); }
public JiraService(final Site jiraSite) { this.jiraSite = jiraSite; final ConnectionPool CONNECTION_POOL = new ConnectionPool(5, 60, TimeUnit.SECONDS); OkHttpClient httpClient = new OkHttpClient.Builder() .connectTimeout(jiraSite.getTimeout(), TimeUnit.MILLISECONDS) .readTimeout(jiraSite.getReadTimeout(), TimeUnit.MILLISECONDS) .connectionPool(CONNECTION_POOL) .retryOnConnectionFailure(true).addInterceptor(new SigningInterceptor(jiraSite)).build(); final ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JodaModule()); this.jiraEndPoints = new Retrofit.Builder().baseUrl(this.jiraSite.getUrl().toString()) .addConverterFactory(JacksonConverterFactory.create(mapper)) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).client(httpClient).build() .create(JiraEndPoints.class); }
public OkHttpRequestExecutor(HttpProperties props) { client = new OkHttpClient.Builder() .connectionPool(new ConnectionPool(props.getMaxIdleConnections(), props.getKeepAliveDuration(), TimeUnit.MILLISECONDS)) .connectTimeout(props.getConnectionTimeout(), TimeUnit.MILLISECONDS) .readTimeout(props.getReadTimeout(), TimeUnit.MILLISECONDS) .build(); }
private void loadHTTPClient() { HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel( HttpLoggingInterceptor.Level.NONE); SocketFactory f = getSocketFactory(); OkHttpClient.Builder builder = new OkHttpClient.Builder() .followRedirects(true) .followSslRedirects(true) .connectTimeout(15, TimeUnit.SECONDS) .connectionPool(new ConnectionPool()) .addInterceptor(logging); if(f != null) { builder.sslSocketFactory(f, (X509TrustManager) f.getTrustManagers()[0]); } mHttpClient = builder.build(); }