|
| 1 | +package me.chanjar.weixin.common.util.http.okhttp; |
| 2 | + |
| 3 | +import lombok.Data; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import okhttp3.*; |
| 6 | + |
| 7 | +import javax.annotation.concurrent.NotThreadSafe; |
| 8 | +import java.net.Proxy; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author wulang |
| 16 | + **/ |
| 17 | +@Slf4j |
| 18 | +@Data |
| 19 | +@NotThreadSafe |
| 20 | +public class DefaultOkHttpClientBuilder implements OkHttpClientBuilder { |
| 21 | + |
| 22 | + private final AtomicBoolean prepared = new AtomicBoolean(false); |
| 23 | + |
| 24 | + /** |
| 25 | + * 代理 |
| 26 | + */ |
| 27 | + private Proxy proxy; |
| 28 | + |
| 29 | + /** |
| 30 | + * 授权 |
| 31 | + */ |
| 32 | + private Authenticator authenticator; |
| 33 | + |
| 34 | + /** |
| 35 | + * 拦截器 |
| 36 | + */ |
| 37 | + private final List<Interceptor> interceptorList = new ArrayList<>(); |
| 38 | + |
| 39 | + /** |
| 40 | + * 请求调度管理 |
| 41 | + */ |
| 42 | + private Dispatcher dispatcher; |
| 43 | + |
| 44 | + /** |
| 45 | + * 连接池 |
| 46 | + */ |
| 47 | + private ConnectionPool connectionPool; |
| 48 | + |
| 49 | + /** |
| 50 | + * 监听网络请求过程 |
| 51 | + */ |
| 52 | + private EventListener.Factory eventListenerFactory; |
| 53 | + |
| 54 | + /** |
| 55 | + * 是否支持失败重连 |
| 56 | + */ |
| 57 | + private Boolean retryOnConnectionFailure; |
| 58 | + |
| 59 | + /** |
| 60 | + * 是否允许重定向操作 |
| 61 | + */ |
| 62 | + private Boolean followRedirects; |
| 63 | + |
| 64 | + /** |
| 65 | + * 连接建立的超时时长 |
| 66 | + */ |
| 67 | + private Long connectTimeout; |
| 68 | + |
| 69 | + /** |
| 70 | + * 连接建立的超时时间单位 |
| 71 | + */ |
| 72 | + private TimeUnit connectTimeUnit; |
| 73 | + |
| 74 | + /** |
| 75 | + * 完整的请求过程超时时长 |
| 76 | + */ |
| 77 | + private Long callTimeout; |
| 78 | + |
| 79 | + /** |
| 80 | + * 完整的请求过程超时时间单位 |
| 81 | + */ |
| 82 | + private TimeUnit callTimeUnit; |
| 83 | + |
| 84 | + /** |
| 85 | + * 连接的IO读操作超时时长 |
| 86 | + */ |
| 87 | + private Long readTimeout; |
| 88 | + |
| 89 | + /** |
| 90 | + * 连接的IO读操作超时时间单位 |
| 91 | + */ |
| 92 | + private TimeUnit readTimeUnit; |
| 93 | + |
| 94 | + /** |
| 95 | + * 连接的IO写操作超时时长 |
| 96 | + */ |
| 97 | + private Long writeTimeout; |
| 98 | + |
| 99 | + /** |
| 100 | + * 连接的IO写操作超时时间单位 |
| 101 | + */ |
| 102 | + private TimeUnit writeTimeUnit; |
| 103 | + |
| 104 | + /** |
| 105 | + * ping的时间间隔 |
| 106 | + */ |
| 107 | + private Integer pingInterval; |
| 108 | + |
| 109 | + /** |
| 110 | + * 持有client对象,仅初始化一次,避免多service实例的时候造成重复初始化的问题 |
| 111 | + */ |
| 112 | + private OkHttpClient okHttpClient; |
| 113 | + |
| 114 | + private DefaultOkHttpClientBuilder() { |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + public static DefaultOkHttpClientBuilder get() { |
| 119 | + return DefaultOkHttpClientBuilder.SingletonHolder.INSTANCE; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public OkHttpClient build() { |
| 124 | + if (!prepared.get()) { |
| 125 | + prepare(); |
| 126 | + } |
| 127 | + return this.okHttpClient; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public OkHttpClientBuilder proxy(Proxy proxy) { |
| 132 | + this.proxy = proxy; |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public OkHttpClientBuilder authenticator(Authenticator authenticator) { |
| 138 | + this.authenticator = authenticator; |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public OkHttpClientBuilder addInterceptor(Interceptor interceptor) { |
| 144 | + this.interceptorList.add(interceptor); |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public OkHttpClientBuilder setDispatcher(Dispatcher dispatcher) { |
| 150 | + this.dispatcher = dispatcher; |
| 151 | + return this; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public OkHttpClientBuilder setConnectionPool(ConnectionPool connectionPool) { |
| 156 | + this.connectionPool = connectionPool; |
| 157 | + return this; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public OkHttpClientBuilder setEventListenerFactory(EventListener.Factory eventListenerFactory) { |
| 162 | + this.eventListenerFactory = eventListenerFactory; |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public OkHttpClientBuilder setRetryOnConnectionFailure(Boolean retryOnConnectionFailure) { |
| 168 | + this.retryOnConnectionFailure = retryOnConnectionFailure; |
| 169 | + return this; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public OkHttpClientBuilder setFollowRedirects(Boolean followRedirects) { |
| 174 | + this.followRedirects = followRedirects; |
| 175 | + return this; |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public OkHttpClientBuilder connectTimeout(Long timeout, TimeUnit unit) { |
| 180 | + this.connectTimeout = timeout; |
| 181 | + this.connectTimeUnit = unit; |
| 182 | + return this; |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public OkHttpClientBuilder callTimeout(Long timeout, TimeUnit unit) { |
| 187 | + this.callTimeout = timeout; |
| 188 | + this.callTimeUnit = unit; |
| 189 | + return this; |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public OkHttpClientBuilder readTimeout(Long timeout, TimeUnit unit) { |
| 194 | + this.readTimeout = timeout; |
| 195 | + this.readTimeUnit = unit; |
| 196 | + return this; |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public OkHttpClientBuilder writeTimeout(Long timeout, TimeUnit unit) { |
| 201 | + this.writeTimeout = timeout; |
| 202 | + this.writeTimeUnit = unit; |
| 203 | + return this; |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public OkHttpClientBuilder setPingInterval(Integer pingInterval) { |
| 208 | + this.pingInterval = pingInterval; |
| 209 | + return this; |
| 210 | + } |
| 211 | + |
| 212 | + private synchronized void prepare() { |
| 213 | + if (prepared.get()) { |
| 214 | + return; |
| 215 | + } |
| 216 | + OkHttpClient.Builder builder = new OkHttpClient.Builder(); |
| 217 | + if (this.authenticator != null) { |
| 218 | + builder.authenticator(this.authenticator); |
| 219 | + } |
| 220 | + if (this.proxy != null) { |
| 221 | + builder.proxy(this.proxy); |
| 222 | + } |
| 223 | + for (Interceptor interceptor : this.interceptorList) { |
| 224 | + builder.addInterceptor(interceptor); |
| 225 | + } |
| 226 | + if (this.dispatcher != null) { |
| 227 | + builder.dispatcher(dispatcher); |
| 228 | + } |
| 229 | + if (this.connectionPool != null) { |
| 230 | + builder.connectionPool(connectionPool); |
| 231 | + } |
| 232 | + if (this.eventListenerFactory != null) { |
| 233 | + builder.eventListenerFactory(this.eventListenerFactory); |
| 234 | + } |
| 235 | + if (this.retryOnConnectionFailure != null) { |
| 236 | + builder.setRetryOnConnectionFailure$okhttp(this.retryOnConnectionFailure); |
| 237 | + } |
| 238 | + if (this.followRedirects != null) { |
| 239 | + builder.followRedirects(this.followRedirects); |
| 240 | + } |
| 241 | + if (this.connectTimeout != null && this.connectTimeUnit != null) { |
| 242 | + builder.connectTimeout(this.connectTimeout, this.connectTimeUnit); |
| 243 | + } |
| 244 | + if (this.callTimeout != null && this.callTimeUnit != null) { |
| 245 | + builder.callTimeout(this.callTimeout, this.callTimeUnit); |
| 246 | + } |
| 247 | + if (this.readTimeout != null && this.readTimeUnit != null) { |
| 248 | + builder.readTimeout(this.readTimeout, this.readTimeUnit); |
| 249 | + } |
| 250 | + if (this.writeTimeout != null && this.writeTimeUnit != null) { |
| 251 | + builder.writeTimeout(this.writeTimeout, this.writeTimeUnit); |
| 252 | + } |
| 253 | + if (this.pingInterval != null) { |
| 254 | + builder.setPingInterval$okhttp(this.pingInterval); |
| 255 | + } |
| 256 | + this.okHttpClient = builder.build(); |
| 257 | + prepared.set(true); |
| 258 | + } |
| 259 | + |
| 260 | + private static class SingletonHolder { |
| 261 | + private static final DefaultOkHttpClientBuilder INSTANCE = new DefaultOkHttpClientBuilder(); |
| 262 | + } |
| 263 | +} |
0 commit comments