3

バージョン 2.0.0 以降、Togglz は機能に合わせてアクティベーション戦略を提供しています。たとえば、機能を有効にするサーバー IP アドレスのリストを接続できます。しかし、これらの戦略は実際に機能にどのように関連付けられているのでしょうか? 私が見たのは、Togglz コンソールで戦略を変更したり、データベースでデータを手動で編集したりできることだけでした。

私が探していたのは、にかなり似たデフォルトのメカニズムです@EnabledByDefault。カスタムの状態リポジトリを実装でき、アノテーションを探すこともできましたが、このソリューションはすぐに使えるのではないかと思いました。

4

2 に答える 2

5

私自身の解決策を共有するだけです。

デフォルトの注釈

そのまま使用する必要があるアノテーションを定義しました@EnabledByDefault。これは server-ip 戦略の 1 つです。

/**
 * 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();

}

注釈の使用

機能定義では、次のような注釈を使用します。

…
@EnabledByDefault
@UsingServerIPStrategy(value = "192.168.1.211")
@Label("Run regular jobs to send status e-mails to participants")
MAIL_CRON_JOBS;
…

評価する状態リポジトリ

すでに保存されている場合は、リポジトリから機能の状態を取得したいと考えています。そうでない場合は、注釈を評価する必要があります。このためには、委任リポジトリが必要です。

/**
 * 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 に従いますが、キャッシュも可能にします。

…
@Override
public StateRepository getStateRepository() {
    JDBCStateRepository jdbcStateRepository = new JDBCStateRepository(dataSource);
    DefaultingStateRepository defaultingStateRepository = new
            DefaultingStateRepository(jdbcStateRepository);
    return new CachingStateRepository(defaultingStateRepository, 60_000);
}
…
于 2013-08-23T06:21:58.823 に答える