Skip to content

Commit beceabe

Browse files
committed
sdk初始化增加dispatcher配置
1 parent 636e8a5 commit beceabe

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

dependency-reduced-pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.easemob.im</groupId>
55
<artifactId>im-sdk-core</artifactId>
66
<name>im-sdk-core</name>
7-
<version>1.0.14.1-SNAPSHOT</version>
7+
<version>1.0.15-SNAPSHOT</version>
88
<description>OpenAPI Java</description>
99
<url>https://github.com/openapitools/openapi-generator</url>
1010
<developers>

src/main/java/com/easemob/im/ApiClient.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public ApiClient() {
115115
}
116116

117117
public ApiClient(String basePath, Realm realm, String appKey, String clientId, String clientSecret, String appId, String appCert, int maxIdleConnections, int connectKeepAliveMilliSeconds,
118-
int connectTimeoutMilliSeconds, EMProxy proxy)
118+
int connectTimeoutMilliSeconds, EMProxy proxy, int dispatcherMaxRequests, int dispatcherMaxRequestsPerHost)
119119
throws ApiException {
120120
this.basePath = basePath;
121121
this.realm = realm;
@@ -134,7 +134,8 @@ public ApiClient(String basePath, Realm realm, String appKey, String clientId, S
134134
);
135135

136136
initHttpClient(Collections.<Interceptor>emptyList(), maxIdleConnections,
137-
connectKeepAliveMilliSeconds, connectTimeoutMilliSeconds, proxy);
137+
connectKeepAliveMilliSeconds, connectTimeoutMilliSeconds, proxy,
138+
dispatcherMaxRequests, dispatcherMaxRequestsPerHost);
138139
init();
139140

140141
// Setup authentications (key: authentication name, value: authentication).
@@ -159,7 +160,8 @@ public ApiClient(OkHttpClient client) throws ApiException {
159160
}
160161

161162
private void initHttpClient(List<Interceptor> interceptors, int maxIdleConnections,
162-
int connectKeepAliveMilliSeconds, int connectTimeoutMilliSeconds, EMProxy emProxy) {
163+
int connectKeepAliveMilliSeconds, int connectTimeoutMilliSeconds, EMProxy emProxy,
164+
int dispatcherMaxRequests, int dispatcherMaxRequestsPerHost) {
163165
OkHttpClient.Builder builder = new OkHttpClient.Builder();
164166
builder.addNetworkInterceptor(getProgressInterceptor());
165167
for (Interceptor interceptor: interceptors) {
@@ -192,6 +194,11 @@ private void initHttpClient(List<Interceptor> interceptors, int maxIdleConnectio
192194
}
193195
}
194196

197+
Dispatcher customDispatcher = new Dispatcher();
198+
customDispatcher.setMaxRequests(dispatcherMaxRequests);
199+
customDispatcher.setMaxRequestsPerHost(dispatcherMaxRequestsPerHost);
200+
201+
builder.dispatcher(customDispatcher);
195202
httpClient = builder.build();
196203
}
197204

@@ -353,6 +360,10 @@ public static class Builder {
353360

354361
private int connectTimeoutMilliSeconds = 10000;
355362

363+
private int dispatcherMaxRequests = 200;
364+
365+
private int dispatcherMaxRequestsPerHost = 50;
366+
356367
private EMProxy proxy;
357368

358369
public Builder setRealm(Realm realm) {
@@ -442,6 +453,22 @@ public Builder setConnectTimeoutMilliSeconds(int connectTimeoutMilliSeconds) {
442453
return this;
443454
}
444455

456+
public Builder setDispatcherMaxRequests(int dispatcherMaxRequests) {
457+
if (dispatcherMaxRequests < 0) {
458+
throw new IllegalArgumentException("dispatcherMaxRequests must not be negative");
459+
}
460+
this.dispatcherMaxRequests = dispatcherMaxRequests;
461+
return this;
462+
}
463+
464+
public Builder setDispatcherMaxRequestsPerHost(int dispatcherMaxRequestsPerHost) {
465+
if (dispatcherMaxRequestsPerHost < 0) {
466+
throw new IllegalArgumentException("dispatcherMaxRequestsPerHost must not be negative");
467+
}
468+
this.dispatcherMaxRequestsPerHost = dispatcherMaxRequestsPerHost;
469+
return this;
470+
}
471+
445472
public Builder setProxy(EMProxy proxy) {
446473
this.proxy = proxy;
447474
return this;
@@ -480,7 +507,7 @@ public ApiClient build() throws ApiException {
480507
return new ApiClient(this.basePath, this.realm, this.appKey, this.clientId,
481508
this.clientSecret, null, null, this.maxIdleConnections,
482509
this.connectKeepAliveMilliSeconds, this.connectTimeoutMilliSeconds,
483-
this.proxy);
510+
this.proxy, this.dispatcherMaxRequests, this.dispatcherMaxRequestsPerHost);
484511
} else if (this.realm.equals(Realm.AGORA_REALM)) {
485512
if (this.appId == null) {
486513
throw new IllegalArgumentException("appId not set");
@@ -494,7 +521,7 @@ public ApiClient build() throws ApiException {
494521

495522
return new ApiClient(this.basePath, this.realm, this.appKey, null, null, this.appId,
496523
this.appCert, this.maxIdleConnections, this.connectKeepAliveMilliSeconds,
497-
this.connectTimeoutMilliSeconds, this.proxy);
524+
this.connectTimeoutMilliSeconds, this.proxy, this.dispatcherMaxRequests, this.dispatcherMaxRequestsPerHost);
498525
} else {
499526
throw new IllegalArgumentException(
500527
String.format("invalid realm type %s", this.realm));

src/test/java/com/easemob/im/api/AbstractTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public abstract class AbstractTest {
1515
.setAppKey(System.getenv("IM_APPKEY"))
1616
.setClientId(System.getenv("IM_CLIENT_ID"))
1717
.setClientSecret(System.getenv("IM_CLIENT_SECRET"))
18+
.setDispatcherMaxRequests(300)
19+
.setDispatcherMaxRequestsPerHost(60)
1820
.build());
1921
} catch (ApiException e) {
2022
throw new RuntimeException(e);

0 commit comments

Comments
 (0)