The EventBusBuilder class configures various aspects of EventBus. For example, here’s how to build an EventBus that keeps quiet in case a posted event has no subscribers:
1 2 3 4 | EventBus eventBus = EventBus.builder() .logNoSubscriberMessages(false) .sendNoSubscriberEvent(false) .build(); |
Another example is to fail when a subscriber throws an exception:
1 | EventBus eventBus = EventBus.builder().throwSubscriberException(true).build(); |
Note: by default, EventBus catches exceptions thrown from subscriber methods and sends a SubscriberExceptionEvent that may, but does not have to, be handled.
Check the EventBusBuilder class and its JavaDoc for all possible configuration possibilities.
Configure the default EventBus instance
Using EventBus.getDefault() is a simple way to get a shared EventBus instance from anywhere in your app. EventBusBuilder also allows to configure this default instance using the method installDefaultEventBus().
For example, it’s possible to configure the default EventBus instance to rethrow exceptions, which occurred in subscriber methods. But let’s to this only for DEBUG builds, because this will likely crash the app on exceptions:
1 | EventBus.builder().throwSubscriberException(BuildConfig.DEBUG).installDefaultEventBus(); |
Note: this can be done only once before the default EventBus instance is used the first time. Subsequent calls to installDefaultEventBus() will throw an exception. This ensures consistent behavior in your app. Your Application class is a good place to configure the default EventBus instance before its used.