org.bushe.swing.event

Class ThreadSafeEventService

    • Field Detail

      • CLEANUP_START_THRESHOLD_DEFAULT

        public static final Integer CLEANUP_START_THRESHOLD_DEFAULT
      • CLEANUP_STOP_THRESHOLD_DEFAULT

        public static final Integer CLEANUP_STOP_THRESHOLD_DEFAULT
      • CLEANUP_PERIOD_MS_DEFAULT

        public static final Long CLEANUP_PERIOD_MS_DEFAULT
      • LOG

        protected static final Logger LOG
    • Constructor Detail

      • ThreadSafeEventService

        public ThreadSafeEventService()
        Creates a ThreadSafeEventService that does not monitor timing of handlers.
      • ThreadSafeEventService

        public ThreadSafeEventService(Long timeThresholdForEventTimingEventPublication)
        Creates a ThreadSafeEventService while providing time monitoring options.
        Parameters:
        timeThresholdForEventTimingEventPublication - the longest time a subscriber should spend handling an event, The service will publish an SubscriberTimingEvent after listener processing if the time was exceeded. If null, no EventSubscriberTimingEvent will be issued.
      • ThreadSafeEventService

        public ThreadSafeEventService(Long timeThresholdForEventTimingEventPublication,
                              boolean subscribeTimingEventsInternally)
        Creates a ThreadSafeEventService while providing time monitoring options.
        Parameters:
        timeThresholdForEventTimingEventPublication - the longest time a subscriber should spend handling an event, The service will publish an SubscriberTimingEvent after listener processing if the time was exceeded. If null, no EventSubscriberTimingEvent will be issued.
        subscribeTimingEventsInternally - add a subscriber to the SubscriberTimingEvent internally and call the protected subscribeTiming() method when they occur. This logs a warning to the Logger by default.
      • ThreadSafeEventService

        public ThreadSafeEventService(Integer cleanupStartThreshold,
                              Integer cleanupStopThreshold,
                              Long cleanupPeriodMS)
        Creates a ThreadSafeEventService while providing proxy cleanup customization. Proxies are used with Annotations.
        Parameters:
        cleanupStartThreshold - see class javadoc.
        cleanupStopThreshold - see class javadoc.
        cleanupPeriodMS - see class javadoc.
      • ThreadSafeEventService

        public ThreadSafeEventService(Long timeThresholdForEventTimingEventPublication,
                              boolean subscribeTimingEventsInternally,
                              Integer cleanupStartThreshold,
                              Integer cleanupStopThreshold,
                              Long cleanupPeriodMS)
        Creates a ThreadSafeEventService while providing time monitoring options.
        Parameters:
        timeThresholdForEventTimingEventPublication - the longest time a subscriber should spend handling an event. The service will publish an SubscriberTimingEvent after listener processing if the time was exceeded. If null, no SubscriberTimingEvent will be issued.
        subscribeTimingEventsInternally - add a subscriber to the SubscriberTimingEvent internally and call the protected subscribeTiming() method when they occur. This logs a warning to the Logger by default.
        cleanupStartThreshold - see class javadoc.
        cleanupStopThreshold - see class javadoc.
        cleanupPeriodMS - see class javadoc.
        Throws:
        IllegalArgumentException - if timeThresholdForEventTimingEventPublication is null and subscribeTimingEventsInternally is true.
    • Method Detail

      • getCleanupStartThreshhold

        public Integer getCleanupStartThreshhold()
        Gets the threshold above which cleanup starts. See the class javadoc on cleanup.
        Returns:
        the threshold at which cleanup starts
      • setCleanupStartThreshhold

        public void setCleanupStartThreshhold(Integer cleanupStartThreshhold)
        Sets the threshold above which cleanup starts. See the class javadoc on cleanup.
        Parameters:
        cleanupStartThreshhold - threshold at which cleanup starts
      • getCleanupStopThreshold

        public Integer getCleanupStopThreshold()
        Gets the threshold below which cleanup stops. See the class javadoc on cleanup.
        Returns:
        threshold at which cleanup stops (it may start again)
      • setCleanupStopThreshold

        public void setCleanupStopThreshold(Integer cleanupStopThreshold)
        Sets the threshold below which cleanup stops. See the class javadoc on cleanup.
        Parameters:
        cleanupStopThreshold - threshold at which cleanup stops (it may start again).
      • getCleanupPeriodMS

        public Long getCleanupPeriodMS()
        Get the cleanup interval. See the class javadoc on cleanup.
        Returns:
        interval in milliseconds between cleanup runs.
      • setCleanupPeriodMS

        public void setCleanupPeriodMS(Long cleanupPeriodMS)
        Sets the cleanup interval. See the class javadoc on cleanup.
        Parameters:
        cleanupPeriodMS - interval in milliseconds between cleanup runs. Passing null stops cleanup.
      • subscribe

        public boolean subscribe(Class cl,
                        EventSubscriber eh)
        Description copied from interface: EventService
        Subscribes an EventSubscriber to the publication of objects matching a type. Only a WeakReference to the subscriber is held by the EventService.

        Subscribing to a class means the subscriber will be called when objects of that class are published, when objects of subclasses of the class are published, when objects implementing any of the interfaces of the class are published, or when generic types are published with the class' raw type.

        Subscription is weak by default to avoid having to call unsubscribe(), and to avoid the memory leaks that would occur if unsubscribe was not called. The service will respect the WeakReference semantics. In other words, if the subscriber has not been garbage collected, then onEvent(Object) will be called normally. If the hard reference has been garbage collected, the service will unsubscribe it's WeakReference.

        It's allowable to call unsubscribe() with the same EventSubscriber hard reference to stop a subscription immediately.

        The service will create the WeakReference on behalf of the caller.

        Specified by:
        subscribe in interface EventService
        Parameters:
        cl - the class of published objects to subscriber listen to
        eh - The subscriber that will accept the events of the event class when published.
        Returns:
        true if the subscriber was subscribed successfully, false otherwise
        See Also:
        EventService.subscribe(Class,EventSubscriber)
      • subscribe

        public boolean subscribe(Type type,
                        EventSubscriber eh)
        Description copied from interface: EventService
        Subscribe an EventSubscriber to publication of generic Types. Subscribers will only be notified for publications using EventService.publish(java.lang.reflect.Type, Object).

        Due to generic type erasure, the type must be supplied by the publisher. You can get a declared object's type by using the TypeReference class. For Example:

         TypeReference<List<Trade>> subscribingTypeReference = new TypeReference<List<Trade>>(){};
         EventBus.subscribe(subscribingTypeReference.getType(), mySubscriber);
         EventBus.subscribe(List.class, thisSubscriberWillGetCalledToo);
         ...
         //Likely in some other class
         TypeReference<List<Trade>> publishingTypeReference = new TypeReference<List<Trade>>(){};
         List<Trade> trades = new ArrayList<Trade>();
         EventBus.publish(publishingTypeReference.getType(), trades);
         trades.add(trade);
         EventBus.publish(publishingTypeReference.getType(), trades);
         

        Specified by:
        subscribe in interface EventService
        Parameters:
        type - the generic type to subscribe to
        eh - the subscriber to the type
        Returns:
        true if a new subscription is made, false if it already existed
        See Also:
        EventService.subscribe(java.lang.reflect.Type, EventSubscriber)
      • subscribeExactly

        public boolean subscribeExactly(Class cl,
                               EventSubscriber eh)
        Description copied from interface: EventService
        Subscribes an EventSubscriber to the publication of objects exactly matching a type. Only a WeakReference to the subscriber is held by the EventService.

        Subscription is weak by default to avoid having to call unsubscribe(), and to avoid the memory leaks that would occur if unsubscribe was not called. The service will respect the WeakReference semantics. In other words, if the subscriber has not been garbage collected, then the onEvent will be called normally. If the hard reference has been garbage collected, the service will unsubscribe it's WeakReference.

        It's allowable to call unsubscribe() with the same EventSubscriber hard reference to stop a subscription immediately.

        The service will create the WeakReference on behalf of the caller.

        Specified by:
        subscribeExactly in interface EventService
        Parameters:
        cl - the class of published objects to listen to
        eh - The subscriber that will accept the events when published.
        Returns:
        true if the subscriber was subscribed successfully, false otherwise
        See Also:
        EventService.subscribeExactly(Class,EventSubscriber)
      • subscribe

        public boolean subscribe(String topic,
                        EventTopicSubscriber eh)
        Description copied from interface: EventService
        Subscribes an EventTopicSubscriber to the publication of a topic name. Only a WeakReference to the subscriber is held by the EventService.

        Subscription is weak by default to avoid having to call unsubscribe(), and to avoid the memory leaks that would occur if unsubscribe was not called. The service will respect the WeakReference semantics. In other words, if the subscriber has not been garbage collected, then the onEvent will be called normally. If the hard reference has been garbage collected, the service will unsubscribe it's WeakReference.

        It's allowable to call unsubscribe() with the same EventSubscriber hard reference to stop a subscription immediately.

        Specified by:
        subscribe in interface EventService
        Parameters:
        topic - the name of the topic listened to
        eh - The topic subscriber that will accept the events when published.
        Returns:
        true if the subscriber was subscribed successfully, false otherwise
        See Also:
        EventService.subscribe(String,EventTopicSubscriber)
      • subscribe

        public boolean subscribe(Pattern pat,
                        EventTopicSubscriber eh)
        Description copied from interface: EventService
        Subscribes an EventSubscriber to the publication of all the topic names that match a RegEx Pattern. Only a WeakReference to the subscriber is held by the EventService.

        Subscription is weak by default to avoid having to call unsubscribe(), and to avoid the memory leaks that would occur if unsubscribe was not called. The service will respect the WeakReference semantics. In other words, if the subscriber has not been garbage collected, then the onEvent will be called normally. If the hard reference has been garbage collected, the service will unsubscribe it's WeakReference.

        It's allowable to call unsubscribe() with the same EventSubscriber hard reference to stop a subscription immediately.

        Specified by:
        subscribe in interface EventService
        Parameters:
        pat - pattern that matches to the name of the topic published to
        eh - The topic subscriber that will accept the events when published.
        Returns:
        true if the subscriber was subscribed successfully, false otherwise
        See Also:
        EventService.subscribe(Pattern,EventTopicSubscriber)
      • subscribeVetoListener

        public boolean subscribeVetoListener(Class eventClass,
                                    VetoEventListener vetoListener)
        Description copied from interface: EventService
        Subscribes a VetoEventListener to publication of event matching a class. Only a WeakReference to the VetoEventListener is held by the EventService.

        Use this method to avoid having to call unsubscribe(), though with care since garbage collection semantics is indeterminate. The service will respect the WeakReference semantics. In other words, if the vetoListener has not been garbage collected, then the onEvent will be called normally. If the hard reference has been garbage collected, the service will unsubscribe it's WeakReference.

        It's allowable to call unsubscribe() with the same VetoEventListener hard reference to stop a subscription immediately.

        The service will create the WeakReference on behalf of the caller.

        Specified by:
        subscribeVetoListener in interface EventService
        Parameters:
        eventClass - the class of published objects that can be vetoed
        vetoListener - The VetoEventListener that can determine whether an event is published.
        Returns:
        true if the VetoEventListener was subscribed successfully, false otherwise
        See Also:
        EventService.subscribeVetoListener(Class,VetoEventListener)
      • subscribeVetoListenerExactly

        public boolean subscribeVetoListenerExactly(Class eventClass,
                                           VetoEventListener vetoListener)
        Description copied from interface: EventService
        Subscribes a VetoEventListener to publication of an exact event class. Only a WeakReference to the VetoEventListener is held by the EventService.

        Use this method to avoid having to call unsubscribe(), though with care since garbage collection semantics is indeterminate. The service will respect the WeakReference semantics. In other words, if the vetoListener has not been garbage collected, then the onEvent will be called normally. If the hard reference has been garbage collected, the service will unsubscribe it's WeakReference.

        It's allowable to call unsubscribe() with the same VetoEventListener hard reference to stop a subscription immediately.

        The service will create the WeakReference on behalf of the caller.

        Specified by:
        subscribeVetoListenerExactly in interface EventService
        Parameters:
        eventClass - the class of published objects that can be vetoed
        vetoListener - The vetoListener that can determine whether an event is published.
        Returns:
        true if the vetoListener was subscribed successfully, false otherwise
        See Also:
        EventService.subscribeVetoListenerExactly(Class,VetoEventListener)
      • subscribeVetoListener

        public boolean subscribeVetoListener(Pattern topicPattern,
                                    VetoTopicEventListener vetoListener)
        Description copied from interface: EventService
        Subscribes an VetoTopicEventListener to all the topic names that match the RegEx Pattern. Only a WeakReference to the VetoEventListener is held by the EventService.
        Specified by:
        subscribeVetoListener in interface EventService
        Parameters:
        topicPattern - the RegEx pattern to match topics with
        vetoListener - The vetoListener that can determine whether an event is published.
        Returns:
        true if the vetoListener was subscribed successfully, false otherwise
        See Also:
        EventService.subscribeVetoListener(Pattern,VetoTopicEventListener)
      • subscribeVetoListener

        protected boolean subscribeVetoListener(Object subscription,
                                    Map vetoListenerMap,
                                    Object vetoListener)
        All veto subscriptions methods call this method. Extending classes only have to override this method to subscribe all veto subscriptions.
        Parameters:
        subscription - the topic, Pattern, or event class to subscribe to
        vetoListenerMap - the internal map of veto listeners to use (by topic of class)
        vetoListener - the veto listener to subscribe, may be a VetoEventListener or a WeakReference to one
        Returns:
        boolean if the veto listener is subscribed (was not subscribed).
        Throws:
        IllegalArgumentException - if vl or o is null
      • subscribe

        protected boolean subscribe(Object classTopicOrPatternWrapper,
                        Map<Object,Object> subscriberMap,
                        Object subscriber)
        All subscribe methods call this method, including veto subscriptions. Extending classes only have to override this method to subscribe all subscriber subscriptions.

        Overriding this method is only for the adventurous. This basically gives you just enough rope to hang yourself.

        Parameters:
        classTopicOrPatternWrapper - the topic String, event Class, or PatternWrapper to subscribe to
        subscriberMap - the internal map of subscribers to use (by topic or class)
        subscriber - the EventSubscriber or EventTopicSubscriber to subscribe, or a WeakReference to either
        Returns:
        boolean if the subscriber is subscribed (was not subscribed).
        Throws:
        IllegalArgumentException - if subscriber or topicOrClass is null
      • unsubscribe

        public boolean unsubscribe(Class cl,
                          EventSubscriber eh)
        Description copied from interface: EventService
        Stop the subscription for a subscriber that is subscribed to a class.
        Specified by:
        unsubscribe in interface EventService
        Parameters:
        cl - the class of published objects to listen to
        eh - The subscriber that is subscribed to the event. The same reference as the one subscribed.
        Returns:
        true if the subscriber was subscribed to the event, false if it wasn't
        See Also:
        EventService.unsubscribe(Class,EventSubscriber)
      • unsubscribe

        public boolean unsubscribe(Pattern topicPattern,
                          EventTopicSubscriber eh)
        Description copied from interface: EventService
        Stop the subscription for a subscriber that is subscribed to event topics via a Pattern.
        Specified by:
        unsubscribe in interface EventService
        Parameters:
        topicPattern - the regex expression matching topics listened to
        eh - The subscriber that is subscribed to the topic. The same reference as the one subscribed.
        Returns:
        true if the subscriber was subscribed to the event, false if it wasn't
        See Also:
        EventService.unsubscribe(String,EventTopicSubscriber)
      • unsubscribe

        public boolean unsubscribe(Class eventClass,
                          Object subscribedByProxy)
        Description copied from interface: EventService
        Stop a subscription for an object that is subscribed with a ProxySubscriber.

        If an object is subscribed by proxy and it implements EventSubscriber, then the normal unsubscribe methods will still unsubscribe the object.

        Specified by:
        unsubscribe in interface EventService
        Parameters:
        eventClass - class this object is subscribed to by proxy
        subscribedByProxy - object subscribed by proxy
        Returns:
        true if the subscription was cancelled, false if it never existed
        See Also:
        EventService.unsubscribe(Class,Object)
      • unsubscribeExactly

        public boolean unsubscribeExactly(Class eventClass,
                                 Object subscribedByProxy)
        Description copied from interface: EventService
        Stop a subscription for an object that is subscribed exactly with a ProxySubscriber.

        If an object is subscribed by proxy and it implements EventSubscriber, then the normal unsubscribe methods will still unsubscribe the object.

        Specified by:
        unsubscribeExactly in interface EventService
        Parameters:
        eventClass - class this object is subscribed to by proxy
        subscribedByProxy - object subscribed by proxy
        Returns:
        true if the subscription was cancelled, false if it never existed
        See Also:
        EventService.unsubscribeExactly(Class,Object)
      • unsubscribe

        public boolean unsubscribe(String topic,
                          Object subscribedByProxy)
        Description copied from interface: EventService
        Stop a subscription for an object that is subscribed to a topic with a ProxySubscriber.

        If an object is subscribed by proxy and it implements EventSubscriber, then the normal unsubscribe methods will still unsubscribe the object.

        Specified by:
        unsubscribe in interface EventService
        Parameters:
        topic - the topic this object is subscribed to by proxy
        subscribedByProxy - object subscribed by proxy
        Returns:
        true if the subscription was cancelled, false if it never existed
        See Also:
        EventService.unsubscribe(String,Object)
      • unsubscribe

        public boolean unsubscribe(Pattern pattern,
                          Object subscribedByProxy)
        Description copied from interface: EventService
        When using annotations, an object may be subscribed by proxy. This unsubscribe method will unsubscribe an object that is subscribed with a ProxySubscriber.

        If an object is subscribed by proxy and it implements EventSubscriber, then the normal unsubscribe methods will still unsubscribe the object.

        Specified by:
        unsubscribe in interface EventService
        Parameters:
        pattern - the RegEx expression this object is subscribed to by proxy
        subscribedByProxy - object subscribed by proxy
        Returns:
        true if the subscription was cancelled, false if it never existed
        See Also:
        EventService.unsubscribe(java.util.regex.Pattern,Object)
      • unsubscribe

        protected boolean unsubscribe(Object o,
                          Map subscriberMap,
                          Object subscriber)
        All event subscriber unsubscriptions call this method. Extending classes only have to override this method to subscribe all subscriber unsubscriptions.
        Parameters:
        o - the topic or event class to unsubscribe from
        subscriberMap - the map of subscribers to use (by topic of class)
        subscriber - the subscriber to unsubscribe, either an EventSubscriber or an EventTopicSubscriber, or a WeakReference to either
        Returns:
        boolean if the subscriber is unsubscribed (was subscribed).
      • unsubscribeVeto

        public boolean unsubscribeVeto(Class eventClass,
                              Object subscribedByProxy)
        Description copied from interface: EventService
        Stop a veto subscription for an object that is subscribed with a ProxySubscriber.

        If an object is subscribed by proxy and it implements VetoSubscriber, then the normal unsubscribe methods will still unsubscribe the object.

        Specified by:
        unsubscribeVeto in interface EventService
        Parameters:
        eventClass - class this object is subscribed to by proxy
        subscribedByProxy - object subscribed by proxy
        Returns:
        true if the subscription was cancelled, false if it never existed
        See Also:
        EventService.unsubscribeVeto(Class,Object)
      • unsubscribeVetoExactly

        public boolean unsubscribeVetoExactly(Class eventClass,
                                     Object subscribedByProxy)
        Description copied from interface: EventService
        Stop a veto subscription for an object that is subscribed exactly with a ProxySubscriber.

        If an object is subscribed by proxy and it implements VetoSubscriber, then the normal unsubscribe methods will still unsubscribe the object.

        Specified by:
        unsubscribeVetoExactly in interface EventService
        Parameters:
        eventClass - class this object is subscribed to by proxy
        subscribedByProxy - object subscribed by proxy
        Returns:
        true if the subscription was cancelled, false if it never existed
        See Also:
        EventService.unsubscribeVetoExactly(Class,Object)
      • unsubscribeVeto

        public boolean unsubscribeVeto(String topic,
                              Object subscribedByProxy)
        Description copied from interface: EventService
        Stop a veto subscription for an object that is subscribed to a topic with a ProxySubscriber.

        If an object is subscribed by proxy and it implements EventSubscriber, then the normal unsubscribe methods will still unsubscribe the object.

        Specified by:
        unsubscribeVeto in interface EventService
        Parameters:
        topic - the topic this object is subscribed to by proxy
        subscribedByProxy - object subscribed by proxy
        Returns:
        true if the subscription was cancelled, false if it never existed
        See Also:
        EventService.unsubscribeVeto(String,Object)
      • unsubscribeVeto

        public boolean unsubscribeVeto(Pattern pattern,
                              Object subscribedByProxy)
        Description copied from interface: EventService
        When using annotations, an object may be subscribed by proxy. This unsubscribe method will unsubscribe an object that is subscribed with a ProxySubscriber.

        If an object is subscribed by proxy and it implements EventSubscriber, then the normal unsubscribe methods will still unsubscribe the object.

        Specified by:
        unsubscribeVeto in interface EventService
        Parameters:
        pattern - the RegEx expression this object is subscribed to by proxy
        subscribedByProxy - object subscribed by proxy
        Returns:
        true if the subscription was cancelled, false if it never existed
        See Also:
        EventService.unsubscribeVeto(java.util.regex.Pattern,Object)
      • unsubscribeVetoListener

        protected boolean unsubscribeVetoListener(Object o,
                                      Map vetoListenerMap,
                                      Object vl)
        All veto unsubscriptions methods call this method. Extending classes only have to override this method to subscribe all veto unsubscriptions.
        Parameters:
        o - the topic or event class to unsubscribe from
        vetoListenerMap - the map of veto listeners to use (by topic or class)
        vl - the veto listener to unsubscribe, or a WeakReference to one
        Returns:
        boolean if the veto listener is unsubscribed (was subscribed).
      • publish

        public void publish(Object event)
        Description copied from interface: EventService
        Publishes an object so that subscribers will be notified if they subscribed to the object's class, one of its subclasses, or to one of the interfaces it implements.
        Specified by:
        publish in interface EventService
        Parameters:
        event - the object to publish
        See Also:
        EventService.publish(Object)
      • publish

        public void publish(Type genericType,
                   Object event)
        Description copied from interface: EventService
        Use this method to publish generified objects to subscribers of Types, i.e. subscribers that use EventService.subscribe(Type, EventSubscriber), and to publish to subscribers of the non-generic type.

        Due to generic type erasure, the type must be supplied by the caller. You can get a declared object's type by using the TypeReference class. For Example:

         TypeReference<List<Trade>> subscribingTypeReference = new TypeReference<List<Trade>>(){};
         EventBus.subscribe(subscribingTypeReference.getType(), mySubscriber);
         EventBus.subscribe(List.class, thisSubscriberWillGetCalledToo);
         ...
         //Likely in some other class
         TypeReference<List<Trade>> publishingTypeReference = new TypeReference<List<Trade>>(){};
         List<Trade> trades = new ArrayList<Trade>();
         EventBus.publish(publishingTypeReference.getType(), trades);
         trades.add(trade);
         EventBus.publish(publishingTypeReference.getType(), trades);
         

        Specified by:
        publish in interface EventService
        Parameters:
        genericType - the generified type of the published object.
        event - The event that occurred
        See Also:
        EventService.publish(java.lang.reflect.Type, Object)
      • publish

        public void publish(String topicName,
                   Object eventObj)
        Description copied from interface: EventService
        Publishes an object on a topic name so that all subscribers to that name or a Regular Expression that matches the topic name will be notified.
        Specified by:
        publish in interface EventService
        Parameters:
        topicName - The name of the topic subscribed to
        eventObj - the object to publish
        See Also:
        EventService.publish(String,Object)
      • publish

        protected void publish(Object event,
                   String topic,
                   Object eventObj,
                   List subscribers,
                   List vetoSubscribers,
                   StackTraceElement[] callingStack)
        All publish methods call this method. Extending classes only have to override this method to handle all publishing cases.
        Parameters:
        event - the event to publish, null if publishing on a topic
        topic - if publishing on a topic, the topic to publish on, else null
        eventObj - if publishing on a topic, the eventObj to publish, else null
        subscribers - the subscribers to publish to - must be a snapshot copy
        vetoSubscribers - the veto subscribers to publish to - must be a snapshot copy.
        callingStack - the stack that called this publication, helpful for reporting errors on other threads
        Throws:
        IllegalArgumentException - if eh or o is null
      • setStatus

        protected void setStatus(PublicationStatus status,
                     Object event,
                     String topic,
                     Object eventObj)
        Called during publication to set the status on an event. Can be used by subclasses to be notified when an event transitions from one state to another. Implementers are required to call setPublicationStatus
        Parameters:
        status - the status to set on the object
        event - the event being published, will be null if topic is not null
        topic - the topic eventObj is being published on, will be null if event is not null
        eventObj - the payload being published on the topic , will be null if event is not null
      • addEventToCache

        protected void addEventToCache(Object event,
                           String topic,
                           Object eventObj)
        Adds an event to the event cache, if appropriate. This method is called just before publication to listeners, after the event passes any veto listeners.

        Using protected visibility to open the caching to other implementations.

        Parameters:
        event - the event about to be published, null if topic is non-null
        topic - the topic about to be published to, null if the event is non-null
        eventObj - the eventObj about to be published on a topic, null if the event is non-null
      • getSubscribers

        public <T> List<T> getSubscribers(Class<T> eventClass)
        Description copied from interface: EventService
        Union of getSubscribersToClass(Class) and getSubscribersToExactClass(Class)
        Specified by:
        getSubscribers in interface EventService
        Parameters:
        eventClass - the eventClass of interest
        Returns:
        the subscribers that will be called when an event of eventClass is published, this includes those subscribed that match by exact class and those that match to a class and its subtypes
        See Also:
        EventService.getSubscribers(Class)
      • getSubscribersToClass

        public <T> List<T> getSubscribersToClass(Class<T> eventClass)
        Description copied from interface: EventService
        Gets subscribers that subscribed with the given a class, but not those subscribed exactly to the class.
        Specified by:
        getSubscribersToClass in interface EventService
        Parameters:
        eventClass - the eventClass of interest
        Returns:
        the subscribers that are subscribed to match to a class and its subtypes, but not those subscribed by exact class
        See Also:
        EventService.getSubscribersToClass(Class)
      • getSubscribersToExactClass

        public <T> List<T> getSubscribersToExactClass(Class<T> eventClass)
        Description copied from interface: EventService
        Gets subscribers that are subscribed exactly to a class, but not those subscribed non-exactly to a class.
        Specified by:
        getSubscribersToExactClass in interface EventService
        Parameters:
        eventClass - the eventClass of interest
        Returns:
        the subscribers that are subscribed by exact class but not those subscribed to match to a class and its subtypes
        See Also:
        EventService.getSubscribersToExactClass(Class)
      • getSubscribers

        public <T> List<T> getSubscribers(Type eventType)
        Description copied from interface: EventService
        Gets the subscribers that subscribed to a generic type.
        Specified by:
        getSubscribers in interface EventService
        Parameters:
        eventType - the type of interest
        Returns:
        the subscribers that will be called when an event of eventClass is published, this includes those subscribed that match by exact class and those that match to a class and its subtypes
        See Also:
        EventService.getSubscribers(Type)
      • getSubscribers

        public <T> List<T> getSubscribers(String topic)
        Description copied from interface: EventService
        Union of getSubscribersByPattern(String) and geSubscribersToTopic(String)
        Specified by:
        getSubscribers in interface EventService
        Parameters:
        topic - the topic of interest
        Returns:
        the subscribers that will be called when an event is published on the topic. This includes subscribers subscribed to match the exact topic name and those subscribed by a RegEx Pattern that matches the topic name.
        See Also:
        EventService.getSubscribers(String)
      • getVetoSubscribers

        public <T> List<T> getVetoSubscribers(Class<T> eventClass)
        Description copied from interface: EventService
        Gets veto subscribers that subscribed to a given class.
        Specified by:
        getVetoSubscribers in interface EventService
        Parameters:
        eventClass - the eventClass of interest
        Returns:
        the veto subscribers that will be called when an event of eventClass or its subclasses is published.
        See Also:
        EventService.getVetoSubscribers(Class)
      • getVetoSubscribers

        public <T> List<T> getVetoSubscribers(String topic)
        Deprecated. use getVetoSubscribersToTopic instead for direct replacement, or use getVetoEventListeners to get topic and pattern matchers. In EventBus 2.0 this name will replace getVetoEventListeners() and have it's union functionality
        Note: this is inconsistent with getSubscribers(String)
        Specified by:
        getVetoSubscribers in interface EventService
        Parameters:
        topic - the topic exactly subscribed to
        Returns:
        the veto subscribers that are subscribed to the topic.
        See Also:
        EventService.getVetoSubscribersToTopic(String)
      • getSubscribersToPattern

        protected <T> List<T> getSubscribersToPattern(Pattern topicPattern)
      • handleVeto

        protected void handleVeto(VetoEventListener vl,
                      Object event,
                      VetoTopicEventListener vtl,
                      String topic,
                      Object eventObj)
        Handle vetos of an event or topic, by default logs finely.
        Parameters:
        vl - the veto listener for an event
        event - the event, can be null if topic is not
        vtl - the veto listener for a topic
        topic - can be null if event is not
        eventObj - the object published with the topic
      • setDefaultCacheSizePerClassOrTopic

        public void setDefaultCacheSizePerClassOrTopic(int defaultCacheSizePerClassOrTopic)
        Sets the default cache size for each kind of event, default is 0 (no caching).

        If this value is set to a positive number, then when an event is published, the EventService caches the event or topic payload data for later retrieval. This allows subscribers to find out what has most recently happened before they subscribed. The cached event(s) are returned from #getLastEvent(Class), #getLastTopicData(String), #getCachedEvents(Class), or #getCachedTopicData(String)

        The default can be overridden on a by-event-class or by-topic basis.

        Specified by:
        setDefaultCacheSizePerClassOrTopic in interface EventService
        Parameters:
        defaultCacheSizePerClassOrTopic -
      • getDefaultCacheSizePerClassOrTopic

        public int getDefaultCacheSizePerClassOrTopic()
        Description copied from interface: EventService
        The default number of events or payloads kept per event class or topic
        Specified by:
        getDefaultCacheSizePerClassOrTopic in interface EventService
        Returns:
        the default number of event payloads kept per event class or topic
      • setCacheSizeForEventClass

        public void setCacheSizeForEventClass(Class eventClass,
                                     int cacheSize)
        Set the number of events cached for a particular class of event. By default, no events are cached.

        This overrides any setting for the DefaultCacheSizePerClassOrTopic.

        Class hierarchy semantics are respected. That is, if there are three events, A, X and Y, and X and Y are both derived from A, then setting the cache size for A applies the cache size for all three. Setting the cache size for X applies to X and leaves the settings for A and Y in tact. Interfaces can be passed to this method, but they only take effect if the cache size of a class or it's superclasses has been set. Just like Class.getInterfaces(), if multiple cache sizes are set, the interface names declared earliest in the implements clause of the eventClass takes effect.

        The cache for an event is not adjusted until the next event of that class is published.

        Specified by:
        setCacheSizeForEventClass in interface EventService
        Parameters:
        eventClass - the class of event
        cacheSize - the number of published events to cache for this event
      • getCacheSizeForEventClass

        public int getCacheSizeForEventClass(Class eventClass)
        Returns the number of events cached for a particular class of event. By default, no events are cached.

        This result is computed for a particular class from the values passed to #setCacheSizeForEventClass(Class, int), and respects the class hierarchy.

        Specified by:
        getCacheSizeForEventClass in interface EventService
        Parameters:
        eventClass - the class of event
        Returns:
        the maximum size of the event cache for the given event class
        See Also:
        setCacheSizeForEventClass(Class,int)
      • setCacheSizeForTopic

        public void setCacheSizeForTopic(String topicName,
                                int cacheSize)
        Set the number of published data objects cached for a particular event topic. By default, no caching is done.

        This overrides any setting for the DefaultCacheSizePerClassOrTopic.

        Settings for exact topic names take precedence over pattern matching.

        The cache for a topic is not adjusted until the next publication on that topic.

        Specified by:
        setCacheSizeForTopic in interface EventService
        Parameters:
        topicName - the topic name
        cacheSize - the number of published data Objects to cache for this topic
      • setCacheSizeForTopic

        public void setCacheSizeForTopic(Pattern pattern,
                                int cacheSize)
        Set the number of published data objects cached for topics matching a pattern. By default, caching is done.

        This overrides any setting for the DefaultCacheSizePerClassOrTopic.

        Settings for exact topic names take precedence over pattern matching. If a topic matches the cache settings for more than one pattern, the cache size chosen is an undetermined one from one of the matched pattern settings.

        The cache for a topic is not adjusted until the next publication on that topic.

        Specified by:
        setCacheSizeForTopic in interface EventService
        Parameters:
        pattern - the pattern matching topic names
        cacheSize - the number of data Objects to cache for this topic
      • getLastEvent

        public Object getLastEvent(Class eventClass)
        Description copied from interface: EventService
        When caching, returns the last event publish for the type supplied.
        Specified by:
        getLastEvent in interface EventService
        Parameters:
        eventClass - an index into the cache, cannot be an interface
        Returns:
        the last event published for this event class, or null if caching is turned off (the default)
      • getCachedEvents

        public List getCachedEvents(Class eventClass)
        Description copied from interface: EventService
        When caching, returns the last set of event published for the type supplied.
        Specified by:
        getCachedEvents in interface EventService
        Parameters:
        eventClass - an index into the cache, cannot be an interface
        Returns:
        the last events published for this event class, or null if caching is turned off (the default)
      • getLastTopicData

        public Object getLastTopicData(String topic)
        Description copied from interface: EventService
        When caching, returns the last payload published on the topic name supplied.
        Specified by:
        getLastTopicData in interface EventService
        Parameters:
        topic - an index into the cache
        Returns:
        the last data Object published on this topic, or null if caching is turned off (the default)
      • getCachedTopicData

        public List getCachedTopicData(String topic)
        Description copied from interface: EventService
        When caching, returns the last set of payload objects published on the topic name supplied.
        Specified by:
        getCachedTopicData in interface EventService
        Parameters:
        topic - an index into the cache
        Returns:
        the last data Objects published on this topic, or null if caching is turned off (the default)
      • clearCache

        public void clearCache(Class eventClassToClear)
        Clears the event cache for a specific event class or interface and it's any of it's subclasses or implementing classes.
        Specified by:
        clearCache in interface EventService
        Parameters:
        eventClassToClear - the event class to clear the cache for
      • clearCache

        public void clearCache(String topic)
        Clears the topic data cache for a specific topic name.
        Specified by:
        clearCache in interface EventService
        Parameters:
        topic - the topic name to clear the cache for
      • clearCache

        public void clearCache(Pattern pattern)
        Clears the topic data cache for all topics that match a particular pattern.
        Specified by:
        clearCache in interface EventService
        Parameters:
        pattern - the pattern to match topic caches to
      • clearCache

        public void clearCache()
        Clear all event caches for all topics and event.
        Specified by:
        clearCache in interface EventService
      • getRealSubscriberAndCleanStaleSubscriberIfNecessary

        protected Object getRealSubscriberAndCleanStaleSubscriberIfNecessary(Iterator iterator,
                                                                 Object existingSubscriber)
        Unsubscribe a subscriber if it is a stale ProxySubscriber. Used during subscribe() and in the cleanup Timer. See the class javadoc.

        Not private since I don't claim I'm smart enough to anticipate all needs, but I am smart enough to doc the rules you must follow to override this method. Those rules may change (changes will be doc'ed), override at your own risk.

        Overriders MUST call iterator.remove() to unsubscribe the proxy if the subscriber is a ProxySubscriber and is stale and should be cleaned up. If the ProxySubscriber is unsubscribed, then implementers MUST also call proxyUnsubscribed() on the subscriber. Overriders MUST also remove the proxy from the weakProxySubscriber list by calling removeStaleProxyFromList. Method assumes caller is holding the listenerList lock (else how can you pass the iterator?).

        Parameters:
        iterator - current iterator
        existingSubscriber - the current value of the iterator
        Returns:
        the real value of the param, or the proxied subscriber of the param if the param is a a ProxySubscriber
      • incWeakRefPlusProxySubscriberCount

        protected void incWeakRefPlusProxySubscriberCount()
        Increment the count of stale proxies and start a cleanup task if necessary
      • decWeakRefPlusProxySubscriberCount

        protected void decWeakRefPlusProxySubscriberCount()
        Decrement the count of stale proxies

Copyright © 2013 Bushe Enterprises, Inc.. All Rights Reserved.