public class JerseyPublisher<T> extends java.lang.Object implements Flow.Publisher<T>
Flow.Publisher
corresponding to reactive streams specification.
Delegates to SubmissionPublisher
repackaged from jsr166.
Modifier and Type | Class and Description |
---|---|
static class |
JerseyPublisher.PublisherStrategy |
static class |
JerseyPublisher.SubscriberWrapper<T> |
Modifier and Type | Field and Description |
---|---|
private static int |
DEFAULT_BUFFER_CAPACITY |
private JerseyPublisher.PublisherStrategy |
strategy |
private SubmissionPublisher<T> |
submissionPublisher |
Constructor and Description |
---|
JerseyPublisher()
Creates a new JerseyPublisher using the
ForkJoinPool.commonPool() for async delivery to subscribers
(unless it does not support a parallelism level of at least two, in which case, a new Thread is created to run
each task), with maximum buffer capacity of 256 and default JerseyPublisher.PublisherStrategy ,
which is JerseyPublisher.PublisherStrategy.BEST_EFFORT . |
JerseyPublisher(java.util.concurrent.Executor executor)
Creates a new JerseyPublisher using the given
Executor for async delivery to subscribers, with the default
maximum buffer size of 256 and default JerseyPublisher.PublisherStrategy , which is
JerseyPublisher.PublisherStrategy.BEST_EFFORT . |
JerseyPublisher(java.util.concurrent.Executor executor,
int maxBufferCapacity,
JerseyPublisher.PublisherStrategy strategy)
Creates a new JerseyPublisher using the given
Executor for async delivery to subscribers, with the given
maximum buffer size for each subscriber and given JerseyPublisher.PublisherStrategy . |
JerseyPublisher(java.util.concurrent.Executor executor,
JerseyPublisher.PublisherStrategy strategy)
Creates a new JerseyPublisher using the given
Executor for async delivery to subscribers, with the default
maximum buffer size of 256 and given JerseyPublisher.PublisherStrategy . |
JerseyPublisher(int maxBufferCapacity)
Creates a new JerseyPublisher using the
ForkJoinPool.commonPool() for async delivery to subscribers
(unless it does not support a parallelism level of at least two, in which case, a new Thread is created to run
each task), with specified maximum buffer capacity and default JerseyPublisher.PublisherStrategy , which is
JerseyPublisher.PublisherStrategy.BEST_EFFORT . |
JerseyPublisher(JerseyPublisher.PublisherStrategy strategy)
Creates a new JerseyPublisher using the
ForkJoinPool.commonPool() for async delivery to subscribers
(unless it does not support a parallelism level of at least two, in which case, a new Thread is created to run
each task), with maximum buffer capacity of 256 and given JerseyPublisher.PublisherStrategy . |
Modifier and Type | Method and Description |
---|---|
void |
close()
Unless already closed, issues
onComplete() signals to current subscribers, and disallows subsequent
attempts to publish. |
void |
closeExceptionally(java.lang.Throwable error)
Issues onError signals to current subscribers with the given error, and disallows subsequent attempts to publish.
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
consume(java.util.function.Consumer<? super T> consumer)
Processes all published items using the given Consumer function.
|
int |
estimateMaximumLag()
Returns an estimate of the maximum number of items produced but not yet consumed among all current subscribers.
|
long |
estimateMinimumDemand()
Returns an estimate of the minimum number of items requested but not yet produced, among all current subscribers.
|
java.lang.Throwable |
getClosedException()
Returns the exception associated with
closeExceptionally(java.lang.Throwable) , or null if not closed or if closed normally. |
int |
getMaxBufferCapacity()
Returns the maximum per-subscriber buffer capacity.
|
private JerseyPublisher.SubscriberWrapper |
getSubscriberWrapper(Flow.Subscriber subscriber) |
private int |
offer(T item,
java.util.function.BiPredicate<Flow.Subscriber<? super T>,? super T> onDrop)
Publishes the given item, if possible, to each current subscriber
by asynchronously invoking its
onNext method. |
private int |
offer(T item,
long timeout,
java.util.concurrent.TimeUnit unit,
java.util.function.BiPredicate<Flow.Subscriber<? super T>,? super T> onDrop)
Publishes the given item, if possible, to each current subscriber
by asynchronously invoking its
onNext method,
blocking while resources for any subscription are unavailable,
up to the specified timeout or until the caller thread is
interrupted, at which point the given handler (if non-null) is
invoked, and if it returns true, retried once. |
private boolean |
onDrop(Flow.Subscriber<? super T> subscriber,
T t) |
int |
publish(T item)
Publishes the given item to all current subscribers by invoking its
onNext() method using Executor
provided as constructor parameter (or the default Executor if not provided). |
private int |
submit(T data)
Publishes the given item to each current subscriber by asynchronously invoking its onNext method.
|
void |
subscribe(Flow.Subscriber<? super T> subscriber)
Adds the given Subscriber if possible.
|
private static final int DEFAULT_BUFFER_CAPACITY
private SubmissionPublisher<T> submissionPublisher
private final JerseyPublisher.PublisherStrategy strategy
public JerseyPublisher()
ForkJoinPool.commonPool()
for async delivery to subscribers
(unless it does not support a parallelism level of at least two, in which case, a new Thread is created to run
each task), with maximum buffer capacity of 256 and default JerseyPublisher.PublisherStrategy
,
which is JerseyPublisher.PublisherStrategy.BEST_EFFORT
.public JerseyPublisher(JerseyPublisher.PublisherStrategy strategy)
ForkJoinPool.commonPool()
for async delivery to subscribers
(unless it does not support a parallelism level of at least two, in which case, a new Thread is created to run
each task), with maximum buffer capacity of 256 and given JerseyPublisher.PublisherStrategy
.strategy
- publisher delivering strategypublic JerseyPublisher(java.util.concurrent.Executor executor)
Executor
for async delivery to subscribers, with the default
maximum buffer size of 256 and default JerseyPublisher.PublisherStrategy
, which is
JerseyPublisher.PublisherStrategy.BEST_EFFORT
.executor
- Executor
the executor to use for async delivery,
supporting creation of at least one independent threadjava.lang.NullPointerException
- if executor is nulljava.lang.IllegalArgumentException
- if maxBufferCapacity not positivepublic JerseyPublisher(java.util.concurrent.Executor executor, JerseyPublisher.PublisherStrategy strategy)
Executor
for async delivery to subscribers, with the default
maximum buffer size of 256 and given JerseyPublisher.PublisherStrategy
.executor
- Executor
the executor to use for async delivery,
supporting creation of at least one independent threadstrategy
- publisher delivering strategyjava.lang.NullPointerException
- if executor is nulljava.lang.IllegalArgumentException
- if maxBufferCapacity not positivepublic JerseyPublisher(int maxBufferCapacity)
ForkJoinPool.commonPool()
for async delivery to subscribers
(unless it does not support a parallelism level of at least two, in which case, a new Thread is created to run
each task), with specified maximum buffer capacity and default JerseyPublisher.PublisherStrategy
, which is
JerseyPublisher.PublisherStrategy.BEST_EFFORT
.maxBufferCapacity
- the maximum capacity for each
subscriber's buffer (the enforced capacity may be rounded up to
the nearest power of two and/or bounded by the largest value
supported by this implementation; method getMaxBufferCapacity()
returns the actual value)public JerseyPublisher(java.util.concurrent.Executor executor, int maxBufferCapacity, JerseyPublisher.PublisherStrategy strategy)
Executor
for async delivery to subscribers, with the given
maximum buffer size for each subscriber and given JerseyPublisher.PublisherStrategy
.executor
- Executor
the executor to use for async delivery,
supporting creation of at least one independent threadmaxBufferCapacity
- the maximum capacity for each
subscriber's buffer (the enforced capacity may be rounded up to
the nearest power of two and/or bounded by the largest value
supported by this implementation; method getMaxBufferCapacity()
returns the actual value)strategy
- publisher delivering strategyjava.lang.NullPointerException
- if executor is nulljava.lang.IllegalArgumentException
- if maxBufferCapacity not positivepublic void subscribe(Flow.Subscriber<? super T> subscriber)
Flow.Publisher
onError
method is invoked with an IllegalStateException
.
Otherwise, the Subscriber's onSubscribe
method is
invoked with a new Flow.Subscription
. Subscribers may
enable receiving items by invoking the request
method of this Subscription, and may unsubscribe by
invoking its cancel
method.subscribe
in interface Flow.Publisher<T>
subscriber
- the subscriberprivate int submit(T data)
Blocks uninterruptibly while resources for any subscriber are unavailable.
data
- published datajava.lang.IllegalStateException
- if closedjava.lang.NullPointerException
- if data is nulljava.util.concurrent.RejectedExecutionException
- if thrown by Executorpublic java.util.concurrent.CompletableFuture<java.lang.Void> consume(java.util.function.Consumer<? super T> consumer)
onComplete()
, or completed exceptionally upon any error, or an
exception is thrown by the Consumer, or the returned CompletableFuture is cancelled, in which case no further
items are processed.consumer
- function to process all published dataCompletableFuture
that is completed normally when the publisher signals onComplete,
and exceptionally upon any error or cancellationjava.lang.NullPointerException
- if consumer is nullprivate int offer(T item, java.util.function.BiPredicate<Flow.Subscriber<? super T>,? super T> onDrop)
onNext
method.
The item may be dropped by one or more subscribers if resource
limits are exceeded, in which case the given handler (if non-null)
is invoked, and if it returns true, retried once. Other calls to
methods in this class by other threads are blocked while the
handler is invoked. Unless recovery is assured, options are
usually limited to logging the error and/or issuing an onError
signal to the subscriber.
This method returns a status indicator: If negative, it represents the (negative) number of drops (failed attempts to issue the item to a subscriber). Otherwise it is an estimate of the maximum lag (number of items submitted but not yet consumed) among all current subscribers. This value is at least one (accounting for this submitted item) if there are any subscribers, else zero.
If the Executor for this publisher throws a RejectedExecutionException (or any other RuntimeException or Error) when attempting to asynchronously notify subscribers, or the drop handler throws an exception when processing a dropped item, then this exception is rethrown.
item
- the (non-null) item to publishonDrop
- if non-null, the handler invoked upon a drop to a
subscriber, with arguments of the subscriber and item; if it
returns true, an offer is re-attempted (once)java.lang.IllegalStateException
- if closedjava.lang.NullPointerException
- if item is nulljava.util.concurrent.RejectedExecutionException
- if thrown by Executorprivate int offer(T item, long timeout, java.util.concurrent.TimeUnit unit, java.util.function.BiPredicate<Flow.Subscriber<? super T>,? super T> onDrop)
onNext
method,
blocking while resources for any subscription are unavailable,
up to the specified timeout or until the caller thread is
interrupted, at which point the given handler (if non-null) is
invoked, and if it returns true, retried once. (The drop handler
may distinguish timeouts from interrupts by checking whether
the current thread is interrupted.)
Other calls to methods in this class by other
threads are blocked while the handler is invoked. Unless
recovery is assured, options are usually limited to logging the
error and/or issuing an
onError
signal to the subscriber.
This method returns a status indicator: If negative, it represents the (negative) number of drops (failed attempts to issue the item to a subscriber). Otherwise it is an estimate of the maximum lag (number of items submitted but not yet consumed) among all current subscribers. This value is at least one (accounting for this submitted item) if there are any subscribers, else zero.
If the Executor for this publisher throws a RejectedExecutionException (or any other RuntimeException or Error) when attempting to asynchronously notify subscribers, or the drop handler throws an exception when processing a dropped item, then this exception is rethrown.
item
- the (non-null) item to publishtimeout
- how long to wait for resources for any subscriber
before giving up, in units of unit
unit
- a TimeUnit
determining how to interpret the
timeout
parameteronDrop
- if non-null, the handler invoked upon a drop to a
subscriber, with arguments of the subscriber and item; if it
returns true, an offer is re-attempted (once)java.lang.IllegalStateException
- if closedjava.lang.NullPointerException
- if item is nulljava.util.concurrent.RejectedExecutionException
- if thrown by Executorprivate boolean onDrop(Flow.Subscriber<? super T> subscriber, T t)
private JerseyPublisher.SubscriberWrapper getSubscriberWrapper(Flow.Subscriber subscriber)
public int publish(T item)
onNext() method
using Executor
provided as constructor parameter (or the default Executor
if not provided).
Concrete behaviour is specified by JerseyPublisher.PublisherStrategy
selected upon JerseyPublisher
creation.
item
- the (non-null) item to publish.java.lang.IllegalStateException
- if closedjava.lang.NullPointerException
- if item is nulljava.util.concurrent.RejectedExecutionException
- if thrown by Executor
public void close()
onComplete()
signals to current subscribers, and disallows subsequent
attempts to publish. Upon return, this method does NOT guarantee that all subscribers have yet
completed.public void closeExceptionally(java.lang.Throwable error)
error
- the onError
argument sent to subscribersjava.lang.NullPointerException
- if error is nullpublic int estimateMaximumLag()
public long estimateMinimumDemand()
public java.lang.Throwable getClosedException()
closeExceptionally(java.lang.Throwable)
, or null if not closed or if closed normally.null
public int getMaxBufferCapacity()