Our Java SDK keeps the developer from needing to interact directly with our underlying API. In spite of the API being relatively simple, using a framework (SDK) written in the same language as your application's codebase is always preferred.
The following example shows how to add the Split artifact to a project using Maven for dependency management:
Once the artifact is in the classpath, it's ready to be imported and ready to be set up.
Creating a Long-lived Client
The second step to setting up the SDK is to create the long-lived Split client. You can do so by creating it manually or injecting it using your favorite dependency injection framework.
or using Guice as the injection framework:
A similar approach can be used for other dependency injection frameworks, like Spring.
After this step, the Split SDK client is ready to go and be used.
You can use Split to make decisions on what code path to take given different responses from Split, also known as treatments.
As an example, let's assume we have two types of credit card payments we support—Visa and Master Card—and are about to introduce a third—Amex.
The code would look like this:
Kill Switch and Default Treatment
Split provides the ability to kill a feature by hitting a red button for a specific test in the Split UI console. But what does that mean from the code standpoint? It means that, once killed, any subsequent call to getTeeatment will return the treatment marked in the UI as default. For the example above, if the user selected VISA as default treatment, then the branch in the if statement corresponding to VISA will be selected.
Split as On/Off Switch
Imagine the valid scenario where you want to start testing a new database like Apache Cassandra to migrate part of your existing dataset in your primary datastore. A safe approach is to do what are called dark writes, which for this exercise we'll do in a feature named double_writes_to_cassandra. This practice is known as dark writes because for each write of the data we care about, we also perform a similar write to another database to start testing its performance. But, this data isn't yet read from the DB, hence, it's "dark".
For this use-case, Split can be used as a simple on/off switch. The code would look something like this:
and of course the previous code can be rewritten as follows:
Hopefully this article sheds some light on how to get up and running with Split on Java, as well the basic concepts behind its core functionality in a couple different uses.