T
- Interface for listeners to implement and called into withpublic class AsyncCallListenerHelper<T> extends ListenerHelper<T>
ListenerHelper
. In this implementation when listeners are invoked with the
ListenerHelper.call()
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
ListenerHelper.call()
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 ListenerHelper
, 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, DefaultExecutorListenerHelper
is
likely a better choice. This class is only designed to ensure that ListenerHelper.call()
invocations
will never block.
To better clarify when this implementation makes sense compared to ListenerHelper
and
DefaultExecutorListenerHelper
. 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
ListenerHelper
is likely a better choice. If you have long running/complex listeners,
DefaultExecutorListenerHelper
is possibly the better choice. Alternative for the last
condition you could use the normal ListenerHelper
, 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 DefaultExecutorListenerHelper
will handle this for you).
It is important to note that this class does not ensure ordering of how listeners are called.
For example if you provided a multi-threaded executor, and are calling the listeners twice,
those listeners call order is non-deterministic. If this is important to you, you must ensure
that the Executor provided is single threaded (ie by using the
KeyDistributedExecutor
to get an executor from a single
key, or by using the ExecutorLimiter
with a
limit of one, or an instance of the SingleThreadScheduler
).
Constructor and Description |
---|
AsyncCallListenerHelper(java.lang.Class<? super T> listenerInterface,
java.util.concurrent.Executor executor)
Constructs a new
AsyncCallListenerHelper that will handle listeners with the provided
interface. |
addListener, addListener, call, clearListeners, getSubscribedListeners, registeredListenerCount, removeListener
public AsyncCallListenerHelper(java.lang.Class<? super T> listenerInterface, java.util.concurrent.Executor executor)
AsyncCallListenerHelper
that will handle listeners with the provided
interface. The provided class MUST be an interface. All ListenerHelper.call()
invocations will
occur on the provided executor, but listeners may run on different executors if they are
added with their respective executors.listenerInterface
- Interface that listeners need to implementexecutor
- Executor
that ListenerHelper.call()
invocation will occur on