SimpleApplicationEventMulticaster class
The standard ApplicationEventMulticaster: it calls the accepting listeners in order, in the publishing call or on a task executor.
Every context has one. Declare your own as an
applicationEventMulticaster bean to deliver events asynchronously
(set taskExecutor: the publisher no longer waits for the listeners)
or to keep one failing listener from stopping the others (set
errorHandler).
Listeners are called by their Ordered order. Without an
errorHandler, the first listener error stops the delivery and is
rethrown to the publisher (Spring's default); with a taskExecutor it
is left to the executor (a SimpleAsyncTaskExecutor logs it). A
listener's condition (@EventListener(condition:)) is evaluated inside
this error handling, on the executor when there is one, as in Spring.
// Listeners run later, on the event loop; their errors are logged.
@Configuration()
class EventConfig {
@Bean()
ApplicationEventMulticaster applicationEventMulticaster() =>
SimpleApplicationEventMulticaster(
taskExecutor: SimpleAsyncTaskExecutor(),
errorHandler: (error, stack) => log.error('listener', error: error),
);
}
Spring (Java):
@Bean
ApplicationEventMulticaster applicationEventMulticaster() {
var multicaster = new SimpleApplicationEventMulticaster();
multicaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
multicaster.setErrorHandler(t -> log.error("listener", t));
return multicaster;
}
- Implemented types
Constructors
- SimpleApplicationEventMulticaster({TaskExecutor? taskExecutor, void errorHandler(Object error, StackTrace stackTrace)?})
- Creates a multicaster; without arguments it is synchronous and rethrows listener errors.
Properties
-
applicationListeners
→ List<
GenericApplicationListener> -
The listeners sorted by order; equal orders keep the order in which
they were added.
no setteroverride
- errorHandler ↔ void Function(Object error, StackTrace stackTrace)?
-
Receives the error of a failing listener, and the delivery goes on
to the next one;
nullrethrows the error to the publisher. The context also sends the failures of asynchronous@EventListenermethods here.getter/setter pair - hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- taskExecutor ↔ TaskExecutor?
-
Runs each listener call, so that the publisher does not wait (Spring's
setTaskExecutor);nullcalls the listeners in the publishing call. Which listeners match is still decided when the event is published.getter/setter pair
Methods
-
addApplicationListener(
GenericApplicationListener listener) → void -
Adds
listener; adding it again moves it to the end of the listeners with the same order.override -
invokeListener(
GenericApplicationListener listener, Object event) → void -
Calls
listenerwithevent, handing an error to errorHandler when there is one. Override it to wrap each call, for example to time it or to set up a zone (Spring'sinvokeListener). -
multicastEvent(
Object event) → void -
Delivers
eventto every listener whosesupportsEventaccepts it. The context passes events already wrapped: a published object that is not anApplicationEventarrives as aPayloadApplicationEvent.override -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
removeAllListeners(
) → void -
Removes every listener; the context calls it when it closes.
override
-
removeApplicationListener(
GenericApplicationListener listener) → void -
Removes
listener; does nothing if it is not there.override -
removeApplicationListeners(
bool predicate(GenericApplicationListener listener)) → void -
Removes every listener
predicateaccepts, for example those of one bean that is being destroyed (Spring 5.3.5'sremoveApplicationListeners(Predicate)).override -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited