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

com.ibm.cp4waiops.connectors.sdk.CloudEventStreamObserver Maven / Gradle / Ivy

There is a newer version: 2.2.8
Show newest version
package com.ibm.cp4waiops.connectors.sdk;

import java.util.concurrent.atomic.AtomicBoolean;

import io.cloudevents.v1.proto.CloudEvent;
import io.grpc.stub.StreamObserver;

/**
 * CloudEventStreamObserver wraps a StreamObserver and tracks if the stream has been closed
 */
public class CloudEventStreamObserver implements StreamObserver {
    AtomicBoolean _active;
    StreamObserver _decorated;

    public CloudEventStreamObserver(StreamObserver decorated) {
        this(decorated, new AtomicBoolean(true));
    }

    /**
     * CloudEventStreamObserver constructor
     * 
     * @param decorated
     *            the stream to wrap
     * @param flag
     *            an atomic boolean initialized to true; will be set to false when the stream stops
     */
    public CloudEventStreamObserver(StreamObserver decorated, AtomicBoolean flag) {
        _active = flag;
        _decorated = decorated;
    }

    /**
     * isActive
     * 
     * @return true if the stream is active, false if it has been closed
     */
    public boolean isActive() {
        return _active.get();
    }

    @Override
    public void onNext(CloudEvent value) {
        _decorated.onNext(value);
    }

    @Override
    public void onError(Throwable t) {
        _active.set(false);
        _decorated.onError(t);
    }

    @Override
    public void onCompleted() {
        _active.set(false);
        _decorated.onCompleted();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy