@@ -73,7 +73,6 @@ public class SplitFactoryImpl implements SplitFactory {
7373 private final EventClient _eventClient ;
7474 private final SyncManager _syncManager ;
7575 private final Evaluator _evaluator ;
76- private final Runnable _destroyer ;
7776 private final String _apiToken ;
7877
7978 // Caches
@@ -141,8 +140,13 @@ public SplitFactoryImpl(String apiToken, SplitClientConfig config) throws URISyn
141140 _syncManager = SyncManagerImp .build (config .streamingEnabled (), _splitSynchronizationTask , _splitFetcher , _segmentSynchronizationTaskImp , _splitCache , config .authServiceURL (), _httpclient , config .streamingServiceURL (), config .authRetryBackoffBase (), buildSSEdHttpClient (config ), _segmentCache );
142141 _syncManager .start ();
143142
144- // Destroyer
145- _destroyer = buildDestroyer (config );
143+ // DestroyOnShutDown
144+ if (config .destroyOnShutDown ()) {
145+ Runtime .getRuntime ().addShutdownHook (new Thread (() -> {
146+ // Using the full path to avoid conflicting with Thread.destroy()
147+ SplitFactoryImpl .this .destroy ();
148+ }));
149+ }
146150
147151 // Evaluator
148152 _evaluator = new EvaluatorImp (_splitCache );
@@ -168,7 +172,30 @@ public SplitManager manager() {
168172 public void destroy () {
169173 synchronized (SplitFactoryImpl .class ) {
170174 if (!isTerminated ) {
171- _destroyer .run ();
175+ Runnable destroyer = () -> {
176+ _log .info ("Shutdown called for split" );
177+ try {
178+ _segmentSynchronizationTaskImp .close ();
179+ _log .info ("Successful shutdown of segment fetchers" );
180+ _splitSynchronizationTask .close ();
181+ _log .info ("Successful shutdown of splits" );
182+ _impressionsManager .close ();
183+ _log .info ("Successful shutdown of impressions manager" );
184+ _unCachedFireAndForget .close ();
185+ _log .info ("Successful shutdown of metrics 1" );
186+ _cachedFireAndForgetMetrics .close ();
187+ _log .info ("Successful shutdown of metrics 2" );
188+ _httpclient .close ();
189+ _log .info ("Successful shutdown of httpclient" );
190+ _eventClient .close ();
191+ _log .info ("Successful shutdown of eventClient" );
192+ _syncManager .shutdown ();
193+ _log .info ("Successful shutdown of syncManager" );
194+ } catch (IOException e ) {
195+ _log .error ("We could not shutdown split" , e );
196+ }
197+ };
198+ destroyer .run ();
172199 _apiKeyCounter .remove (_apiToken );
173200 isTerminated = true ;
174201 }
@@ -306,37 +333,4 @@ private FireAndForgetMetrics buildCachedFireAndForgetMetrics(SplitClientConfig c
306333
307334 return FireAndForgetMetrics .instance (cachedMetrics , 2 , 1000 );
308335 }
309-
310- private Runnable buildDestroyer (SplitClientConfig config ) {
311- if (config .destroyOnShutDown ()) {
312- Runtime .getRuntime ().addShutdownHook (new Thread (() -> {
313- // Using the full path to avoid conflicting with Thread.destroy()
314- SplitFactoryImpl .this .destroy ();
315- }));
316- }
317-
318- return () -> {
319- _log .info ("Shutdown called for split" );
320- try {
321- _segmentSynchronizationTaskImp .close ();
322- _log .info ("Successful shutdown of segment fetchers" );
323- _splitSynchronizationTask .close ();
324- _log .info ("Successful shutdown of splits" );
325- _impressionsManager .close ();
326- _log .info ("Successful shutdown of impressions manager" );
327- _unCachedFireAndForget .close ();
328- _log .info ("Successful shutdown of metrics 1" );
329- _cachedFireAndForgetMetrics .close ();
330- _log .info ("Successful shutdown of metrics 2" );
331- _httpclient .close ();
332- _log .info ("Successful shutdown of httpclient" );
333- _eventClient .close ();
334- _log .info ("Successful shutdown of eventClient" );
335- _syncManager .shutdown ();
336- _log .info ("Successful shutdown of syncManager" );
337- } catch (IOException e ) {
338- _log .error ("We could not shutdown split" , e );
339- }
340- };
341- }
342336}
0 commit comments