Class SafeApplicationEventMulticaster

  • All Implemented Interfaces:
    org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware, org.springframework.context.event.ApplicationEventMulticaster

    public class SafeApplicationEventMulticaster
    extends java.lang.Object
    implements org.springframework.context.event.ApplicationEventMulticaster, org.springframework.context.ApplicationContextAware
    Abstract implementation of the ApplicationEventMulticaster interface, providing the basic listener registration facility.

    Doesn't permit multiple instances of the same listener by default, as it keeps listeners in a linked Set. The collection class used to hold ApplicationListener objects can be overridden through the "collectionClass" bean property.

    Implementing ApplicationEventMulticaster's actual multicastEvent(org.springframework.context.ApplicationEvent) method is left to subclasses. SimpleApplicationEventMulticaster simply multicasts all events to all registered listeners, invoking them in the calling thread. Alternative implementations could be more sophisticated in those respects.

    Since:
    1.2.3
    Author:
    Juergen Hoeller
    See Also:
    getApplicationListeners(ApplicationEvent), SimpleApplicationEventMulticaster
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addApplicationListener​(org.springframework.context.ApplicationListener listener)  
      void addApplicationListenerBean​(java.lang.String listenerBeanName)  
      protected java.util.Collection<org.springframework.context.ApplicationListener> getApplicationListeners()
      Return a Collection containing all ApplicationListeners.
      protected java.util.Collection<org.springframework.context.ApplicationListener> getApplicationListeners​(org.springframework.context.ApplicationEvent event)
      Return a Collection of ApplicationListeners matching the given event type.
      protected java.util.concurrent.Executor getTaskExecutor()
      Return the current TaskExecutor for this multicaster.
      void multicastEvent​(org.springframework.context.ApplicationEvent event)  
      void multicastEvent​(org.springframework.context.ApplicationEvent event, org.springframework.core.ResolvableType eventType)  
      protected void multicastEventInternal​(org.springframework.context.ApplicationEvent event)  
      void removeAllListeners()  
      void removeApplicationListener​(org.springframework.context.ApplicationListener listener)  
      void removeApplicationListenerBean​(java.lang.String listenerBeanName)  
      void setApplicationContext​(org.springframework.context.ApplicationContext applicationContext)  
      void setTaskExecutor​(java.util.concurrent.Executor taskExecutor)
      Set the TaskExecutor to execute application listeners with.
      protected boolean supportsEvent​(org.springframework.context.ApplicationListener listener, java.lang.Class<? extends org.springframework.context.ApplicationEvent> eventType, java.lang.Class sourceType)
      Determine whether the given listener supports the given event.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SafeApplicationEventMulticaster

        public SafeApplicationEventMulticaster()
    • Method Detail

      • setTaskExecutor

        public void setTaskExecutor​(java.util.concurrent.Executor taskExecutor)
        Set the TaskExecutor to execute application listeners with.

        Default is a SyncTaskExecutor, executing the listeners synchronously in the calling thread.

        Consider specifying an asynchronous TaskExecutor 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.

      • getTaskExecutor

        protected java.util.concurrent.Executor getTaskExecutor()
        Return the current TaskExecutor for this multicaster.
      • addApplicationListener

        public void addApplicationListener​(org.springframework.context.ApplicationListener listener)
        Specified by:
        addApplicationListener in interface org.springframework.context.event.ApplicationEventMulticaster
      • addApplicationListenerBean

        public void addApplicationListenerBean​(java.lang.String listenerBeanName)
        Specified by:
        addApplicationListenerBean in interface org.springframework.context.event.ApplicationEventMulticaster
      • removeApplicationListener

        public void removeApplicationListener​(org.springframework.context.ApplicationListener listener)
        Specified by:
        removeApplicationListener in interface org.springframework.context.event.ApplicationEventMulticaster
      • removeApplicationListenerBean

        public void removeApplicationListenerBean​(java.lang.String listenerBeanName)
        Specified by:
        removeApplicationListenerBean in interface org.springframework.context.event.ApplicationEventMulticaster
      • removeAllListeners

        public void removeAllListeners()
        Specified by:
        removeAllListeners in interface org.springframework.context.event.ApplicationEventMulticaster
      • multicastEvent

        public void multicastEvent​(org.springframework.context.ApplicationEvent event)
        Specified by:
        multicastEvent in interface org.springframework.context.event.ApplicationEventMulticaster
      • multicastEvent

        public void multicastEvent​(org.springframework.context.ApplicationEvent event,
                                   org.springframework.core.ResolvableType eventType)
        Specified by:
        multicastEvent in interface org.springframework.context.event.ApplicationEventMulticaster
      • multicastEventInternal

        protected void multicastEventInternal​(org.springframework.context.ApplicationEvent event)
      • getApplicationListeners

        protected java.util.Collection<org.springframework.context.ApplicationListener> getApplicationListeners()
        Return a Collection containing all ApplicationListeners.
        Returns:
        a Collection of ApplicationListeners
        See Also:
        ApplicationListener
      • getApplicationListeners

        protected java.util.Collection<org.springframework.context.ApplicationListener> getApplicationListeners​(org.springframework.context.ApplicationEvent event)
        Return a Collection of ApplicationListeners matching the given event type. Non-matching listeners get excluded early.
        Parameters:
        event - the event to be propagated. Allows for excluding non-matching listeners early, based on cached matching information.
        Returns:
        a Collection of ApplicationListeners
        See Also:
        ApplicationListener
      • supportsEvent

        protected boolean supportsEvent​(org.springframework.context.ApplicationListener listener,
                                        java.lang.Class<? extends org.springframework.context.ApplicationEvent> eventType,
                                        java.lang.Class sourceType)
        Determine whether the given listener supports the given event.

        The default implementation detects the SmartApplicationListener interface. In case of a standard ApplicationListener, a GenericApplicationListenerAdapter will be used to introspect the generically declared type of the target listener.

        Parameters:
        listener - the target listener to check
        eventType - the event type to check against
        sourceType - the source type to check against
        Returns:
        whether the given listener should be included in the candidates for the given event type
      • setApplicationContext

        public void setApplicationContext​(org.springframework.context.ApplicationContext applicationContext)
                                   throws org.springframework.beans.BeansException
        Specified by:
        setApplicationContext in interface org.springframework.context.ApplicationContextAware
        Throws:
        org.springframework.beans.BeansException