diff --git a/EventBus/src/org/greenrobot/eventbus/EventBus.java b/EventBus/src/org/greenrobot/eventbus/EventBus.java index 147bb5ff..7efbb090 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBus.java +++ b/EventBus/src/org/greenrobot/eventbus/EventBus.java @@ -69,6 +69,7 @@ protected PostingThreadState initialValue() { private final ExecutorService executorService; private final boolean throwSubscriberException; + private final boolean throwNoSubscriberException; private final boolean logSubscriberExceptions; private final boolean logNoSubscriberMessages; private final boolean sendSubscriberExceptionEvent; @@ -127,6 +128,7 @@ public EventBus() { sendSubscriberExceptionEvent = builder.sendSubscriberExceptionEvent; sendNoSubscriberEvent = builder.sendNoSubscriberEvent; throwSubscriberException = builder.throwSubscriberException; + throwNoSubscriberException = builder.throwNoSubscriberException; eventInheritance = builder.eventInheritance; executorService = builder.executorService; } @@ -397,6 +399,9 @@ private void postSingleEvent(Object event, PostingThreadState postingState) thro subscriptionFound = postSingleEventForEventType(event, postingState, eventClass); } if (!subscriptionFound) { + if (throwNoSubscriberException) { + throw new EventBusException("No subscribers registered for event " + eventClass); + } if (logNoSubscriberMessages) { logger.log(Level.FINE, "No subscribers registered for event " + eventClass); } diff --git a/EventBus/src/org/greenrobot/eventbus/EventBusBuilder.java b/EventBus/src/org/greenrobot/eventbus/EventBusBuilder.java index 6df346d6..99a64d4d 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBusBuilder.java +++ b/EventBus/src/org/greenrobot/eventbus/EventBusBuilder.java @@ -15,13 +15,14 @@ */ package org.greenrobot.eventbus; -import org.greenrobot.eventbus.android.AndroidComponents; -import org.greenrobot.eventbus.meta.SubscriberInfoIndex; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import org.greenrobot.eventbus.android.AndroidComponents; +import org.greenrobot.eventbus.meta.SubscriberInfoIndex; + /** * Creates EventBus instances with custom parameters and also allows to install a custom default EventBus instance. * Create a new builder using {@link EventBus#builder()}. @@ -35,6 +36,7 @@ public class EventBusBuilder { boolean sendSubscriberExceptionEvent = true; boolean sendNoSubscriberEvent = true; boolean throwSubscriberException; + boolean throwNoSubscriberException; boolean eventInheritance = true; boolean ignoreGeneratedIndex; boolean strictMethodVerification; @@ -82,6 +84,18 @@ public EventBusBuilder throwSubscriberException(boolean throwSubscriberException return this; } + /** + * Fails if no subscriber is found for a posted event (default: false). + *
+ * This is useful for critical events that must have a subscriber. For example, in startup scenarios + * where bean registration hasn't completed yet, you might want to catch configuration errors early. + * + */ + public EventBusBuilder throwNoSubscriberException(boolean throwNoSubscriberException) { + this.throwNoSubscriberException = throwNoSubscriberException; + return this; + } + /** * By default, EventBus considers the event class hierarchy (subscribers to super classes will be notified). * Switching this feature off will improve posting of events. For simple event classes extending Object directly,