我试图找到是否可以在非Web应用程序中使用togglz-就像我们有普通的Java项目或Java批处理程序一样。

我尝试将togglz库添加到独立应用程序中并尝试运行它。

这是我的代码段-

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;
    }

}


它说 -

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)

最佳答案

您必须正确配置Togglz才能使其正常工作。在独立应用程序中,我建议进行以下设置。

首先使用FeatureManager创建一个FeatureManagerBuilder。像这样:

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


告诉StaticFeatureManagerProvider您的经理:

StaticFeatureManagerProvider.setFeatureManager(featureManager);


现在,StaticFeatureManagerProvider可以将您的FeatureManager告诉Togglz,并且一切正常!

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

10-07 23:41