Package io.unlaunch
Class OfflineUnlaunchClient
java.lang.Object
io.unlaunch.OfflineUnlaunchClient
- All Implemented Interfaces:
UnlaunchClient
,java.lang.Cloneable
public class OfflineUnlaunchClient extends java.lang.Object implements UnlaunchClient
Offline Unlaunch client which doesn't connect to the Unlauch server over network to download feature flags, nor
does it sends any events or metrics data. By default, it will return default variation for all feature flags. Users
can specify a YAML file containing feature flags and default variations and it will return those when the feature is
evaluated.
- Author:
- umermansoor
-
Constructor Summary
Constructors Constructor Description OfflineUnlaunchClient()
OfflineUnlaunchClient(java.lang.String yamlFilePath)
-
Method Summary
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, java.util.concurrent.TimeUnit unit)
Causes the current thread to wait untilUnlaunchClient
is initialized, unless the thread is interrupted, or the specified waiting time elapses.UnlaunchFeature
getFeature(java.lang.String flagKey, java.lang.String identity)
Same asUnlaunchClient.getVariation(String, String)
but returns aUnlaunchFeature
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(java.lang.String flagKey, java.lang.String identity, UnlaunchAttribute... attributes)
Same asUnlaunchClient.getFeature(String, String)
but usesattributes
that are passed as argument when evaluating the feature flag's targeting rules.java.lang.String
getVariation(java.lang.String flagKey, java.lang.String identity)
Evaluates and returns the variation (variation key) for this feature.java.lang.String
getVariation(java.lang.String flagKey, java.lang.String identity, UnlaunchAttribute... attributes)
Same asUnlaunchClient.getVariation(String, String)
but usesattributes
that are passed as argument when evaluating the feature flag's targeting rules.boolean
isReady()
Returns true is the client is intialized 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.
-
Constructor Details
-
OfflineUnlaunchClient
public OfflineUnlaunchClient() -
OfflineUnlaunchClient
public OfflineUnlaunchClient(java.lang.String yamlFilePath)
-
-
Method Details
-
accountDetails
Description copied from interface:UnlaunchClient
Returns account details for the Unlaunch project and environment the client is connected to.- Specified by:
accountDetails
in interfaceUnlaunchClient
- Returns:
AccountDetails
-
getVariation
public java.lang.String getVariation(java.lang.String flagKey, java.lang.String identity)Description copied from interface:UnlaunchClient
Evaluates and returns the variation (variation key) for this feature. Variations are defined using the Unlaunch Console at https://app.unlaunch.io.This method returns "control" if:
- The flag was not found.
- There was an exception evaluation the feature flag.
- The flag was archived.
This method doesn't throw any exceptions nor does it return
null
value- Specified by:
getVariation
in interfaceUnlaunchClient
- Parameters:
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.- Returns:
- the evaluated variation or "control" if there was an error.
-
getVariation
public java.lang.String getVariation(java.lang.String flagKey, java.lang.String identity, UnlaunchAttribute... attributes)Description copied from interface:UnlaunchClient
Same asUnlaunchClient.getVariation(String, String)
but usesattributes
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- Specified by:
getVariation
in interfaceUnlaunchClient
- Parameters:
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.- Returns:
- the evaluated variation or "control" if there was an error.
-
getFeature
Description copied from interface:UnlaunchClient
Same asUnlaunchClient.getVariation(String, String)
but returns aUnlaunchFeature
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);
- Specified by:
getFeature
in interfaceUnlaunchClient
- Parameters:
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.- Returns:
UnlaunchFeature
object that contains evaluated variation key, configuration and evaluation reason.
-
getFeature
public UnlaunchFeature getFeature(java.lang.String flagKey, java.lang.String identity, UnlaunchAttribute... attributes)Description copied from interface:UnlaunchClient
Same asUnlaunchClient.getFeature(String, String)
but usesattributes
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.- Specified by:
getFeature
in interfaceUnlaunchClient
- Parameters:
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 against- Returns:
UnlaunchFeature
object that contains evaluated variation key, configuration and evaluation reason.
-
awaitUntilReady
public void awaitUntilReady(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.TimeoutExceptionDescription copied from interface:UnlaunchClient
Causes the current thread to wait untilUnlaunchClient
is initialized, unless the thread is interrupted, or the specified waiting time elapses.If the current thread:
- has its interrupted status set on entry to this method; or
- is interrupted while waiting,
then
InterruptedException
is thrown and the current thread's interrupted status is cleared.- Specified by:
awaitUntilReady
in interfaceUnlaunchClient
- Parameters:
timeout
- the maximum time to waitunit
- the time unit of the timeout argument- Throws:
java.lang.InterruptedException
- if the current thread is interrupted while waitingjava.util.concurrent.TimeoutException
- if it times out before initialization is complete
-
isReady
public boolean isReady()Description copied from interface:UnlaunchClient
Returns true is the client is intialized and ready. False otherwise.Initialized means that the client was able to download flags and data from the Unlaunch server at leasr once.
- Specified by:
isReady
in interfaceUnlaunchClient
- Returns:
-
shutdown
public void shutdown()Description copied from interface:UnlaunchClient
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.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.)
- Specified by:
shutdown
in interfaceUnlaunchClient
-