Search code examples
javatogglz

is it possible to use togglz in non web application


I am trying to find if it is possible to use togglz in non web application - like we have plain java project or java batch programs.

I tried adding the togglz library in the stand alone application and tried running it.

this is my code snippet -

import com.feature.MyFeature;

public class Test {

    public static void main(String[] args) {

        Test t = new Test();
        boolean valid=t.validate("CREATE_TEAM");
        System.out.println(valid);

    }

    public boolean validate(String feature){

        if (MyFeature.valueOf(feature).isActive()) {
        return true;
        }

        return false;
    }

}

It says -

Exception in thread "main" java.lang.IllegalStateException: Could not find the FeatureManager. For web applications please verify that the TogglzFilter starts up correctly. In other deployment scenarios you will typically have to implement a FeatureManagerProvider as described in the 'Advanced Configuration' chapter of the documentation.
    at com.amdocs.switchlite.core.context.FeatureContext.getFeatureManager(FeatureContext.java:53)
    at com.feature.MyFeature.isActive(MyFeature.java:20)
    at Test.validate(Test.java:22)
    at Test.main(Test.java:12)

Solution

  • You will have to configure Togglz correctly to make it work. In a standalone application I recommend the following setup.

    First create a FeatureManager using the FeatureManagerBuilder. Something like this:

    FeatureManager featureManager = FeatureManagerBuilder.begin()
            .featureEnum(Features.class)
            .stateRepository(new InMemoryStateRepository())
            .userProvider(new NoOpUserProvider())
            .build();
    

    The tell StaticFeatureManagerProvider about your manager:

    StaticFeatureManagerProvider.setFeatureManager(featureManager);
    

    Now StaticFeatureManagerProvider is able to tell Togglz about your FeatureManager and everything should work fine!

    Features.FOOBAR.isActive();
    // > false