Class SimpleEventBus
- All Implemented Interfaces:
DescribableComponent,SubscribableEventSource,EventBus,EventSink
EventBus that provides synchronous event publication with optional
ProcessingContext integration for transactional event handling.
This event bus supports two publication modes depending on whether a ProcessingContext is provided:
- Immediate publication (context is
null): Events are published directly to all subscribers without any queueing or lifecycle management. This mode is useful for fire-and-forget event publication outside of any transactional boundary. - Deferred publication (context is provided): Events are queued and published during the
PREPARE_COMMITphase of the processing lifecycle. This ensures that events are only published if the processing context successfully reaches the commit phase, providing transactional consistency.
ProcessingContext Integration:
When events are published with a ProcessingContext, this event bus registers lifecycle hooks on the first
publication within that context:
- A
onPrepareCommithandler that publishes all queued events to subscribers - A
doFinallyhandler that cleans up the event queue to free memory
ProcessingContext are queued together and published as a single batch
during the PREPARE_COMMIT phase. If additional events are published by event handlers during this phase, they are
also processed in the same phase to ensure complete event propagation.
Publication Restrictions:
Event publication is forbidden once the ProcessingContext has been committed. Attempting to publish
events during or after the
COMMIT phase will result
in an IllegalStateException. This restriction ensures that events cannot be published after the
transactional boundary has been crossed, preventing inconsistent state.
Example usage:
// Immediate publication (no transactional context)
EventBus eventBus = new SimpleEventBus();
eventBus.publish(null, List.of(new GenericEventMessage<>(new OrderPlacedEvent())));
// Deferred publication within a UnitOfWork
UnitOfWork uow = unitOfWorkFactory.create();
uow.runOnInvocation(ctx -> {
eventBus.publish(ctx, List.of(new GenericEventMessage<>(new OrderPlacedEvent())));
// Events are not yet published - they're queued
});
uow.execute(); // Events are published during PREPARE_COMMIT phase
- Since:
- 0.5
- Author:
- Allard Buijze, Mateusz Nowak, René de Waele
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoiddescribeTo(ComponentDescriptor descriptor) Describe the properties ofthis DescribableComponentwith the givendescriptor.publish(ProcessingContext context, List<EventMessage> events) Publishes the giveneventswithin the givencontext, when present.subscribe(BiFunction<List<? extends EventMessage>, ProcessingContext, CompletableFuture<?>> eventsBatchConsumer) Subscribe the giveneventsBatchConsumerto this event source.
-
Constructor Details
-
SimpleEventBus
public SimpleEventBus()Instantiate anSimpleEventBus.
-
-
Method Details
-
subscribe
public Registration subscribe(@Nonnull BiFunction<List<? extends EventMessage>, ProcessingContext, CompletableFuture<?>> eventsBatchConsumer) Description copied from interface:SubscribableEventSourceSubscribe the giveneventsBatchConsumerto this event source. When subscribed, it will receive all events published to this source since the subscription.If the given
eventsBatchConsumeris already subscribed, nothing happens.Note on
ProcessingContext: TheProcessingContextparameter passed to the consumer may benull. Whennull, it is the responsibility of the registeredeventsBatchConsumerto create an appropriateProcessingContextas needed for processing the events.- Specified by:
subscribein interfaceSubscribableEventSource- Parameters:
eventsBatchConsumer- The event batches consumer to subscribe.- Returns:
- A
Registrationhandle to unsubscribe theeventsBatchConsumer. When unsubscribed, it will no longer receive events.
-
publish
public CompletableFuture<Void> publish(@Nullable ProcessingContext context, @Nonnull List<EventMessage> events) Description copied from interface:EventSinkPublishes the giveneventswithin the givencontext, when present.When present, the
post invocationphase is used to publish theevents. As a consequence, the resultingCompletableFuturecompletes when theeventsare staged in that phase.When no
ProcessingContextis provided, implementers of this interface may choose to create aProcessingContextwhen necessary.- Specified by:
publishin interfaceEventSink- Parameters:
context- The processing context, if any, to publish the giveneventsin.events- Theeventsto publish in this sink.- Returns:
- A
CompletableFutureofVoid. When this completes and a non-nullcontextwas given, this means theeventshave been successfully staged. When a nullcontextwas provided, successful completion of this future means theeventswhere published.
-
describeTo
Description copied from interface:DescribableComponentDescribe the properties ofthis DescribableComponentwith the givendescriptor.Components should call the appropriate
describePropertymethods on the descriptor to register their properties. The descriptor is responsible for determining how these properties are formatted and structured in the final output.Best Practices: As a general rule, all relevant fields of a
DescribableComponentimplementation should be described in this method. However, developers have discretion to include only the fields that make sense in the context. Not every field may be meaningful for description purposes, especially internal implementation details. Furthermore, components might want to expose different information based on their current state. The final decision on what properties to include lies with the person implementing thedescribeTomethod, who should focus on providing information that is useful for understanding the component's configuration and state.Example implementation:
public void describeTo(ComponentDescriptor descriptor) { descriptor.describeProperty("name", this.name); descriptor.describeProperty("enabled", this.enabled); descriptor.describeProperty("configuration", this.configuration); // A nested component descriptor.describeProperty("handlers", this.eventHandlers); // A collection }- Specified by:
describeToin interfaceDescribableComponent- Parameters:
descriptor- The component descriptor to describethis DescribableComponentn its properties in.
-