@SuppressWarnings("unchecked")
protected Observable<Void> _closeChannel() {
closeStartTimeMillis = Clock.newStartTimeMillis();
eventsSubject.onEvent(metricEventProvider.getChannelCloseStartEvent());
final ChannelFuture closeFuture = getChannel().close();
/**
* This listener if added inside the returned Observable onSubscribe() function, would mean that the
* metric events will only be fired if someone subscribed to the close() Observable. However, we need them to
* fire independent of someone subscribing.
*/
closeFuture.addListener(new ChannelFutureListener() {
@SuppressWarnings("unchecked")
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
eventsSubject.onEvent(metricEventProvider.getChannelCloseSuccessEvent(),
Clock.onEndMillis(closeStartTimeMillis));
} else {
eventsSubject.onEvent(metricEventProvider.getChannelCloseFailedEvent(),
Clock.onEndMillis(closeStartTimeMillis), future.cause());
}
}
});
return Observable.create(new Observable.OnSubscribe<Void>() {
@Override
public void call(final Subscriber<? super Void> subscriber) {
closeFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
subscriber.onCompleted();
} else {