T
- Interface for listeners to implement and called into withpublic class DefaultExecutorListenerHelper<T> extends ListenerHelper<T>
ListenerHelper.call()
. It does this in a different way from how the AsyncCallListenerHelper
does it. In this implementation the iteration of the listeners still occurs on the thread
executing the ListenerHelper.call()
, but as listeners are added, it is ensured that they are provided
an executor to execute on (so listener execution will actually happen on the executor). If a
listener is provided with an executor, that provided Executor will NOT be overridden, and
instead it will be used for the listeners execution.
Internally this class uses the KeyDistributedExecutor
, using the listener as the
execution key, to ensure that any single listener will NEVER execute concurrently with
itself.
In general, this implementation is most efficient when there are few listeners, but the
listeners are high complexity, or take a long time to execute. If you have few listeners AND
they execute quickly, the normal ListenerHelper
is likely a better choice. If you have
MANY listeners, but they execute very quickly, AsyncCallListenerHelper
is possibly a
better choice.
Unlike AsyncCallListenerHelper
, even if the executor provided here is multi-threaded,
order of listener call's are preserved. So there is no need to provide a single threaded
executor into this class.
Constructor and Description |
---|
DefaultExecutorListenerHelper(java.lang.Class<? super T> listenerInterface,
java.util.concurrent.Executor executor)
Constructs a new
DefaultExecutorListenerHelper that will handle listeners with the
provided interface. |
Modifier and Type | Method and Description |
---|---|
void |
addListener(T listener,
java.util.concurrent.Executor executor)
Adds a listener to be executed on the next
ListenerHelper.call() to this instance. |
addListener, call, clearListeners, getSubscribedListeners, registeredListenerCount, removeListener
public DefaultExecutorListenerHelper(java.lang.Class<? super T> listenerInterface, java.util.concurrent.Executor executor)
DefaultExecutorListenerHelper
that will handle listeners with the
provided interface. The provided class MUST be an interface. If any listeners are not
provided an executor, they will execute on the provided executor.listenerInterface
- Interface that listeners need to implementexecutor
- Executor to execute listeners which were not provided one by defaultpublic void addListener(T listener, java.util.concurrent.Executor executor)
ListenerHelper
ListenerHelper.call()
to this instance. If an executor
is provided, on the next ListenerHelper.call()
a task will be put on the executor to call this
listener. If none is provided, the listener will be executed on the thread that is invoking
the ListenerHelper.call()
.
If an Executor
is provided, and that Executor
is NOT single threaded, the
listener may be called concurrently. You can ensure this wont happen 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
.
addListener
in class ListenerHelper<T>
listener
- Listener to be called when ListenerHelper.call()
is invokedexecutor
- Executor
to call listener on, or null