Cloneable
OfflineUnlaunchClient
public interface UnlaunchClient extends Cloneable
Client for accessing Unlaunch. Provides all features for accessing and evaluating features flags setup through the Unlaunch web console i.e. https://app.unlaunch.io). UnLaunch is an enterprise-grade feature flags platform inspired by the way you ship features. Deploy your features to production more frequently and with confidence.
Sample code to get you started:
UnlaunchClient client = UnlaunchClient.create("your sdk key"); String variation = client.getVariation("flagKey", "userId123"); if (variation == "on") { System.out.println("Variation is on"); } else if (variation == "off") { System.out.println("Variation is off"); } else { System.out.println("no variation"); } client.shutdown();
This is a server-side SDK which means it will download all feature flags and associated data upon initialization and keep it in-memory. All feature flag evaluations will be done against the in-memory data store for performance. It will periodically sync up with server to download any new changes. For more information, see the Official Guide of this SDK here: https://docs.unlaunch.io/docs/sdks/java-sdk.
Creating a new client can be an expensive operation and should only be done once and shared in an application
The close() method needs to be called on the client object to clean up resources such as threads during
application shutdown. To create clients, you can use the create methods such as
create(String)
or using the static builder()
method which returns
UnlaunchClientBuilder
which can be used to customize the client.
Modifier and Type | Method | Description |
---|---|---|
AccountDetails |
accountDetails() |
Returns account details for the Unlaunch project and environment the client is connected to.
|
void |
awaitUntilReady(long timeout,
TimeUnit unit) |
Causes the current thread to wait until
UnlaunchClient is initialized, unless the thread is interrupted,
or the specified waiting time elapses. |
static UnlaunchClientBuilder |
builder() |
Create a builder that can be used to customize and create a
UnlaunchClient . |
static UnlaunchClient |
create() |
Create a
UnlaunchClient with the SDK key loaded from the UNLAUNCH_SDK_KEY environment variable. |
static UnlaunchClient |
create(String sdkKey) |
Constructs an instance of the
UnlaunchClient using the given SDK key. |
UnlaunchFeature |
getFeature(String flagKey,
String identity) |
Same as
getVariation(String, String) but returns a UnlaunchFeature object that contains the
evaluated variation (variation key) and any configuration (key, value properties or JSON) associated with the
evaluated variation, defined in the Unlaunch web console. |
UnlaunchFeature |
getFeature(String flagKey,
String identity,
UnlaunchAttribute... attributes) |
Same as
getFeature(String, String) but uses attributes that are passed as
argument when evaluating the feature flag's targeting rules. |
String |
getVariation(String flagKey,
String identity) |
Evaluates and returns the variation (variation key) for this feature.
|
String |
getVariation(String flagKey,
String identity,
UnlaunchAttribute... attributes) |
Same as
getVariation(String, String) but uses attributes that are passed as argument when
evaluating the feature flag's targeting rules. |
boolean |
isReady() |
Returns true is the client is initialized and ready.
|
void |
shutdown() |
Closes the client by cleaning up all resources such as in memory caches, etc., stops running tasks and
rejects any new requests which arrive after this method is called.
|
static UnlaunchClient create()
UnlaunchClient
with the SDK key loaded from the UNLAUNCH_SDK_KEY environment variable.UnlaunchClient
static UnlaunchClient create(String sdkKey)
UnlaunchClient
using the given SDK key.sdkKey
- - SDK key for your Unlaunch project.static UnlaunchClientBuilder builder()
UnlaunchClient
.UnlaunchClientBuilder
AccountDetails accountDetails()
AccountDetails
String getVariation(String flagKey, String identity)
This method returns "control" if:
This method doesn't throw any exceptions nor does it return null
value
flagKey
- the feature flag you want to evaluate.identity
- unique id of your user or a unique identifier such as request or session id, email, etc. It
may be null but 'Percentage rollout' will not work because it is used to determine bucketing.
If this is null and 'Percentge rollout' is enabled, we'll log exceptions in your code and also
in the Unlaunch web app.String getVariation(String flagKey, String identity, UnlaunchAttribute... attributes)
getVariation(String, String)
but uses attributes
that are passed as argument when
evaluating the feature flag's targeting rules. Targeting rules are defined on the Unlaunch web console.
To understand attributes, suppose you have defined a feature flag with targeting rules to return certain
variation based on user's country e.g.
if country is "USA" AND subscriber is true return "on" otherwise return "off"In this example, country will be subscriber are attributes that must be passed in. If the user is from USA and is subscriber, the "on" variation will be returned. Otherwise, "off".
client.getFeature( "show_bonus_pack", userId, UnlaunchAttribute.newString("country", "USA"), UnlaunchAttribute.newBoolean("sbscriber", true) );
This method doesn't throw any exceptions nor does it return null
value
flagKey
- the feature flag you want to evaluate.identity
- unique id of your user or a unique identifier such as request or session id, email, etc. It
* may be null but 'Percentage rollout' will not work because it is used to determine bucketing.
* If this is null and 'Percentge rollout' is enabled, we'll log exceptions in your code and also
* in the Unlaunch web app.attributes
- attributes to apply when evaluating target rules.UnlaunchFeature getFeature(String flagKey, String identity)
getVariation(String, String)
but returns a UnlaunchFeature
object that contains the
evaluated variation (variation key) and any configuration (key, value properties or JSON) associated with the
evaluated variation, defined in the Unlaunch web console.
For example, to get dynamic configuration associated with a variation, you can:
UnlaunchFeature feature = client.getFeature("new_login_ui", userId); String colorHexCode = feature.getVariationConfig().getString("login_button_color", "#cd5c5c"); renderButton(colorHexCode);
flagKey
- the feature flag you want to evaluate.identity
- unique id of your user or a unique identifier such as request or session id, email, etc. It
may be null but 'Percentage rollout' will not work because it is used to determine bucketing.UnlaunchFeature
object that contains evaluated variation key, configuration and evaluation reason.UnlaunchFeature getFeature(String flagKey, String identity, UnlaunchAttribute... attributes)
getFeature(String, String)
but uses attributes
that are passed as
argument when evaluating the feature flag's targeting rules. Targeting rules are defined on the Unlaunch web
console.
To understand attributes, suppose you have defined a feature flag with targeting rules to return certain
variation based on user's country e.g.
if user's country is "USA" AND device is "handheld" return "on" otherwise return "off"In this example, country will be device are attributes that must be passed in.
flagKey
- the feature flag you want to evaluate.identity
- unique id of your user or a unique identifier such as request or session id, email, etc. It
may be null but 'Percentage rollout' will not work because it is used to determine bucketing.attributes
- an array of attributes to evaluate againstUnlaunchFeature
object that contains evaluated variation key, configuration and evaluation reason.void awaitUntilReady(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException
UnlaunchClient
is initialized, unless the thread is interrupted,
or the specified waiting time elapses.
If the current thread:
then InterruptedException
is thrown and the current thread's interrupted status is cleared.
timeout
- the maximum time to waitunit
- the time unit of the timeout argumentInterruptedException
- if the current thread is interrupted while waitingTimeoutException
- if it times out before initialization is completeboolean isReady()
Returns true is the client is initialized and ready. False otherwise.
Initialized means that the client was able to download flags and data from the Unlaunch server at leasr once.
void shutdown()
The default behavior of this method is to block until everything has been shutdown or it encountered an error or timed out (usually a few seconds.)
Copyright © 2021 Unlaunch. All rights reserved.