com.eworkcloud.okhttp.OkHttpConfiguration Maven / Gradle / Ivy
package com.eworkcloud.okhttp;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@Configuration
@EnableConfigurationProperties(OkHttpProperties.class)
public class OkHttpConfiguration {
@Resource
private OkHttpProperties okHttpProperties;
@Bean
public OkHttpClient okHttpClient() {
ConnectionPool connectionPool;
if (null != okHttpProperties.getPool()) {
connectionPool = new ConnectionPool(okHttpProperties.getPool().getMaxIdleConnections(),
okHttpProperties.getPool().getKeepAliveDuration(), TimeUnit.MINUTES);
} else {
connectionPool = new ConnectionPool();
}
return new OkHttpClient().newBuilder()
.connectionPool(connectionPool)
.connectTimeout(okHttpProperties.getConnectTimeout(), TimeUnit.SECONDS)
.readTimeout(okHttpProperties.getReadTimeout(), TimeUnit.SECONDS)
.writeTimeout(okHttpProperties.getWriteTimeout(), TimeUnit.SECONDS)
.followRedirects(okHttpProperties.isFollowRedirects())
.followSslRedirects(okHttpProperties.isFollowSslRedirects())
.retryOnConnectionFailure(okHttpProperties.isRetryOnConnectionFailure())
.build();
}
@Bean
public OkHttpTemplate okHttpTemplate() {
return new OkHttpTemplate(okHttpClient());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy