use of org.apache.camel.component.reactive.streams.api.DispatchCallback in project camel by apache.
the class CamelPublisher method publish.
public void publish(StreamPayload<Exchange> data) {
// freeze the subscriptions
List<CamelSubscription> subs = new LinkedList<>(subscriptions);
DispatchCallback<Exchange> originalCallback = data.getCallback();
if (originalCallback != null && subs.size() > 0) {
// When multiple subscribers have an active subscription,
// we aknowledge the exchange once it has been delivered to every
// subscriber (or their subscription is cancelled)
AtomicInteger counter = new AtomicInteger(subs.size());
// Use just the first exception in the callback when multiple exceptions are thrown
AtomicReference<Throwable> thrown = new AtomicReference<>(null);
data = new StreamPayload<>(data.getItem(), (ex, error) -> {
thrown.compareAndSet(null, error);
if (counter.decrementAndGet() == 0) {
originalCallback.processed(ex, thrown.get());
}
});
}
if (subs.size() > 0) {
LOG.debug("Exchange published to {} subscriptions for the stream {}: {}", subs.size(), name, data.getItem());
// at least one subscriber
for (CamelSubscription sub : subs) {
sub.publish(data);
}
} else {
data.getCallback().processed(data.getItem(), new IllegalStateException("The stream has no active subscriptions"));
}
}
Aggregations