Class ConcurrentLockedPublisher<T>
java.lang.Object
dev.getelements.elements.sdk.util.ConcurrentLockedPublisher<T>
- Type Parameters:
T
- the type of event to publish
- All Implemented Interfaces:
AsyncPublisher<T>
,Publisher<T>
,Iterable<Object>
public class ConcurrentLockedPublisher<T>
extends Object
implements AsyncPublisher<T>, Iterable<Object>
Implements a
AsyncPublisher
using a Lock
to control concurrency. For each published event, the
supplied Lock
will be acquired and then released when all associated Subscription
s have been
notified.
This implements Iterable
for debugging only. This locks and copies the list of Subscription
s
to perform the operation.-
Constructor Summary
ConstructorsConstructorDescriptionCreates a newConcurrentLockedPublisher
with the suppliedLock
.ConcurrentLockedPublisher
(Lock lock, Consumer<Runnable> dispatch) Creates a newConcurrentLockedPublisher
with the suppliedLock
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clears allSubscription
s and implicitly removes them from the internal pool.iterator()
void
Publishes the event synchronously.void
Publishes the supplied event synchronously and calls theConsumer
when allSubscription
s have been notified.void
Publishes the supplied event synchronously and calls theConsumer
when allSubscription
s have been notified.void
publishAsync
(T t) Publishes the supplied event asynchronously.void
publishAsync
(T t, Consumer<T> onFinish) Publishes the supplied event asynchronously and calls theConsumer<T>
when allSubscription
s have been notified.void
Publishes the supplied event asynchronously and calls theConsumer<T>
when allSubscription
s have been notified.subscribe
(BiConsumer<Subscription, ? super T> consumer) Subscribes to a particular event.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
ConcurrentLockedPublisher
Creates a newConcurrentLockedPublisher
with the suppliedLock
. This uses an internal thread pool to dispatch events to listeners.- Parameters:
lock
- the lock to ensure access to thisConcurrentLockedPublisher<T>
-
ConcurrentLockedPublisher
Creates a newConcurrentLockedPublisher
with the suppliedLock
.- Parameters:
lock
- the lock to ensure access to thisConcurrentLockedPublisher<T>
dispatch
- aConsumer<Runnable>
which is used to dispatch tasks
-
-
Method Details
-
subscribe
Description copied from interface:Publisher
Subscribes to a particular event. The suppliedBiConsumer
will receive zero or more events in the future until the associated call toSubscription.unsubscribe()
.- Specified by:
subscribe
in interfacePublisher<T>
- Parameters:
consumer
- theBiConsumer
which will accept event- Returns:
- the
Subscription
-
clear
public void clear()Description copied from interface:Publisher
Clears allSubscription
s and implicitly removes them from the internal pool. Implementing this interface method is optional, and therefore this may throw an instance ofUnsupportedOperationException
. -
publish
Description copied from interface:Publisher
Publishes the event synchronously. -
publish
Description copied from interface:Publisher
Publishes the supplied event synchronously and calls theConsumer
when allSubscription
s have been notified.- Specified by:
publish
in interfacePublisher<T>
- Parameters:
t
- the eventonFinish
- theConsumer
to be called after allSubscription
s have been notified
-
publish
Description copied from interface:Publisher
Publishes the supplied event synchronously and calls theConsumer
when allSubscription
s have been notified. Additionally, this notifies for every exception thrown in the process. -
publishAsync
Description copied from interface:AsyncPublisher
Publishes the supplied event asynchronously.- Specified by:
publishAsync
in interfaceAsyncPublisher<T>
- Parameters:
t
- the event
-
publishAsync
Description copied from interface:AsyncPublisher
Publishes the supplied event asynchronously and calls theConsumer<T>
when allSubscription
s have been notified.- Specified by:
publishAsync
in interfaceAsyncPublisher<T>
- Parameters:
t
- the eventonFinish
- theConsumer<T>
to be called after allSubscription
s have been notified
-
publishAsync
Description copied from interface:AsyncPublisher
Publishes the supplied event asynchronously and calls theConsumer<T>
when allSubscription
s have been notified. Additionally, this notifies for every exception thrown in the process.- Specified by:
publishAsync
in interfaceAsyncPublisher<T>
- Parameters:
t
- the eventonFinish
- theConsumer
to be called after allSubscription
s have been notifiedonException
- theConsumer
which is notified for each exception thrown
-
iterator
-