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

io.lettuce.core.masterreplica.ResumeAfter Maven / Gradle / Ivy

Go to download

Advanced and thread-safe Java Redis client for synchronous, asynchronous, and reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs and much more.

The newest version!
package io.lettuce.core.masterreplica;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

import reactor.core.publisher.Mono;
import io.lettuce.core.api.AsyncCloseable;

/**
 * Utility to resume a {@link org.reactivestreams.Publisher} after termination.
 *
 * @author Mark Paluch
 */
class ResumeAfter {

    private static final AtomicIntegerFieldUpdater UPDATER = AtomicIntegerFieldUpdater
            .newUpdater(ResumeAfter.class, "closed");

    private final AsyncCloseable closeable;

    private static final int ST_OPEN = 0;

    private static final int ST_CLOSED = 1;

    @SuppressWarnings("unused")
    private volatile int closed = ST_OPEN;

    private ResumeAfter(AsyncCloseable closeable) {
        this.closeable = closeable;
    }

    public static ResumeAfter close(AsyncCloseable closeable) {
        return new ResumeAfter(closeable);
    }

    public  Mono thenEmit(T value) {

        return Mono.defer(() -> {

            if (firstCloseLatch()) {
                return Mono.fromCompletionStage(closeable.closeAsync());
            }

            return Mono.empty();

        }).then(Mono.just(value)).doFinally(s -> {

            if (firstCloseLatch()) {
                closeable.closeAsync();
            }
        });
    }

    public  Mono thenError(Throwable t) {

        return Mono.defer(() -> {

            if (firstCloseLatch()) {
                return Mono.fromCompletionStage(closeable.closeAsync());
            }

            return Mono.empty();

        }).then(Mono. error(t)).doFinally(s -> {

            if (firstCloseLatch()) {
                closeable.closeAsync();
            }
        });
    }

    private boolean firstCloseLatch() {
        return UPDATER.compareAndSet(ResumeAfter.this, ST_OPEN, ST_CLOSED);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy