@@ -111,6 +111,23 @@ protected <T> Mono<List<ConfigurationItem<T>>> doGet(String appId, String group,
111
111
// todo:need to get the specific env from system properties
112
112
String applicationName = appId + "_FAT" ;
113
113
String configurationName = keys .get (0 );
114
+ /*
115
+ check whether the config file has been initialized before
116
+ the function of if here is to first check init status to rough filter
117
+ */
118
+ if (!isInitialized (applicationName , configurationName )) {
119
+ /*
120
+ return Configuration.EMPTY if has been initialized before by others,
121
+ or return configuration which initialized just now.
122
+ */
123
+ Configuration <T > initConfiguration = initConfig (applicationName , configurationName , group , label , metadata , type );
124
+ if (!Objects .equals (initConfiguration , Configuration .EMPTY )) {
125
+ //init just now and the config value is the latest value,return immediately
126
+ return Mono .just (Lists .newArrayList (initConfiguration .getConfigurationItem ()));
127
+ }
128
+ }
129
+
130
+ //has been initialized before, and need to get the latest value
114
131
String clientConfigurationVersion = getCurVersion (applicationName , configurationName );
115
132
116
133
GetConfigurationRequest request = GetConfigurationRequest .builder ()
@@ -148,10 +165,21 @@ protected <T> Flux<SubscribeResp<T>> doSubscribe(String appId, String group, Str
148
165
return doSub (applicationName , configurationName , group , label , metadata , type , appId );
149
166
}
150
167
151
- private synchronized <T > Mono <Boolean > initConfig (String applicationName , String configurationName , String group , String label , Map <String , String > metadata , TypeRef <T > type ) {
168
+ /**
169
+ * @param applicationName applicationName
170
+ * @param configurationName configurationName
171
+ * @param group group
172
+ * @param label label
173
+ * @param metadata metadata
174
+ * @param type type
175
+ * @param <T> T
176
+ * @return return Configuration.EMPTY if has been initialized before by others and not been done this time;
177
+ * or return configuration value which initialized just now.
178
+ */
179
+ private synchronized <T > Configuration <T > initConfig (String applicationName , String configurationName , String group , String label , Map <String , String > metadata , TypeRef <T > type ) {
152
180
// double check whether has been initialized
153
181
if (isInitialized (applicationName , configurationName )) {
154
- return Mono . just ( true ) ;
182
+ return Configuration . EMPTY ;
155
183
}
156
184
return Mono .create (monoSink -> {
157
185
AwsCapaConfigurationScheduler .INSTANCE .configInitScheduler
@@ -173,16 +201,18 @@ private synchronized <T> Mono<Boolean> initConfig(String applicationName, String
173
201
LOGGER .error ("error occurs when getConfiguration,configurationName:{},version:{}" , request .configuration (), request .clientConfigurationVersion (), e );
174
202
}
175
203
if (resp != null && !Objects .equals (resp .configurationVersion (), version )) {
176
- initConfigurationItem (applicationName , configurationName , type , resp .content (), resp .configurationVersion ());
177
- monoSink .success (true );
204
+ Configuration < T > tConfiguration = initConfigurationItem (applicationName , configurationName , type , resp .content (), resp .configurationVersion ());
205
+ monoSink .success (tConfiguration );
178
206
}
179
- }, 0 , TimeUnit .SECONDS );
180
- });
207
+ });
208
+ })
209
+ .map (resp -> (Configuration <T >) resp )
210
+ .block ();
181
211
}
182
212
183
213
private <T > void initSubscribe (String applicationName , String configurationName , String group , String label , Map <String , String > metadata , TypeRef <T > type ) {
184
214
if (!isInitialized (applicationName , configurationName )) {
185
- initConfig (applicationName , configurationName , group , label , metadata , type ). block () ;
215
+ initConfig (applicationName , configurationName , group , label , metadata , type );
186
216
}
187
217
if (!isSubscribed (applicationName , configurationName )) {
188
218
createSubscribe (applicationName , configurationName , type );
@@ -194,33 +224,33 @@ private synchronized <T> void createSubscribe(String applicationName, String con
194
224
return ;
195
225
}
196
226
Flux .create (fluxSink -> {
197
- AwsCapaConfigurationScheduler .INSTANCE .configSubscribePollingScheduler
198
- .schedulePeriodically (() -> {
199
- String version = getCurVersion (applicationName , configurationName );
200
-
201
- GetConfigurationRequest request = GetConfigurationRequest .builder ()
202
- .application (applicationName )
203
- .clientId (UUID .randomUUID ().toString ())
204
- .configuration (configurationName )
205
- .clientConfigurationVersion (version )
206
- .environment (AwsCapaConfigurationProperties .AppConfigProperties .Settings .getAwsAppConfigEnv ())
207
- .build ();
208
-
209
- GetConfigurationResponse resp = null ;
210
- try {
211
- resp = appConfigAsyncClient .getConfiguration (request ).get ();
212
- } catch (InterruptedException | ExecutionException e ) {
213
- LOGGER .error ("error occurs when getConfiguration,configurationName:{},version:{}" , request .configuration (), request .clientConfigurationVersion (), e );
214
- }
215
- // update subscribed status if needs
216
- getConfiguration (applicationName , configurationName ).getSubscribed ().compareAndSet (false , true );
217
-
218
- if (resp != null && !Objects .equals (resp .configurationVersion (), version )) {
219
- fluxSink .next (resp );
220
- }
221
- // todo: make the polling frequency configurable
222
- }, 0 , 1 , TimeUnit .SECONDS );
223
- })
227
+ AwsCapaConfigurationScheduler .INSTANCE .configSubscribePollingScheduler
228
+ .schedulePeriodically (() -> {
229
+ String version = getCurVersion (applicationName , configurationName );
230
+
231
+ GetConfigurationRequest request = GetConfigurationRequest .builder ()
232
+ .application (applicationName )
233
+ .clientId (UUID .randomUUID ().toString ())
234
+ .configuration (configurationName )
235
+ .clientConfigurationVersion (version )
236
+ .environment (AwsCapaConfigurationProperties .AppConfigProperties .Settings .getAwsAppConfigEnv ())
237
+ .build ();
238
+
239
+ GetConfigurationResponse resp = null ;
240
+ try {
241
+ resp = appConfigAsyncClient .getConfiguration (request ).get ();
242
+ } catch (InterruptedException | ExecutionException e ) {
243
+ LOGGER .error ("error occurs when getConfiguration,configurationName:{},version:{}" , request .configuration (), request .clientConfigurationVersion (), e );
244
+ }
245
+ // update subscribed status if needs
246
+ getConfiguration (applicationName , configurationName ).getSubscribed ().compareAndSet (false , true );
247
+
248
+ if (resp != null && !Objects .equals (resp .configurationVersion (), version )) {
249
+ fluxSink .next (resp );
250
+ }
251
+ // todo: make the polling frequency configurable
252
+ }, 0 , 1 , TimeUnit .SECONDS );
253
+ })
224
254
.publishOn (AwsCapaConfigurationScheduler .INSTANCE .configPublisherScheduler )
225
255
.map (origin -> {
226
256
GetConfigurationResponse resp = (GetConfigurationResponse ) origin ;
@@ -235,11 +265,14 @@ private synchronized <T> void createSubscribe(String applicationName, String con
235
265
236
266
private <T > Flux <SubscribeResp <T >> doSub (String applicationName , String configurationName , String group , String label , Map <String , String > metadata , TypeRef <T > type , String appId ) {
237
267
Configuration <?> configuration = getConfiguration (applicationName , configurationName );
268
+ if (Objects .equals (configuration , Configuration .EMPTY )) {
269
+ return Flux .empty ();
270
+ }
238
271
return Flux .create (fluxSink -> {
239
- configuration .addListener (configurationItem -> {
240
- fluxSink .next (configurationItem );
241
- });
242
- })
272
+ configuration .addListener (configurationItem -> {
273
+ fluxSink .next (configurationItem );
274
+ });
275
+ })
243
276
.map (resp -> (ConfigurationItem <T >) resp )
244
277
.map (resp -> convert (resp , appId ));
245
278
}
0 commit comments