Class SimpleApplicationEventMulticaster
- All Implemented Interfaces:
Aware, BeanClassLoaderAware, BeanFactoryAware, ApplicationEventMulticaster
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.
- Author:
- Rod Johnson, Juergen Hoeller, Stephane Nicoll, Brian Clozel
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a new SimpleApplicationEventMulticaster.SimpleApplicationEventMulticaster(BeanFactory beanFactory) Create a new SimpleApplicationEventMulticaster for the given BeanFactory. -
Method Summary
Modifier and TypeMethodDescriptionprotected @Nullable ErrorHandlerReturn the current error handler for this multicaster.Return the current task executor for this multicaster.protected voidinvokeListener(ApplicationListener<?> listener, ApplicationEvent event) Invoke the given listener with the given event.voidmulticastEvent(ApplicationEvent event) Multicast the given application event to appropriate listeners.voidmulticastEvent(ApplicationEvent event, @Nullable ResolvableType eventType) Multicast the given application event to appropriate listeners.voidsetErrorHandler(@Nullable ErrorHandler errorHandler) Set theErrorHandlerto invoke in case an exception is thrown from a listener.voidsetTaskExecutor(@Nullable Executor taskExecutor) Set a custom executor (typically aTaskExecutor) to invoke each listener with.Methods inherited from class AbstractApplicationEventMulticaster
addApplicationListener, addApplicationListenerBean, getApplicationListeners, getApplicationListeners, removeAllListeners, removeApplicationListener, removeApplicationListenerBean, removeApplicationListenerBeans, removeApplicationListeners, setBeanClassLoader, setBeanFactory, supportsEvent, supportsEvent
-
Constructor Details
-
SimpleApplicationEventMulticaster
public SimpleApplicationEventMulticaster()Create a new SimpleApplicationEventMulticaster. -
SimpleApplicationEventMulticaster
Create a new SimpleApplicationEventMulticaster for the given BeanFactory.
-
-
Method Details
-
setTaskExecutor
Set a custom executor (typically aTaskExecutor) 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 context) unless the TaskExecutor explicitly supports this.
ApplicationListenerinstances which declare no support for asynchronous execution (ApplicationListener.supportsAsyncExecution()always run within the original thread which published the event, for example, the transaction-synchronizedTransactionalApplicationListener.- Since:
- 2.0
- See Also:
-
getTaskExecutor
-
setErrorHandler
Set theErrorHandlerto 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
ErrorHandlerimplementation that catches and logs exceptions (a laTaskUtils.LOG_AND_SUPPRESS_ERROR_HANDLER) or an implementation that logs exceptions while nevertheless propagating them (for example,TaskUtils.LOG_AND_PROPAGATE_ERROR_HANDLER).- Since:
- 4.1
-
getErrorHandler
Return the current error handler for this multicaster.- Since:
- 4.1
-
multicastEvent
Description copied from interface:ApplicationEventMulticasterMulticast the given application event to appropriate listeners.Consider using
ApplicationEventMulticaster.multicastEvent(ApplicationEvent, ResolvableType)if possible as it provides better support for generics-based events.If a matching
ApplicationListenerdoes not support asynchronous execution, it must be run within the calling thread of this multicast call.- Parameters:
event- the event to multicast- See Also:
-
multicastEvent
Description copied from interface:ApplicationEventMulticasterMulticast the given application event to appropriate listeners.If the
eventTypeisnull, a default type is built based on theeventinstance.If a matching
ApplicationListenerdoes not support asynchronous execution, it must be run within the calling thread of this multicast call.- Parameters:
event- the event to multicasteventType- the type of event (can benull)- See Also:
-
invokeListener
Invoke the given listener with the given event.- Parameters:
listener- the ApplicationListener to invokeevent- the current event to propagate- Since:
- 4.1
-