All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eworkcloud.okhttp.OkHttpConfiguration Maven / Gradle / Ivy

There is a newer version: 1.1.8
Show newest version
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