Search in sources :

Example 1 with DispatchCallback

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"));
    }
}
Also used : Exchange(org.apache.camel.Exchange) CamelContext(org.apache.camel.CamelContext) ReactiveStreamsBackpressureStrategy(org.apache.camel.component.reactive.streams.ReactiveStreamsBackpressureStrategy) Logger(org.slf4j.Logger) Publisher(org.reactivestreams.Publisher) ReactiveStreamsProducer(org.apache.camel.component.reactive.streams.ReactiveStreamsProducer) LoggerFactory(org.slf4j.LoggerFactory) Exchange(org.apache.camel.Exchange) DispatchCallback(org.apache.camel.component.reactive.streams.api.DispatchCallback) AtomicReference(java.util.concurrent.atomic.AtomicReference) Objects(java.util.Objects) ReactiveStreamsEndpoint(org.apache.camel.component.reactive.streams.ReactiveStreamsEndpoint) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReactiveStreamsComponent(org.apache.camel.component.reactive.streams.ReactiveStreamsComponent) LinkedList(java.util.LinkedList) Subscriber(org.reactivestreams.Subscriber) ExecutorService(java.util.concurrent.ExecutorService) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) LinkedList(java.util.LinkedList)

Aggregations

LinkedList (java.util.LinkedList)1 List (java.util.List)1 Objects (java.util.Objects)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CamelContext (org.apache.camel.CamelContext)1 Exchange (org.apache.camel.Exchange)1 ReactiveStreamsBackpressureStrategy (org.apache.camel.component.reactive.streams.ReactiveStreamsBackpressureStrategy)1 ReactiveStreamsComponent (org.apache.camel.component.reactive.streams.ReactiveStreamsComponent)1 ReactiveStreamsEndpoint (org.apache.camel.component.reactive.streams.ReactiveStreamsEndpoint)1 ReactiveStreamsProducer (org.apache.camel.component.reactive.streams.ReactiveStreamsProducer)1 DispatchCallback (org.apache.camel.component.reactive.streams.api.DispatchCallback)1 Publisher (org.reactivestreams.Publisher)1 Subscriber (org.reactivestreams.Subscriber)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1