com.terheyden.event.ReceivedEventHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of event-router Show documentation
Show all versions of event-router Show documentation
Simple, fast, flexible event router / event bus for Java
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);
}
}