Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Flips: Feature Flipping for Java

DZone's Guide to

Flips: Feature Flipping for Java

Take a look at Flips, a new feature toggling library designed to be easy to use and require minimal configuration while still being customizable.

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

Flips is an implementation of the Feature Toggles pattern for Java. Feature Toggles are a powerful technique that allows teams to modify system behavior without changing  code.

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, which works with Java 8 and Spring Core/Spring MVC/Spring Boot, 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:

Flips Features


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

Let's get started on an in-depth understanding of these features.

Getting Started

Include the necessary dependency:

<dependency>
    <groupId>com.github.feature-flip</groupId>
    <artifactId>flips-web</artifactId>
    <version>1.0.1</version>
</dependency>


Or:

<dependency>
    <groupId>com.github.feature-flip</groupId>
    <artifactId>flips-core</artifactId>
    <version>1.0.1</version>
</dependency>


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.

Usage:

@Component
class EmailSender {

    @FlipOnEnvironmentProperty(property = "feature.send.email", expectedValue = "true")
    public void sendEmail(EmailMessage emailMessage) {}
}


Feature sendEmail is enabled if the property "feature.send.email" is set to true.

@FlipOnProfiles

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

Usage:

@Component
class EmailSender {

    @FlipOnProfiles(activeProfiles = {
        "dev",
        "qa"
    })
    public void sendEmail(EmailMessage emailMessage) {}
}


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

@FlipOnDaysOfWeek

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

Usage:

@Component
class EmailSender {

    @FlipOnDaysOfWeek(daysOfWeek = {
        DayOfWeek.MONDAY,
        DayOfWeek.TUESDAY
    })
    public void sendEmail(EmailMessage emailMessage) {}
}


Feature sendEmail is enabled if the current day is either MONDAY or TUESDAY.

@FlipOnDateTime

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

Usage:

@Component
class EmailSender {

    @FlipOnDateTime(cutoffDateTimeProperty = "default.date.enabled")
    public void sendEmail(EmailMessage emailMessage) {}
}


Feature sendEmail is enabled if the current datetime is equal to or greater than the value defined by the default.date.enabled property.

@FlipOnSpringExpression

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

Usage

@Component
class EmailSender {

    @FlipOnSpringExpression(expression = "T(java.lang.Math).sqrt(4) * 100.0 < T(java.lang.Math).sqrt(4) * 10.0")
    public void sendEmail(EmailMessage emailMessage) {}
}


Feature sendEmail is enabled if the expression evaluates to TRUE.

@FlipBean

@FlipBean is used to flip the invocation of a method with another method defined in a different bean. It is most likely to be used in conjunction with @FlipOn... annotation.

Usage:

@Component
class EmailSender {

    @FlipBean(with = SendGridEmailSender.class)
    @FlipOnProfiles(activeProfiles = "DEV")
    public void sendEmail(EmailMessage emailMessage) {}
}


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

@FlipOff

@FlipOff is used to flip a feature off.

Usage:

@Component
class EmailSender {

    @FlipOff
    public void sendEmail(EmailMessage emailMessage) {}
}


Feature sendEmail is always DISABLED.

Combining annotations:

@Component
class EmailSender {

    @FlipOnProfiles(activeProfiles = "dev")
    @FlipOnDateTime(cutoffDateTimeProperty = "sendemail.feature.active.after")
    public void sendEmail(EmailMessage emailMessage) {}
}


Feature sendEmail is enabled if the current profile is "dev" AND the current datetime is equal to or greater than the datetime specified by the property sendemail.feature.active.after.

Import Flip Context Configuration

In order to bring all Flips-related annotations into effect, FlipConfiguration needs to be imported.

Usage:

@SpringBootApplication
@Import(FlipWebContextConfiguration.class)
class ApplicationConfig {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationConfig.class, args);
    }
}


Import FlipWebContextConfiguration along with other configurations, if any. 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:

@Target({
    ElementType.METHOD
})
@Retention(RetentionPolicy.RUNTIME)
@FlipOnOff(value = MyCustomCondition.class) !!Important
public @interface MyCustomAnnotation {}


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

@Component
public class MyCustomCondition implements FlipCondition {

    @Override
    public boolean evaluateCondition(FeatureContext featureContext,
        FlipAnnotationAttributes flipAnnotationAttributes) {
        return false;
    }
}


This Condition class needs to implement FlipCondition and MUST be a Spring Component. That is it!

What Does It Mean When a "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.

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.

Thanks a lot.

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.

Topics:
feature flipping ,flips ,java ,library ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}