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 Subscriptions have been
notified.
This implements Iterable for debugging only. This locks and copies the list of Subscriptions
to perform the operation.-
Constructor Summary
ConstructorsConstructorDescriptionCreates a newConcurrentLockedPublisherwith the suppliedLock.ConcurrentLockedPublisher(Lock lock, Consumer<Runnable> dispatch) Creates a newConcurrentLockedPublisherwith the suppliedLock. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears allSubscriptions and implicitly removes them from the internal pool.iterator()voidPublishes the event synchronously.voidPublishes the supplied event synchronously and calls theConsumerwhen allSubscriptions have been notified.voidPublishes the supplied event synchronously and calls theConsumerwhen allSubscriptions have been notified.voidpublishAsync(T t) Publishes the supplied event asynchronously.voidpublishAsync(T t, Consumer<T> onFinish) Publishes the supplied event asynchronously and calls theConsumer<T>when allSubscriptions have been notified.voidPublishes the supplied event asynchronously and calls theConsumer<T>when allSubscriptions 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, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
ConcurrentLockedPublisher
Creates a newConcurrentLockedPublisherwith 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 newConcurrentLockedPublisherwith 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:PublisherSubscribes to a particular event. The suppliedBiConsumerwill receive zero or more events in the future until the associated call toSubscription.unsubscribe().- Specified by:
subscribein interfacePublisher<T>- Parameters:
consumer- theBiConsumerwhich will accept event- Returns:
- the
Subscription
-
clear
public void clear()Description copied from interface:PublisherClears allSubscriptions 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:PublisherPublishes the event synchronously. -
publish
Description copied from interface:PublisherPublishes the supplied event synchronously and calls theConsumerwhen allSubscriptions have been notified.- Specified by:
publishin interfacePublisher<T>- Parameters:
t- the eventonFinish- theConsumerto be called after allSubscriptions have been notified
-
publish
Description copied from interface:PublisherPublishes the supplied event synchronously and calls theConsumerwhen allSubscriptions have been notified. Additionally, this notifies for every exception thrown in the process. -
publishAsync
Description copied from interface:AsyncPublisherPublishes the supplied event asynchronously.- Specified by:
publishAsyncin interfaceAsyncPublisher<T>- Parameters:
t- the event
-
publishAsync
Description copied from interface:AsyncPublisherPublishes the supplied event asynchronously and calls theConsumer<T>when allSubscriptions have been notified.- Specified by:
publishAsyncin interfaceAsyncPublisher<T>- Parameters:
t- the eventonFinish- theConsumer<T>to be called after allSubscriptions have been notified
-
publishAsync
Description copied from interface:AsyncPublisherPublishes the supplied event asynchronously and calls theConsumer<T>when allSubscriptions have been notified. Additionally, this notifies for every exception thrown in the process.- Specified by:
publishAsyncin interfaceAsyncPublisher<T>- Parameters:
t- the eventonFinish- theConsumerto be called after allSubscriptions have been notifiedonException- theConsumerwhich is notified for each exception thrown
-
iterator
-