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

com.salesforce.reactivegrpc.common.ReactiveProducerConsumerStreamObserver Maven / Gradle / Ivy

/*
 *  Copyright (c) 2017, salesforce.com, inc.
 *  All rights reserved.
 *  Licensed under the BSD 3-Clause license.
 *  For full license text, see LICENSE.txt file in the repo root  or https://opensource.org/licenses/BSD-3-Clause
 */

package com.salesforce.reactivegrpc.common;

import com.google.common.base.Preconditions;
import io.grpc.stub.ClientCallStreamObserver;
import org.reactivestreams.Publisher;

/**
 * ReactorProducerConsumerStreamObserver configures client-side manual flow control for when the client is both producing
 * and consuming streams of messages.
 *
 * @param 
 * @param 
 */
public abstract class ReactiveProducerConsumerStreamObserver extends ReactiveConsumerStreamObserver {
    private Publisher rxProducer;
    private ReactivePublisherBackpressureOnReadyHandlerClient onReadyHandler;

    public ReactiveProducerConsumerStreamObserver(Publisher rxProducer) {
        this.rxProducer = rxProducer;
    }

    @Override
    public void beforeStart(ClientCallStreamObserver requestStream) {
        super.beforeStart(Preconditions.checkNotNull(requestStream));
        onReadyHandler = new ReactivePublisherBackpressureOnReadyHandlerClient(requestStream);
    }

    public void rxSubscribe() {
        rxProducer.subscribe(onReadyHandler);
    }

    @Override
    public void onError(Throwable throwable) {
        super.onError(throwable);
        // Free references for GC
        rxProducer = null;
    }

    @Override
    public void onCompleted() {
        super.onCompleted();
        // Free references for GC
        rxProducer = null;
    }

    public void cancel() {
        onReadyHandler.cancel();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy