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

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

The newest version!
package com.terheyden.event;

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

import org.slf4j.Logger;

import static org.slf4j.LoggerFactory.getLogger;

/**
 * Handles incoming "sendEventToSubscribers my event" requests.
 * Publishing is always done asynchronously using a thread pool,
 * so thread death is automatically taken care of.
 */
class ReceivedEventHandler {

    private static final Logger LOG = getLogger(ReceivedEventHandler.class);

    private final ThreadPoolExecutor eventRequestExecutor;

    ReceivedEventHandler(ThreadPoolExecutor eventRequestExecutor) {
        this.eventRequestExecutor = eventRequestExecutor;
    }

    /**
     * Our main entry point — the user calls EventRouter.sendEventToSubscribers() and EventRouter calls this.
     */
    void publish(PublishRequest publishRequest) {
        eventRequestExecutor.execute(() -> processPublishRequest(publishRequest));
    }

    /**
     * Deliver the given sendEventToSubscribers request to subscribers.
     */
    private static  void processPublishRequest(PublishRequest publishRequest) {

        Collection subscribers = publishRequest.subscribers();

        if (subscribers.isEmpty()) {
            LOG.trace("No subscribers for event: {}", publishRequest);
            return;
        }

        EventRequest eventRequest = publishRequest.eventRequest();
        SendEventStrategy sendStrategy = publishRequest.eventPublisher();
        LOG.trace("Dispatching event: {}", publishRequest);
        sendStrategy.sendEventToSubscribers(eventRequest, subscribers);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy