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());
}
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());
}
Aggregations