3

我正在尝试查找是否可以在非 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)
4

1 回答 1

4

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

首先FeatureManager使用FeatureManagerBuilder. 像这样的东西:

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

告诉StaticFeatureManagerProvider你的经理:

StaticFeatureManagerProvider.setFeatureManager(featureManager);

现在StaticFeatureManagerProvider能够告诉 Togglz 你的FeatureManager一切都应该正常工作!

Features.FOOBAR.isActive();
// > false
于 2017-02-17T16:48:14.390 回答