com.configcat.ConfigCatHooks Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of configcat-java-client Show documentation
Show all versions of configcat-java-client Show documentation
Java SDK for ConfigCat, a feature flag, feature toggle, and configuration management service. That lets you launch new features and change your software configuration remotely without actually (re)deploying code. ConfigCat even helps you do controlled roll-outs like canary releases and blue-green deployments.
The newest version!
package com.configcat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;
public class ConfigCatHooks {
private final AtomicReference clientCacheState = new AtomicReference<>(null);
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
private final List>> onConfigChanged = new ArrayList<>();
private final List> onClientReadyWithState = new ArrayList<>();
private final List onClientReady = new ArrayList<>();
private final List>> onFlagEvaluated = new ArrayList<>();
private final List> onError = new ArrayList<>();
/**
* Subscribes to the onReady event. This event is fired when the SDK reaches the ready state.
* If the SDK is configured with lazy load or manual polling it's considered ready right after instantiation.
* In case of auto polling, the ready state is reached when the SDK has a valid config.json loaded
* into memory either from cache or from HTTP. If the config couldn't be loaded neither from cache nor from HTTP the
* onReady event fires when the auto polling's maxInitWaitTimeInSeconds is reached.
*
* @param callback the method to call when the event fires.
*/
public void addOnClientReady(Consumer callback) {
lock.writeLock().lock();
try {
if(clientCacheState.get() != null) {
callback.accept(clientCacheState.get());
} else {
this.onClientReadyWithState.add(callback);
}
} finally {
lock.writeLock().unlock();
}
}
/**
* Subscribes to the onReady event. This event is fired when the SDK reaches the ready state.
* If the SDK is configured with lazy load or manual polling it's considered ready right after instantiation.
* In case of auto polling, the ready state is reached when the SDK has a valid config.json loaded
* into memory either from cache or from HTTP. If the config couldn't be loaded neither from cache nor from HTTP the
* onReady event fires when the auto polling's maxInitWaitTimeInSeconds is reached.
*
* @param callback the method to call when the event fires.
*/
@Deprecated
public void addOnClientReady(Runnable callback) {
lock.writeLock().lock();
try {
this.onClientReady.add(callback);
} finally {
lock.writeLock().unlock();
}
}
/**
* Subscribes to the onConfigChanged event. This event is fired when the SDK loads a valid config.json
* into memory from cache, and each subsequent time when the loaded config.json changes via HTTP.
*
* @param callback the method to call when the event fires.
*/
public void addOnConfigChanged(Consumer