com.netflix.eureka2.service.AbstractServiceChannel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eureka-core Show documentation
Show all versions of eureka-core Show documentation
eureka-core developed by Netflix
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);
}
});
}
}