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

com.netflix.eureka2.service.AbstractServiceChannel Maven / Gradle / Ivy

There is a newer version: 2.0.0-DP4
Show newest version
package com.netflix.eureka2.service;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.subjects.ReplaySubject;
import rx.subjects.Subject;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author Nitesh Kant
 */
public abstract class AbstractServiceChannel implements ServiceChannel {

    protected static final IllegalStateException CHANNEL_CLOSED_EXCEPTION = new IllegalStateException("Channel is already closed.");

    protected final Subject lifecycle;
    protected final AtomicReference state;

    public AbstractServiceChannel(STATE initState) {
        state = new AtomicReference(initState);
        lifecycle = ReplaySubject.create(); // Since its of type void there isn't any caching of data.
        // Its just the terminal state that is cached.
    }

    @Override
    public Observable asLifecycleObservable() {
        return lifecycle;
    }

    @Override
    public final void close() {
        _close();
        lifecycle.onCompleted();
    }

    protected abstract void _close();

    protected  void connectInputToLifecycle(Observable inputObservable, final Action1 onNext) {
        inputObservable.subscribe(new Subscriber() {
            @Override
            public void onCompleted() {
                close();
                lifecycle.onCompleted();
            }

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

            @Override
            public void onNext(T message) {
                onNext.call(message);
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy