是否有可能在非web应用程序中使用togglz
问题描述:
我正试图查找是否可以在非web应用程序中使用togglz - 就像我们有普通的java项目或java批处理程序一样。是否有可能在非web应用程序中使用togglz
我尝试在独立应用程序中添加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,使其工作。在独立应用程序中,我推荐以下设置。
首先使用FeatureManagerBuilder
创建一个FeatureManager
。事情是这样的:
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
+0
它完全按照要求工作,谢谢@chkal –
我们可以看到你的pom.xml吗?什么是依赖使用?我们也可以看到你的togglz配置。 – 2017-02-17 12:00:02