DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Flips: Feature Flipping for Java

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.

Sarthak Makhija user avatar by
Sarthak Makhija
·
Oct. 02, 17 · Tutorial
Like (6)
Save
Tweet
Share
11.14K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Java (programming language) Annotation

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Why It Is Important To Have an Ownership as a DevOps Engineer
  • How and Why You Should Start Automating DevOps
  • Writing a Modern HTTP(S) Tunnel in Rust
  • Why Open Source Is Much More Than Just a Free Tier

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: