{{announcement.body}}
{{announcement.title}}

Togglz With Spring Boot Integration

DZone 's Guide to

Togglz With Spring Boot Integration

Togglz is an implementation of the feature toggles pattern for Java. In the article, we will see how to integrate togglz into a spring boot project.

· Integration Zone ·
Free Resource

In the disruptive world, business wants to experiment a feature for a time period in production, operational team inspect a feature with resource consumption, developers want to be up to date with trunk branch while their development spans for a long time. How all this happen and still application/product works without any hassles?

It can be solved by toggle points protected and accessed on demand which is called feature flags. To know more about, refer RefCard. Instead of many hanging custom made code, if we have library which can organize all the flags horizontally across the software it will be easy. So Togglz does that!

Introduction

Togglz is an implementation of the Feature Toggles pattern for Java. It is easy to maintain the feature flags in agile development of continuous delivery practices. All the feature states are configured on one location and feature flags incorporated on the implemented places. State of a feature flags can be activated (enabled) or inactive(disabled) by a properties. It can also be done by activation strategy, like when this condition meets, activate the flag.

Togglz ships with the following default strategies:

The strategies are easily understandable from their names, if any details required, it hyperlinks to the actual togglz documentation. We can develop our own custom strategies for activation by implementing the interface ActivationStrategy. Togglz does the service loader mechanism for activation. To do that, we need to register implementation by creating the file called META-INF/services/org.togglz.core.spi.ActivationStrategy which tells the fully qualified implementation class.

Feature State repositories can be maintained in-memory, properties file based, jdbc for rdms databases, monogo databases. We can also add caching state for these repositories. One more thing, togglz requires user information to toggle based on users, so user provider to be configured with Togglz. 

Going through a enum based feature implementation with in-memory state repository in a spring boot project. Below libraries to be integrated in the pom.xml. Togglz admin console is a web application to manage the feature flags which has been at the last.

XML
 




x


 
1
        <dependency>
2
            <groupId>org.togglz</groupId>
3
            <artifactId>togglz-spring-boot-starter</artifactId>
4
            <version>2.6.1.Final</version>
5
        </dependency>
6
        <dependency>
7
            <groupId>org.togglz</groupId>
8
            <artifactId>togglz-spring-web</artifactId>
9
            <version>2.6.1.Final</version>
10
        </dependency>
11
        <dependency>
12
            <groupId>org.togglz</groupId>
13
            <artifactId>togglz-spring-security</artifactId>
14
            <version>2.6.1.Final</version>
15
        </dependency>
16
        <!-- Togglz Admin Console -->
17
        <dependency>
18
            <groupId>org.togglz</groupId>
19
            <artifactId>togglz-console</artifactId>
20
            <version>2.6.1.Final</version>
21
        </dependency>
22
        



Now declare the enum based feature list class. We can give the label which will appear in the admin console and annotated with @EnabledByDefault to enable it at the time of startup. isActive() helper method used to check from the feature context whether feature is active or not.

Java
 




xxxxxxxxxx
1
14


 
1
public enum FeatureOptions implements  Feature {
2
 
          
3
    @EnabledByDefault
4
    @Label("First Feature")
5
    FEATURE_ONE,
6
 
          
7
    @EnabledByDefault
8
    @Label("Greetings Feature")
9
    GREETINGSFEATURE;
10
 
          
11
    public boolean isActive() {
12
        return FeatureContext.getFeatureManager().isActive(this);
13
    }
14
}



Configure the togglz for in memory state repository and inject the Spring security user provider.

Java
 




xxxxxxxxxx
1
15


1
@Configuration
2
public class TogglzConfiguration implements TogglzConfig {
3
 
          
4
    public Class<? extends Feature> getFeatureClass() {
5
        return FeatureOptions.class;
6
    }
7
 
          
8
    public StateRepository getStateRepository() {
9
        return new InMemoryStateRepository();
10
    }
11
 
          
12
    public UserProvider getUserProvider() {
13
        return new SpringSecurityUserProvider("admin");
14
    }
15
}



To inject the feature provider in the spring beans, the following bean needs to be created

Java
 




xxxxxxxxxx
1


 
1
    @Bean
2
    public FeatureProvider featureProvider() {
3
        return new EnumBasedFeatureProvider(FeatureOptions.class);
4
    }



While implementing the rest api, we can add the flag check with the enum helper method to check the feature is active or not. It can also be done by injecting the feature manager to the class which has the provision to check 

Java
 






Now when we hit the api, we can see the response. Its because the feature is enabled by default.

Admin console shows all the feature with the labels provided in the enum class. It also show the settings to dynamically change the activation strategy. 

By clicking on the settings, it provides the list of options available. 

Working source code is available in github.

Topics:
feature flags, springboot, togglz

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}