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

com.terheyden.event.EventQuerySendAsyncStrategy Maven / Gradle / Ivy

The newest version!
package com.terheyden.event;

import java.util.Collection;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * If event {@code MyEvent} is published and there are 3 subscribers,
 * then 3 sendEventToSubscribers tasks are created and run on the thread pool in this publisher.
 */
class EventQuerySendAsyncStrategy extends ExceptionHandlingSendEventStrategy {

    private final ThreadPoolExecutor threadPool;

    EventQuerySendAsyncStrategy(SubscriberExceptionHandler exceptionHandler, ThreadPoolExecutor threadPool) {
        super(exceptionHandler);
        this.threadPool = threadPool;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void sendEventToSubscribers(
        EventRequest eventRequest,
        Collection subscribers) {

        subscribers
            .stream()
            .map(sub -> (EventQuerySubscription) sub)
            .forEach(sub ->
            threadPool.execute(() -> sendEventToSubscriber(sub, eventRequest)));
    }

    private void sendEventToSubscriber(EventQuerySubscription sub, EventRequest eventRequest) {
        try {
            SendStrategies.sendQueryEventResponse(eventRequest, sub);
        } catch (Exception e) {
            handleException(e, eventRequest);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy