首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Togglz特性的默认激活策略

Togglz特性的默认激活策略
EN

Stack Overflow用户
提问于 2013-08-22 12:55:31
回答 2查看 2.7K关注 0票数 3

因为Version2.0.0,Togglz提供了激活策略来提供一个特性。例如,您可以连接启用该功能的服务器IP地址列表。然而,这些策略究竟是如何与特性相关联的呢?我所看到的是,我可以在Togglz控制台中更改策略,甚至可以在数据库中手工编辑数据。

我正在寻找的是一些类似于@EnabledByDefault的默认机制。我可以实现一个自定义的状态存储库,它甚至可以查找注释,但是我怀疑这个解决方案是现成的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-23 06:21:58

分享我自己的解决方案。

默认值的注释

我定义了应该以@EnabledByDefault的方式使用的注释。以下是服务器-ip策略的一种:

代码语言:javascript
复制
/**
 * Allows to specify that the annotated feature should use
 * {@link ServerIPStrategy} if the repository doesn't have any
 * state saved.
 * 
 * @author Michael Piefel
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface UsingServerIPStrategy {

    /**
     * A comma-separated list of server IPs for which
     * the feature should be active.
     */
    String value();

}

使用注释

在特性定义中,使用如下的一个概念:

代码语言:javascript
复制
…
@EnabledByDefault
@UsingServerIPStrategy(value = "192.168.1.211")
@Label("Run regular jobs to send status e-mails to participants")
MAIL_CRON_JOBS;
…

要评估的状态库

如果已经保存了特性状态,我想从存储库中提取它。如果没有,则必须计算注释。为此,需要一个委托存储库:

代码语言:javascript
复制
/**
 * A Togglz {@link StateRepository} that looks for default strategies
 * on the defined features.
 * 
 * @author Michael Piefel
 */
@AllArgsConstructor
public class DefaultingStateRepository implements StateRepository {

    private StateRepository delegate;

    @Override
    public FeatureState getFeatureState(Feature feature) {
        FeatureState featureState = delegate.getFeatureState(feature);
        if (featureState == null) {
            // Look for a default strategy.
            // If none is defined, a null return value is good enough.
            UsingServerIPStrategy serverIPStrategy = FeatureAnnotations
                    .getAnnotation(feature, UsingServerIPStrategy.class);
            if (serverIPStrategy != null) {
                featureState = new FeatureState(feature,
                        FeatureAnnotations.isEnabledByDefault(feature));
                featureState.setStrategyId(ServerIpActivationStrategy.ID);
                featureState.setParameter(ServerIpActivationStrategy.PARAM_IPS,
                        serverIPStrategy.value());
            }
        }

        return featureState;
    }

    @Override
    public void setFeatureState(FeatureState featureState) {
        // write through
        delegate.setFeatureState(featureState);
    }
}

电线在里面吗?

最后,为了使用存储库,我将其连接到我们的TogglzConfig组件中,并将其推迟到JDBC,但也允许缓存它:

代码语言:javascript
复制
…
@Override
public StateRepository getStateRepository() {
    JDBCStateRepository jdbcStateRepository = new JDBCStateRepository(dataSource);
    DefaultingStateRepository defaultingStateRepository = new
            DefaultingStateRepository(jdbcStateRepository);
    return new CachingStateRepository(defaultingStateRepository, 60_000);
}
…
票数 5
EN

Stack Overflow用户

发布于 2019-06-10 12:52:02

@DefaultActivationStrategy可以添加到将激活策略设置为默认状态。此外,还可以将默认值设置为策略。示例:

代码语言:javascript
复制
@EnabledByDefault

@Label("UserNotification")

@DefaultActivationStrategy(
    id = UserNotificationActivationStrategy.ID,
    parameters = {
        @ActivationParameter(name = UserNotificationActivationStrategy.LICENSE_TYPE, value ="PRO")
    }
);

UserNotificationActivationStrategy是用户定义的激活策略。

参考:https://github.com/eugenp/tutorials/blob/master/spring-boot/src/main/java/com/baeldung/toggle/MyFeatures.java https://www.togglz.org/apidocs/2.3.0.final/org/togglz/core/annotation/defaultactivationstrategy

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18381103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档