Features

Using Split for Quick and Easy Java Feature Flags

Patricio Echague on August 29, 2016

For developers building on the Java stack, integrating with Split is easy. We created our SDK with two main goals: simplicity and performance: so much so that it takes only three steps to get up and running, and the overhead of using Split from within your Java code only adds a few microseconds of latency.

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.

SDK Setup

Split's Java SDK can be added as a library using your favorite package manager, such as Maven or Gradle.

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.

Splitting Code

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:

Wrap Up

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.

Stay tuned for upcoming articles on getting up and running using our other SDKs, like JavaScript, Ruby or PHP.

We're Hiring!

Join the Team