Top Description Fields Constructors Methods
org.springframework.context.event

public Class SimpleApplicationEventMulticaster

extends AbstractApplicationEventMulticaster
Class Inheritance
Imports
java.util.concurrent.Executor, .RejectedExecutionException, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.factory.BeanFactory, org.springframework.context.ApplicationEvent, .ApplicationListener, .PayloadApplicationEvent, org.springframework.core.ResolvableType, org.springframework.lang.Nullable, org.springframework.util.ErrorHandler

Simple implementation of the 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.

Authors
Rod Johnson, Juergen Hoeller, Stephane Nicoll, Brian Clozel
See Also
setTaskExecutor

Field Summary

Modifier and TypeField and Description
private ErrorHandler
private volatile Log
private Executor
Inherited from org.springframework.context.event.AbstractApplicationEventMulticaster:
retrieverCache

Constructor Summary

AccessConstructor and Description
public
SimpleApplicationEventMulticaster()

Create a new SimpleApplicationEventMulticaster.

public
SimpleApplicationEventMulticaster(BeanFactory beanFactory)

Create a new SimpleApplicationEventMulticaster for the given BeanFactory.

Method Summary

Modifier and TypeMethod and Description
private void
protected ErrorHandler
getErrorHandler()

Return the current error handler for this multicaster.

protected Executor
getTaskExecutor()

Return the current task executor for this multicaster.

protected void
invokeListener(ApplicationListener<?>
the ApplicationListener to invoke
listener
,
ApplicationEvent
the current event to propagate
event
)

Invoke the given listener with the given event.

private boolean
matchesClassCastMessage(String classCastMessage, Class<?> eventClass)

public void
multicastEvent(ApplicationEvent
the event to multicast
event
)

Implements org.springframework.context.event.ApplicationEventMulticaster.multicastEvent.

Multicast the given application event to appropriate listeners.
public void
multicastEvent(ApplicationEvent
the event to multicast
event
,
ResolvableType
the type of event (can be null)
eventType
)

Implements org.springframework.context.event.ApplicationEventMulticaster.multicastEvent.

Multicast the given application event to appropriate listeners.
public void
setErrorHandler(ErrorHandler errorHandler)

Set the ErrorHandler to invoke in case an exception is thrown from a listener.

public void
setTaskExecutor(Executor taskExecutor)

Set a custom executor (typically a org.springframework.core.task.TaskExecutor) to invoke each listener with.

Inherited from org.springframework.context.event.AbstractApplicationEventMulticaster:
addApplicationListeneraddApplicationListenerBeangetApplicationListenersgetApplicationListenersremoveAllListenersremoveApplicationListenerremoveApplicationListenerBeanremoveApplicationListenerBeansremoveApplicationListenerssetBeanClassLoadersetBeanFactorysupportsEventsupportsEvent

Field Detail

errorHandlerback to summary
private ErrorHandler errorHandler
Annotations
@Nullable
lazyLoggerback to summary
private volatile Log lazyLogger
Annotations
@Nullable
taskExecutorback to summary
private Executor taskExecutor
Annotations
@Nullable

Constructor Detail

SimpleApplicationEventMulticasterback to summary
public SimpleApplicationEventMulticaster()

Create a new SimpleApplicationEventMulticaster.

SimpleApplicationEventMulticasterback to summary
public SimpleApplicationEventMulticaster(BeanFactory beanFactory)

Create a new SimpleApplicationEventMulticaster for the given BeanFactory.

Method Detail

doInvokeListenerback to summary
private void doInvokeListener(ApplicationListener<E extends ApplicationEvent> listener, ApplicationEvent event)
Annotations
@SuppressWarnings:rawtypes, unchecked
getErrorHandlerback to summary
protected ErrorHandler getErrorHandler()

Return the current error handler for this multicaster.

Annotations
@Nullable
Since
4.1
getTaskExecutorback to summary
protected Executor getTaskExecutor()

Return the current task executor for this multicaster.

Annotations
@Nullable
Since
2.0
invokeListenerback to summary
protected void invokeListener(ApplicationListener<?> listener, ApplicationEvent event)

Invoke the given listener with the given event.

Parameters
listener:ApplicationListener<?>

the ApplicationListener to invoke

event:ApplicationEvent

the current event to propagate

Since
4.1
matchesClassCastMessageback to summary
private boolean matchesClassCastMessage(String classCastMessage, Class<?> eventClass)
multicastEventback to summary
public void multicastEvent(ApplicationEvent event)

Implements org.springframework.context.event.ApplicationEventMulticaster.multicastEvent.

Doc from org.springframework.context.event.ApplicationEventMulticaster.multicastEvent.

Multicast the given application event to appropriate listeners.

Consider using multicastEvent(ApplicationEvent, ResolvableType) if possible as it provides better support for generics-based events.

If a matching ApplicationListener does not support asynchronous execution, it must be run within the calling thread of this multicast call.

Parameters
event:ApplicationEvent

the event to multicast

Annotations
@Override
multicastEventback to summary
public void multicastEvent(ApplicationEvent event, ResolvableType eventType)

Implements org.springframework.context.event.ApplicationEventMulticaster.multicastEvent.

Doc from org.springframework.context.event.ApplicationEventMulticaster.multicastEvent.

Multicast the given application event to appropriate listeners.

If the eventType is null, a default type is built based on the event instance.

If a matching ApplicationListener does not support asynchronous execution, it must be run within the calling thread of this multicast call.

Parameters
event:ApplicationEvent

the event to multicast

eventType:ResolvableType

the type of event (can be null)

Annotations
@Override
setErrorHandlerback to summary
public void setErrorHandler(ErrorHandler errorHandler)

Set the 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 org.springframework.scheduling.support.TaskUtils#LOG_AND_SUPPRESS_ERROR_HANDLER) or an implementation that logs exceptions while nevertheless propagating them (e.g. org.springframework.scheduling.support.TaskUtils#LOG_AND_PROPAGATE_ERROR_HANDLER).

Since
4.1
setTaskExecutorback to summary
public void setTaskExecutor(Executor taskExecutor)

Set a custom executor (typically a org.springframework.core.task.TaskExecutor) to invoke each listener with.

Default is equivalent to org.springframework.core.task.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.

ApplicationListener instances which declare no support for asynchronous execution (ApplicationListener#supportsAsyncExecution() always run within the original thread which published the event, e.g. the transaction-synchronized org.springframework.transaction.event.TransactionalApplicationListener.

Since
2.0
See Also
org.springframework.core.task.SyncTaskExecutor, org.springframework.core.task.SimpleAsyncTaskExecutor