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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Filtering Java Collections via Annotation-Driven Introspection
  • Mastering Spring: Synchronizing @Transactional and @Async Annotations With Various Propagation Strategies
  • Implementing Persistence With Clean Architecture
  • Composing Custom Annotations in Spring

Trending

  • Designing Frontend Applications for Failure, Not Just Success
  • The Permission Was Valid. The Action Was Not Authorized.
  • Treat Salesforce Configuration Like Code: Release Discipline in a Copado Pipeline
  • Why Vulnerability Management Fails Without Reliable Asset Data

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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.

By 
Sarthak Makhija
·
Oct. 02, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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.

Related

  • Filtering Java Collections via Annotation-Driven Introspection
  • Mastering Spring: Synchronizing @Transactional and @Async Annotations With Various Propagation Strategies
  • Implementing Persistence With Clean Architecture
  • Composing Custom Annotations in Spring

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook