Flips: Feature Flipping for Java

Flips

Flips is an implementation of the Feature Toggles pattern for Java and Spring (Spring Core / Spring MVC/ Spring Boot) based application.

Feature Toggle is a powerful technique that allows teams to modify system behavior and deliver new functionality to users rapidly but safely.

Why Another Library for Feature Toggles?

The idea behind Flips is to let the clients implement toggles with minimum configuration and coding.

The main motivations behind implementing this library were –

  • Should be simple to use
  • Should require minimal configuration and code
  • Should be able to flip a feature based on various conditions
  • Should be able to flip a feature based on a combination of different conditions
  • Should be possible for the clients to create custom conditions to suit their requirements

Flips works with Java 8 and Spring Core/Spring MVC/Spring Boot, and is available for web and non-web applications.

What Does Flips Offer?

Flips provides various conditions to flip a feature. The image below summarizes the features:

Any feature can be flipped ON or OFF based on different conditions which can be value of a property, current active profiles, days of the week, or a combination of these, etc.

Let’s get started with in-depth understanding of these features.

Getting Started

Include the necessary dependency:

Or:

Detailed Description of All Annotations

Flips provides various annotations to flip a feature. Let’s have a detailed walk-through of all the annotations:

@FlipOnEnvironmentProperty

@FlipOnEnvironmentProperty is used to flip a feature based on the value of an environment property.

@FlipOnProfiles

@FlipOnProfiles is used to flip a feature based on the environment in which the application is running

Feature sendEmail is enabled if the current profile (or environment) is either dev or qa.

@FlipOnDaysOfWeek

@FlipOnDaysOfWeek is used to flip a feature based on the day of the week.

Feature sendEmail is enabled if the current day is MONDAY.

@FlipOnDateTime

@FlipOnDateTime is used to flip a feature based on date and time.

Feature sendEmail is enabled if the current datetime is equal to or greater than the value (in ISO-8601 format) defined by the default.date.enabled property.

@FlipOnSpringExpression

@FlipOnSpringExpression is used to flip a feature based on the evaluation of a Spring expression.

Feature sendEmail is enabled if the expression evaluates to TRUE. This annotation happens to be one of the most powerful annotations in Flips library. Why so ?

One could always write a custom spring component and use the same in @FlipOnSpringExpression to flip a feature.

@FlipBean

@FlipBean is used to flip the invocation of a method with another method defined in a different bean.

This will flip the invocation of the sendEmail method with a method (having same signature) defined in SendGridEmailSender Spring component if the current profile is DEV.

@FlipOff

@FlipOff is used to flip a feature off.

Feature sendEmail is always DISABLED.

Combining annotations

Feature sendEmail is enabled if the current profile is “dev” AND the current day of the week is MONDAY.

Import Flip Context Configuration

In order to bring all Flips-related annotations into effect, you need to import FlipContextConfiguration or FlipWebContextConfiguration and you are ready to go.

Please refer to this sample project.

Creating Custom Annotations

All the annotations provided by the library are of type @FlipOnOff, which is essentially a meta-annotation. So, create a custom annotation annotated with @FlipOnOff at the method level:

As a part of this annotation, specify the condition that will evaluate this annotation.

This Condition class needs to implement FlipCondition and MUST be a Spring Component.

That is it! You can use your custom annotation to any method to flip it ON or OFF based on your condition.

What Does It Mean “Feature Is DISABLED”?

FeatureNotEnabledException is thrown if a disabled feature is invoked. In case of a web application, one could use flips-web dependency, which also provides a ControllerAdvice meant to handle this exception.

It returns a default response and a status code of 501, which can be overridden. Please refer to the sample project for more information.

Wrap Up

We believe the MVP is done and features like flipping at runtime and supporting database-driven feature flips are in the pipeline.

For any custom flip condition, one could go ahead and use @FlipOnSpringExpression with your custom spring bean to determine flip condition.

If you want to have a look at the code or even want to contribute, you can check out Flips. Feel free to give any feedback.

Leave a Reply

avatar
  Subscribe  
Notify of