1616import org .slf4j .LoggerFactory ;
1717
1818import java .util .*;
19- import java .util .concurrent .CompletableFuture ;
20- import java .util .concurrent .Executors ;
21- import java .util .concurrent .ScheduledExecutorService ;
22- import java .util .concurrent .TimeUnit ;
19+ import java .util .concurrent .*;
2320import java .util .stream .Collectors ;
2421
2522import static fr .maif .requests .FeatureRequest .newFeatureRequest ;
@@ -29,6 +26,7 @@ public class FetchFeatureService implements FeatureService {
2926 protected ClientConfiguration configuration ;
3027 private static final Logger LOGGER = LoggerFactory .getLogger (FetchFeatureService .class );
3128 private final Cache <String , Feature <?>> cache ;
29+ private final Optional <ScheduledExecutorService > refreshScheduler ;
3230
3331 public FetchFeatureService (ClientConfiguration configuration ) {
3432 this .configuration = configuration ;
@@ -37,12 +35,15 @@ public FetchFeatureService(ClientConfiguration configuration) {
3735
3836
3937 if (configuration .cacheConfiguration .enabled ) {
40- ScheduledExecutorService scheduler = Executors .newScheduledThreadPool (1 );
38+ var scheduler = Executors .newScheduledThreadPool (1 );
4139 scheduler .scheduleAtFixedRate (
4240 this ::refreshCache ,
4341 0 ,
4442 configuration .cacheConfiguration .refreshInterval .getSeconds (), TimeUnit .SECONDS
4543 );
44+ refreshScheduler = Optional .of (scheduler );
45+ } else {
46+ refreshScheduler = Optional .empty ();
4647 }
4748 }
4849
@@ -76,6 +77,12 @@ public ClientConfiguration configuration() {
7677 return configuration ;
7778 }
7879
80+ @ Override
81+ public CompletableFuture <Void > disconnect () {
82+ refreshScheduler .ifPresent (ExecutorService ::shutdown );
83+ return CompletableFuture .completedFuture (null );
84+ }
85+
7986 @ Override
8087 public CompletableFuture <IzanamiResult > featureValues (FeatureRequest request ) {
8188 LOGGER .debug ("Feature activation request for {}" , String .join ("," , request .features .keySet ()));
0 commit comments