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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Integrate Spring With Open AI
  • Secure Spring Boot Application With Keycloak
  • Spring Config Integration With a PCF Application: A Step-by-Step Guide
  • Integration Between Java and Slack With Webhooks

Trending

  • Scalable System Design: Core Concepts for Building Reliable Software
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  • Overcoming React Development Hurdles: A Guide for Developers
  • Accelerating AI Inference With TensorRT
  1. DZone
  2. Coding
  3. Frameworks
  4. Togglz With Spring Boot Integration

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.

By 
Nagappan Subramanian user avatar
Nagappan Subramanian
DZone Core CORE ·
Jul. 16, 20 · Interview
Likes (1)
Comment
Save
Tweet
Share
10.9K Views

Join the DZone community and get the full member experience.

Join For Free

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:

  • Username
  • Gradual rollout
  • Release date
  • Client IP
  • Server IP
  • ScriptEngine
  • System Properties

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
 




xxxxxxxxxx
1
18


 
1
@RestController
2
@RequestMapping(value = "/api")
3
public class GreetingsController {
4

          
5
    private FeatureManager manager;
6

          
7
    public GreetingsController(FeatureManager manager) {
8
        this.manager = manager;
9
    }
10

          
11
    @GetMapping(value = "/helloworld/{name}")
12
    public String greeter(@PathVariable("name") String name) {
13
       // Other way to check - this.manager.isActive(FeatureOptions.GREETINGSFEATURE);
14
        if (FeatureOptions.GREETINGSFEATURE.isActive()) {
15
            return "Hello " + name;
16
        }
17
        return "It is not active";
18
    }
19
}


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.

Spring Framework Spring Boot Integration

Opinions expressed by DZone contributors are their own.

Related

  • Integrate Spring With Open AI
  • Secure Spring Boot Application With Keycloak
  • Spring Config Integration With a PCF Application: A Step-by-Step Guide
  • Integration Between Java and Slack With Webhooks

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!