Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Feature flags can use feature filters to enable features conditionally. To learn more about feature filters, see Enable conditional features with feature filters.
The example used in this guide is based on the Spring Boot application introduced in the feature management quickstart. Before proceeding further, complete the quickstart to create a Spring Boot application with a Beta feature flag. Once completed, you must add a custom feature filter to the Beta feature flag in your App Configuration store.
In this article, you learn how to implement a custom feature filter and use the feature filter to enable features conditionally.
Prerequisites
Implement a custom feature filter
You've added a custom feature filter named Random with a Percentage parameter for your Beta feature flag in the prerequisites. Next, you implement the feature filter to enable the Beta feature flag based on the chance defined by the Percentage parameter.
Add a
RandomFilter.javafile in the package directory of your application with the following code.import java.util.Random; import com.azure.spring.cloud.feature.management.filters.FeatureFilter; import com.azure.spring.cloud.feature.management.models.FeatureFilterEvaluationContext; import org.springframework.stereotype.Component; @Component("Random") public class RandomFilter implements FeatureFilter { @Override public boolean evaluate(FeatureFilterEvaluationContext context) { Object value = context.getParameters().get("Percentage"); int percentage = value != null ? Integer.parseInt(value.toString()) : 0; int random = new Random().nextInt(100); return random < percentage; } }You added a
RandomFilterclass that implements theFeatureFilterinterface from thespring-cloud-azure-feature-managementlibrary. TheFeatureFilterinterface has a single method namedevaluate, which is called whenever a feature flag is evaluated. Inevaluate, a feature filter enables a feature flag by returningtrue.You decorated the class with
@Component("Random")to register it as a Spring bean with the name Random, which matches the filter name you set in the Beta feature flag in Azure App Configuration.Open your main application class or controller and add code to access the Beta feature flag a few times:
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import com.azure.spring.cloud.feature.management.FeatureManager; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Bean public CommandLineRunner runner(FeatureManager featureManager) { return args -> { for (int i = 0; i < 10; i++) { System.out.println("Beta is " + featureManager.isEnabled("Beta")); } }; } }
Feature filter in action
When you run the application, the configuration provider loads the Beta feature flag from Azure App Configuration. The result of the isEnabled("Beta") method is printed to the console. Since the RandomFilter is used by the Beta feature flag and is configured to 50 percent, the result is True 50 percent of the time and False the other 50 percent of the time.
Running the application shows that the Beta feature flag is sometimes enabled and sometimes not.
Beta is true
Beta is false
Beta is true
Beta is true
Beta is true
Beta is false
Beta is false
Beta is false
Beta is true
Beta is true
Next steps
To learn more about the built-in feature filters, continue to the following documents.