All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.eureka2.client.registry.swap.RegistrySwapOperator Maven / Gradle / Ivy

/*
 * Copyright 2014 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.eureka2.client.registry.swap;

import com.netflix.eureka2.client.registry.EurekaClientRegistry;
import com.netflix.eureka2.interests.ChangeNotification;
import com.netflix.eureka2.registry.InstanceInfo;
import rx.Observable.Operator;
import rx.Subscriber;
import rx.observers.Subscribers;

/**
 * @author Tomasz Bak
 */
public class RegistrySwapOperator implements Operator> {

    private static final IllegalStateException UNEXPECTED_END_OF_STREAM = new IllegalStateException("Unexpected end of interest subscription stream");

    private final EurekaClientRegistry originalRegisry;
    private final EurekaClientRegistry newRegistry;
    private final RegistrySwapStrategyFactory strategyFactory;

    public RegistrySwapOperator(EurekaClientRegistry originalRegisry,
                                EurekaClientRegistry newRegistry,
                                RegistrySwapStrategyFactory strategyFactory) {
        this.originalRegisry = originalRegisry;
        this.newRegistry = newRegistry;
        this.strategyFactory = strategyFactory;
    }

    @Override
    public Subscriber> call(final Subscriber subscriber) {
        final RegistrySwapStrategy swapStrategy = strategyFactory.newInstance();

        if (swapStrategy.isReadyToSwap(originalRegisry, newRegistry)) {
            subscriber.onCompleted();
            return Subscribers.empty();
        }

        return new Subscriber>() {
            @Override
            public void onCompleted() {
                // Interest subscription never completes under normal circumstances.
                subscriber.onError(UNEXPECTED_END_OF_STREAM);
            }

            @Override
            public void onError(Throwable e) {
                subscriber.onError(e);
            }

            @Override
            public void onNext(ChangeNotification instanceInfoChangeNotification) {
                if (swapStrategy.isReadyToSwap(originalRegisry, newRegistry)) {
                    subscriber.onCompleted();
                }
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy