Skip to content

Commit 4fa0f44

Browse files
author
Yang Guo
committed
fix sync service pending on start since load agent list from cc
1 parent b8367b9 commit 4fa0f44

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

platform-api/src/main/java/com/flow/platform/api/service/SyncServiceImpl.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.springframework.beans.factory.annotation.Autowired;
5050
import org.springframework.beans.factory.annotation.Value;
5151
import org.springframework.scheduling.annotation.Scheduled;
52+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
5253
import org.springframework.stereotype.Service;
5354

5455
/**
@@ -76,6 +77,9 @@ public class SyncServiceImpl implements SyncService {
7677
@Autowired
7778
private AgentService agentService;
7879

80+
@Autowired
81+
private ThreadPoolTaskExecutor taskExecutor;
82+
7983
@Value("${domain.api}")
8084
private String apiDomain;
8185

@@ -85,14 +89,18 @@ public class SyncServiceImpl implements SyncService {
8589
private void init() {
8690
callbackUrl = HttpURL.build(apiDomain).append("/hooks/sync").toString();
8791

88-
try {
89-
List<Agent> agents = agentService.list();
90-
for (Agent agent : agents) {
91-
register(agent.getPath());
92+
taskExecutor.execute(() -> {
93+
try {
94+
LOGGER.trace("Start to init agent list in thread: " + Thread.currentThread().getName());
95+
96+
List<Agent> agents = agentService.list();
97+
for (Agent agent : agents) {
98+
register(agent.getPath());
99+
}
100+
} catch (Throwable e) {
101+
LOGGER.warn(e.getMessage());
92102
}
93-
} catch (Throwable e) {
94-
LOGGER.warn("Cannot load agent list " + e.getMessage());
95-
}
103+
});
96104
}
97105

98106
@Override

platform-util-http/src/main/java/com/flow/platform/util/http/HttpClient.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.flow.platform.util.http.HttpResponse.EXCEPTION_STATUS_CODE;
2020

21+
import com.flow.platform.util.StringUtil;
2122
import java.io.IOException;
2223
import java.io.InputStream;
2324
import java.io.UnsupportedEncodingException;
@@ -29,14 +30,15 @@
2930
import java.util.function.Consumer;
3031
import org.apache.commons.codec.binary.Base64;
3132
import org.apache.http.HttpEntity;
33+
import org.apache.http.client.config.RequestConfig;
3234
import org.apache.http.client.methods.CloseableHttpResponse;
3335
import org.apache.http.client.methods.HttpGet;
3436
import org.apache.http.client.methods.HttpPost;
3537
import org.apache.http.client.methods.HttpRequestBase;
3638
import org.apache.http.entity.ContentType;
3739
import org.apache.http.entity.StringEntity;
3840
import org.apache.http.impl.client.CloseableHttpClient;
39-
import org.apache.http.impl.client.HttpClients;
41+
import org.apache.http.impl.client.HttpClientBuilder;
4042
import org.apache.http.util.EntityUtils;
4143

4244
/**
@@ -63,6 +65,14 @@ public static HttpClient build(String url) {
6365
return new HttpClient(url);
6466
}
6567

68+
private final static int HTTP_TIMEOUT = 5 * 1000;
69+
70+
private final RequestConfig config = RequestConfig.custom()
71+
.setConnectTimeout(HTTP_TIMEOUT)
72+
.setConnectionRequestTimeout(HTTP_TIMEOUT)
73+
.setSocketTimeout(HTTP_TIMEOUT)
74+
.build();
75+
6676
private final String url;
6777

6878
private HttpRequestBase httpRequest;
@@ -142,7 +152,7 @@ public HttpResponse<String> bodyAsString() {
142152

143153
exec(httpResponse -> {
144154
if (httpResponse == null) {
145-
wrapper.add(new HttpResponse<>(retried, EXCEPTION_STATUS_CODE, exceptions, ""));
155+
wrapper.add(new HttpResponse<>(retried, EXCEPTION_STATUS_CODE, exceptions, StringUtil.EMPTY));
146156
return;
147157
}
148158

@@ -152,7 +162,7 @@ public HttpResponse<String> bodyAsString() {
152162
wrapper.add(new HttpResponse<>(retried, statusCode, exceptions, body));
153163
} catch (IOException e) {
154164
exceptions.add(e);
155-
wrapper.add(new HttpResponse<>(retried, EXCEPTION_STATUS_CODE, exceptions, ""));
165+
wrapper.add(new HttpResponse<>(retried, EXCEPTION_STATUS_CODE, exceptions, StringUtil.EMPTY));
156166
}
157167
});
158168

@@ -186,7 +196,7 @@ private void exec(Consumer<CloseableHttpResponse> consumer) {
186196
return;
187197
}
188198

189-
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
199+
try (CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build()) {
190200
try (CloseableHttpResponse response = httpClient.execute(httpRequest)) {
191201
int statusCode = response.getStatusLine().getStatusCode();
192202

0 commit comments

Comments
 (0)