2

I'm trying to use the Togglz library which allows you to wrap your application logic and be able to toggle it ON or OFF, with some advanced strategies. I am going through the Spring Boot documentation for it, and although its quite concise, I'm finding it missing pieces of information that are not allowing me to test this properly.

reference: https://www.togglz.org/documentation/spring-boot-starter.html

  1. I am running a Spring Boot 2.4.5 version project and this documentation says to import the dependency, which I did:
<dependency>
  <groupId>org.togglz</groupId>
  <artifactId>togglz-spring-boot-starter</artifactId>
  <version>2.6.1.Final</version>
</dependency>
  1. The documentation then states you can use an auto-configuration class on your @RestController, like
@Controller
public class MyClass {
  private FeatureManager manager;

  public MyClass(FeatureManager manager) {
      this.manager = manager;
  }

  @RequestMapping("/")
  public ResponseEntity<?> index() {
      if (manager.isActive(HELLO_WORLD)) {
           ...
      }
  }
}

Here is already where I had some questions that I didn't see explained, first, that are passing a enum "HELLO_WORLD" as an argument to this isActive() function on the FeatureManager. I don't see how they are injecting this into the method/class. They do show how to declare the feature ENUM in the yaml, though, this is not referencing the "HELLO_WORLD" that was passed into the isActive() method mentioned previously, i.e. :

togglz:
  features:
    FOO:
      enabled: true
    BAR:
      enabled: false

Further down the documentation, they finally do reference this HELLO_WORLD enum, but I tried adding this to my application.yaml and I can't seem to figure out how they are injecting these Feature Enums into these methods:

togglz:
  enabled: true # Enable Togglz for the application.
  features: # The feature states. Only needed if feature states are stored in application properties.
    HELLO_WORLD:
      enabled: true

The documentation does explain how to create an enum class for these features, but they explictly list it as an alternative to defining it in the yaml file

public enum MyFeatures implements Feature {

    @EnabledByDefault
    @Label("First Feature")
    FEATURE_ONE,

    @Label("Second Feature")
    FEATURE_TWO;
}

@Bean
public FeatureProvider featureProvider() {
    return new EnumBasedFeatureProvider(MyFeatures.class);
}

I tried this also, and I just got more Bean exception errors when I try to run the Application, i.e.

Description:

Parameter 2 of method featureManager in org.togglz.spring.boot.autoconfigure.TogglzAutoConfiguration$FeatureManagerConfiguration required a bean of type 'org.togglz.core.user.UserProvider' that could not be found.


Action:

Consider defining a bean of type 'org.togglz.core.user.UserProvider' in your configuration.

Can anyone who has successful used this library provide input how to set a simple feature toggle ? Ultimately, I want to be able turn ON/OFF this feature while the application using a RELEASE DATE activation strategy i.e. 2021-06-30 00:00:00 such that I can have the toggle activate based on a datetime.

reference: https://www.togglz.org/documentation/activation-strategies.html

Can this be done in the yaml?

ennth
  • 1,698
  • 5
  • 31
  • 63

2 Answers2

1

If you don't want to use an Enum you will have to inject the FeatureProvider that was auto-configured and call featureProvider.getFeatures() to get hold of all available features. You can then check their state with the FeatureManager. I agree that this is not obvious from the documentation.

It should be possible to configure the activation strategy via your application.yml too. See the example section "Application Properties" at the very end of https://www.togglz.org/documentation/spring-boot-starter.html. It should look like this:

togglz.features.FOO:
  enabled: true
  strategy: release-date
  param:
    date: ..
    time: ..

Holgzn
  • 449
  • 4
  • 10
0

I have Implemented the library successfully only change required in the above mentioned code or for the exception required a bean of type org.togglz.core.user.UserProvider that could not be found is to add one more bean UserProvider.

@Bean
public UserProvider getUserProvider() {
    return new ServletUserProvider("admin");
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
viveksuggu
  • 737
  • 2
  • 7
  • 12