Class AsyncCallRunnableListenerHelper


  • public class AsyncCallRunnableListenerHelper
    extends RunnableListenerHelper
    This class changes the behavior of how listeners are called from the parent class RunnableListenerHelper. In this implementation when listeners are invoked with the callListeners() function, the invocation of all the listeners will occur on the Executor that was provided at construction. If the listener was added without a provided executor it will then run on the provided executor (in the thread doing the callListeners() invocation, AKA it will run that listener before executing other listeners). If the listener was added with a provided executor, that listener will still execute on the provided executor (so not necessarily the executor provided at construction time).

    If it is desired that all listeners are executed asynchronously from each other, you should actually use the normal RunnableListenerHelper, and instead just ensure that an executor is provided when each listener is added. If you want listeners to execute concurrently from each other, but not concurrently for any single listener, DefaultExecutorRunnableListenerHelper is likely a better choice. This class is only designed to ensure that callListeners() invocations will never block.

    To better clarify when this implementation makes sense compared to RunnableListenerHelper and DefaultExecutorRunnableListenerHelper. If you have a LOT of quick running listeners, this is the right class for you. If you have few listeners that execute quickly, then the normal RunnableListenerHelper is likely a better choice. If you have long running/complex listeners, DefaultExecutorRunnableListenerHelper is possibly the better choice. Alternative for the last condition you could use the normal RunnableListenerHelper, and just ensure that an executor is provided for every listener (but if you want to ensure a given listener is not executed concurrently the DefaultExecutorRunnableListenerHelper will handle this for you).

    Since:
    2.2.0
    • Constructor Detail

      • AsyncCallRunnableListenerHelper

        public AsyncCallRunnableListenerHelper​(boolean callListenersOnce,
                                               java.util.concurrent.Executor executor)
        Constructs a new AsyncCallRunnableListenerHelper. This can call listeners one time, or every time callListeners is called.
        Parameters:
        callListenersOnce - true if listeners should only be called once
        executor - Executor that callListeners() should execute on
    • Method Detail

      • callListeners

        public void callListeners()
        Description copied from class: RunnableListenerHelper
        Will call all listeners that are registered with this helper. If any listeners were provided without an executor, they will execute in the calling thread. No exceptions will be thrown in this calling thread if any exceptions occur from the listeners.

        If calling multiple times, this will only have an effect if constructed with a false, indicating that listeners can expect to be called multiple times. In which case all listeners that have registered will be called again. If this was constructed with the expectation of only calling once an IllegalStateException will be thrown on subsequent calls.

        Overrides:
        callListeners in class RunnableListenerHelper