Search in sources :

Example 1 with PushEventConsumer

use of org.osgi.util.pushstream.PushEventConsumer in project aries by apache.

the class AbstractPushStreamImpl method merge.

@Override
public PushStream<T> merge(PushEventSource<? extends T> source) {
    AbstractPushStreamImpl<T> eventStream = new IntermediatePushStreamImpl<>(psp, defaultExecutor, scheduler, this);
    AtomicInteger count = new AtomicInteger(2);
    PushEventConsumer<T> consumer = event -> {
        try {
            if (!event.isTerminal()) {
                return eventStream.handleEvent(event);
            }
            if (count.decrementAndGet() == 0) {
                eventStream.handleEvent(event.nodata());
                return ABORT;
            }
            return CONTINUE;
        } catch (Exception e) {
            PushEvent<T> error = PushEvent.error(e);
            close(error);
            eventStream.close(event.nodata());
            return ABORT;
        }
    };
    updateNext(consumer);
    AutoCloseable second;
    try {
        second = source.open((PushEvent<? extends T> event) -> {
            return consumer.accept(event);
        });
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw new IllegalStateException("Unable to merge events as the event source could not be opened.", e);
    }
    return eventStream.onClose(() -> {
        try {
            second.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }).map(Function.identity());
}
Also used : Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) AbstractQueue(java.util.AbstractQueue) Deferred(org.osgi.util.promise.Deferred) EventType(org.osgi.util.pushstream.PushEvent.EventType) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Collector(java.util.stream.Collector) PushStreamBuilder(org.osgi.util.pushstream.PushStreamBuilder) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) Promise(org.osgi.util.promise.Promise) PushStreamProvider(org.osgi.util.pushstream.PushStreamProvider) List(java.util.List) Optional(java.util.Optional) Queue(java.util.Queue) ConcurrentModificationException(java.util.ConcurrentModificationException) PushEvent(org.osgi.util.pushstream.PushEvent) LongAdder(java.util.concurrent.atomic.LongAdder) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PushStream(org.osgi.util.pushstream.PushStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) PushEventSource(org.osgi.util.pushstream.PushEventSource) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) IntSupplier(java.util.function.IntSupplier) IntFunction(java.util.function.IntFunction) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Executor(java.util.concurrent.Executor) Semaphore(java.util.concurrent.Semaphore) State(org.apache.aries.pushstream.AbstractPushStreamImpl.State) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Lock(java.util.concurrent.locks.Lock) PushEventConsumer(org.osgi.util.pushstream.PushEventConsumer) Comparator(java.util.Comparator) Collections(java.util.Collections) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentModificationException(java.util.ConcurrentModificationException) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with PushEventConsumer

use of org.osgi.util.pushstream.PushEventConsumer in project aries by apache.

the class AbstractPushStreamImpl method merge.

@Override
public PushStream<T> merge(PushStream<? extends T> source) {
    AbstractPushStreamImpl<T> eventStream = new IntermediatePushStreamImpl<>(psp, defaultExecutor, scheduler, this);
    AtomicInteger count = new AtomicInteger(2);
    PushEventConsumer<T> consumer = event -> {
        try {
            if (!event.isTerminal()) {
                return eventStream.handleEvent(event);
            }
            if (count.decrementAndGet() == 0) {
                eventStream.handleEvent(event.nodata());
                return ABORT;
            }
            return CONTINUE;
        } catch (Exception e) {
            PushEvent<T> error = PushEvent.error(e);
            close(error);
            eventStream.close(event.nodata());
            return ABORT;
        }
    };
    updateNext(consumer);
    try {
        source.forEachEvent(event -> {
            return consumer.accept(event);
        }).then(p -> {
            count.decrementAndGet();
            consumer.accept(PushEvent.close());
            return null;
        }, p -> {
            count.decrementAndGet();
            consumer.accept(PushEvent.error((Exception) p.getFailure()));
        });
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw new IllegalStateException("Unable to merge events as the event source could not be opened.", e);
    }
    return eventStream.onClose(() -> {
        try {
            source.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }).map(Function.identity());
}
Also used : Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) AbstractQueue(java.util.AbstractQueue) Deferred(org.osgi.util.promise.Deferred) EventType(org.osgi.util.pushstream.PushEvent.EventType) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Collector(java.util.stream.Collector) PushStreamBuilder(org.osgi.util.pushstream.PushStreamBuilder) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) Promise(org.osgi.util.promise.Promise) PushStreamProvider(org.osgi.util.pushstream.PushStreamProvider) List(java.util.List) Optional(java.util.Optional) Queue(java.util.Queue) ConcurrentModificationException(java.util.ConcurrentModificationException) PushEvent(org.osgi.util.pushstream.PushEvent) LongAdder(java.util.concurrent.atomic.LongAdder) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PushStream(org.osgi.util.pushstream.PushStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) PushEventSource(org.osgi.util.pushstream.PushEventSource) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) IntSupplier(java.util.function.IntSupplier) IntFunction(java.util.function.IntFunction) Iterator(java.util.Iterator) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Executor(java.util.concurrent.Executor) Semaphore(java.util.concurrent.Semaphore) State(org.apache.aries.pushstream.AbstractPushStreamImpl.State) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Lock(java.util.concurrent.locks.Lock) PushEventConsumer(org.osgi.util.pushstream.PushEventConsumer) Comparator(java.util.Comparator) Collections(java.util.Collections) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentModificationException(java.util.ConcurrentModificationException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

Duration (java.time.Duration)2 AbstractQueue (java.util.AbstractQueue)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Collections.emptyList (java.util.Collections.emptyList)2 Comparator (java.util.Comparator)2 ConcurrentModificationException (java.util.ConcurrentModificationException)2 Iterator (java.util.Iterator)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 NoSuchElementException (java.util.NoSuchElementException)2 Optional (java.util.Optional)2 Queue (java.util.Queue)2 Set (java.util.Set)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Executor (java.util.concurrent.Executor)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2