public class SimpleApplicationEventMulticaster extends AbstractApplicationEventMulticaster
ApplicationEventMulticaster
interface.
Multicasts all events to all registered listeners, leaving it up to
the listeners to ignore events that they are not interested in.
Listeners will usually perform corresponding instanceof
checks on the passed-in event object.
By default, all listeners are invoked in the calling thread. This allows the danger of a rogue listener blocking the entire application, but adds minimal overhead. Specify an alternative task executor to have listeners executed in different threads, for example from a thread pool.
setTaskExecutor(java.util.concurrent.Executor)
Modifier and Type | Field and Description |
---|---|
private ErrorHandler |
errorHandler |
private java.util.concurrent.Executor |
taskExecutor |
retrieverCache
Constructor and Description |
---|
SimpleApplicationEventMulticaster()
Create a new SimpleApplicationEventMulticaster.
|
SimpleApplicationEventMulticaster(BeanFactory beanFactory)
Create a new SimpleApplicationEventMulticaster for the given BeanFactory.
|
Modifier and Type | Method and Description |
---|---|
private void |
doInvokeListener(ApplicationListener listener,
ApplicationEvent event) |
protected ErrorHandler |
getErrorHandler()
Return the current error handler for this multicaster.
|
protected java.util.concurrent.Executor |
getTaskExecutor()
Return the current task executor for this multicaster.
|
protected void |
invokeListener(ApplicationListener<?> listener,
ApplicationEvent event)
Invoke the given listener with the given event.
|
void |
multicastEvent(ApplicationEvent event)
Multicast the given application event to appropriate listeners.
|
void |
multicastEvent(ApplicationEvent event,
ResolvableType eventType)
Multicast the given application event to appropriate listeners.
|
private ResolvableType |
resolveDefaultEventType(ApplicationEvent event) |
void |
setErrorHandler(ErrorHandler errorHandler)
Set the
ErrorHandler to invoke in case an exception is thrown
from a listener. |
void |
setTaskExecutor(java.util.concurrent.Executor taskExecutor)
Set a custom executor (typically a
TaskExecutor )
to invoke each listener with. |
addApplicationListener, addApplicationListenerBean, getApplicationListeners, getApplicationListeners, removeAllListeners, removeApplicationListener, removeApplicationListenerBean, setBeanClassLoader, setBeanFactory, supportsEvent, supportsEvent
@Nullable private java.util.concurrent.Executor taskExecutor
@Nullable private ErrorHandler errorHandler
public SimpleApplicationEventMulticaster()
public SimpleApplicationEventMulticaster(BeanFactory beanFactory)
public void setTaskExecutor(@Nullable java.util.concurrent.Executor taskExecutor)
TaskExecutor
)
to invoke each listener with.
Default is equivalent to SyncTaskExecutor
,
executing all listeners synchronously in the calling thread.
Consider specifying an asynchronous task executor here to not block the caller until all listeners have been executed. However, note that asynchronous execution will not participate in the caller's thread context (class loader, transaction association) unless the TaskExecutor explicitly supports this.
SyncTaskExecutor
,
SimpleAsyncTaskExecutor
@Nullable protected java.util.concurrent.Executor getTaskExecutor()
public void setErrorHandler(@Nullable ErrorHandler errorHandler)
ErrorHandler
to invoke in case an exception is thrown
from a listener.
Default is none, with a listener exception stopping the current multicast and getting propagated to the publisher of the current event. If a task executor is specified, each individual listener exception will get propagated to the executor but won't necessarily stop execution of other listeners.
Consider setting an ErrorHandler
implementation that catches
and logs exceptions (a la
TaskUtils.LOG_AND_SUPPRESS_ERROR_HANDLER
)
or an implementation that logs exceptions while nevertheless propagating them
(e.g. TaskUtils.LOG_AND_PROPAGATE_ERROR_HANDLER
).
@Nullable protected ErrorHandler getErrorHandler()
public void multicastEvent(ApplicationEvent event)
ApplicationEventMulticaster
Consider using ApplicationEventMulticaster.multicastEvent(ApplicationEvent, ResolvableType)
if possible as it provides a better support for generics-based events.
event
- the event to multicastpublic void multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType)
ApplicationEventMulticaster
If the eventType
is null
, a default type is built
based on the event
instance.
event
- the event to multicasteventType
- the type of event (can be null)private ResolvableType resolveDefaultEventType(ApplicationEvent event)
protected void invokeListener(ApplicationListener<?> listener, ApplicationEvent event)
listener
- the ApplicationListener to invokeevent
- the current event to propagateprivate void doInvokeListener(ApplicationListener listener, ApplicationEvent event)