Skip to content

Commit b834352

Browse files
authored
🎨 新增okhttp实现接口请求的单例模式代码
1 parent a47bc22 commit b834352

File tree

8 files changed

+733
-23
lines changed

8 files changed

+733
-23
lines changed
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
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+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package me.chanjar.weixin.common.util.http.okhttp;
2+
3+
import okhttp3.*;
4+
5+
import java.net.Proxy;
6+
import java.util.concurrent.TimeUnit;
7+
8+
/**
9+
* @author wulang
10+
**/
11+
public interface OkHttpClientBuilder {
12+
/**
13+
* 构建OkHttpClient实例.
14+
*
15+
* @return OkHttpClient
16+
*/
17+
OkHttpClient build();
18+
19+
/**
20+
* 代理
21+
*
22+
* @param proxy Proxy
23+
* @return OkHttpClientBuilder
24+
*/
25+
OkHttpClientBuilder proxy(Proxy proxy);
26+
27+
/**
28+
* 授权
29+
*
30+
* @param authenticator Authenticator
31+
* @return OkHttpClientBuilder
32+
*/
33+
OkHttpClientBuilder authenticator(Authenticator authenticator);
34+
35+
/**
36+
* 拦截器
37+
*
38+
* @param interceptor Interceptor
39+
* @return OkHttpClientBuilder
40+
*/
41+
OkHttpClientBuilder addInterceptor(Interceptor interceptor);
42+
43+
/**
44+
* 请求调度管理
45+
*
46+
* @param dispatcher Dispatcher
47+
* @return OkHttpClientBuilder
48+
*/
49+
OkHttpClientBuilder setDispatcher(Dispatcher dispatcher);
50+
51+
/**
52+
* 连接池
53+
*
54+
* @param connectionPool ConnectionPool
55+
* @return OkHttpClientBuilder
56+
*/
57+
OkHttpClientBuilder setConnectionPool(ConnectionPool connectionPool);
58+
59+
/**
60+
* 监听网络请求过程
61+
*
62+
* @param eventListenerFactory EventListener
63+
* @return OkHttpClientBuilder
64+
*/
65+
OkHttpClientBuilder setEventListenerFactory(EventListener.Factory eventListenerFactory);
66+
67+
/**
68+
* 是否支持失败重连
69+
*
70+
* @param retryOnConnectionFailure retryOnConnectionFailure
71+
* @return OkHttpClientBuilder
72+
*/
73+
OkHttpClientBuilder setRetryOnConnectionFailure(Boolean retryOnConnectionFailure);
74+
75+
/**
76+
* 是否允许重定向操作
77+
*
78+
* @param followRedirects followRedirects
79+
* @return OkHttpClientBuilder
80+
*/
81+
OkHttpClientBuilder setFollowRedirects(Boolean followRedirects);
82+
83+
/**
84+
* 连接建立的超时时间
85+
*
86+
* @param timeout 时长
87+
* @param unit 时间单位
88+
* @return OkHttpClientBuilder
89+
*/
90+
OkHttpClientBuilder connectTimeout(Long timeout, TimeUnit unit);
91+
92+
/**
93+
* 完整的请求过程超时时间
94+
*
95+
* @param timeout 时长
96+
* @param unit 时间单位
97+
* @return OkHttpClientBuilder
98+
*/
99+
OkHttpClientBuilder callTimeout(Long timeout, TimeUnit unit);
100+
101+
/**
102+
* 连接的IO读操作超时时间
103+
*
104+
* @param timeout 时长
105+
* @param unit 时间单位
106+
* @return OkHttpClientBuilder
107+
*/
108+
OkHttpClientBuilder readTimeout(Long timeout, TimeUnit unit);
109+
110+
/**
111+
* 连接的IO写操作超时时间
112+
*
113+
* @param timeout 时长
114+
* @param unit 时间单位
115+
* @return OkHttpClientBuilder
116+
*/
117+
OkHttpClientBuilder writeTimeout(Long timeout, TimeUnit unit);
118+
119+
/**
120+
* ping的时间间隔
121+
*
122+
* @param pingInterval ping的时间间隔
123+
* @return OkHttpClientBuilder
124+
*/
125+
OkHttpClientBuilder setPingInterval(Integer pingInterval);
126+
}

0 commit comments

Comments
 (0)