Class JerseyPublisher<T>
- java.lang.Object
-
- org.glassfish.jersey.internal.util.JerseyPublisher<T>
-
- All Implemented Interfaces:
Flow.Publisher<T>
public class JerseyPublisher<T> extends Object implements Flow.Publisher<T>
Implementation ofFlow.Publishercorresponding to reactive streams specification.Delegates to
SubmissionPublisherrepackaged from jsr166 on JDK 8 or to JDKSubmissionPublisher.- Author:
- Adam Lindenthal
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJerseyPublisher.PublisherStrategyclassJerseyPublisher.SubscriberWrapper<T>
-
Constructor Summary
Constructors Constructor Description JerseyPublisher()Creates a new JerseyPublisher using theForkJoinPool.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 defaultJerseyPublisher.PublisherStrategy, which isJerseyPublisher.PublisherStrategy.BEST_EFFORT.JerseyPublisher(int maxBufferCapacity)Creates a new JerseyPublisher using theForkJoinPool.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 defaultJerseyPublisher.PublisherStrategy, which isJerseyPublisher.PublisherStrategy.BEST_EFFORT.JerseyPublisher(Executor executor)Creates a new JerseyPublisher using the givenExecutorfor async delivery to subscribers, with the default maximum buffer size of 256 and defaultJerseyPublisher.PublisherStrategy, which isJerseyPublisher.PublisherStrategy.BEST_EFFORT.JerseyPublisher(Executor executor, int maxBufferCapacity, JerseyPublisher.PublisherStrategy strategy)Creates a new JerseyPublisher using the givenExecutorfor async delivery to subscribers, with the given maximum buffer size for each subscriber and givenJerseyPublisher.PublisherStrategy.JerseyPublisher(Executor executor, JerseyPublisher.PublisherStrategy strategy)Creates a new JerseyPublisher using the givenExecutorfor async delivery to subscribers, with the default maximum buffer size of 256 and givenJerseyPublisher.PublisherStrategy.JerseyPublisher(JerseyPublisher.PublisherStrategy strategy)Creates a new JerseyPublisher using theForkJoinPool.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 givenJerseyPublisher.PublisherStrategy.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Unless already closed, issuesonComplete()signals to current subscribers, and disallows subsequent attempts to publish.voidclose(boolean cascading)Same asclose()but with control as to whether registered subscribers should be closed or not.voidcloseExceptionally(Throwable error)Issues onError signals to current subscribers with the given error, and disallows subsequent attempts to publish.CompletableFuture<Void>consume(Consumer<? super T> consumer)Processes all published items using the given Consumer function.intestimateMaximumLag()Returns an estimate of the maximum number of items produced but not yet consumed among all current subscribers.longestimateMinimumDemand()Returns an estimate of the minimum number of items requested but not yet produced, among all current subscribers.ThrowablegetClosedException()Returns the exception associated withcloseExceptionally(java.lang.Throwable), or null if not closed or if closed normally.intgetMaxBufferCapacity()Returns the maximum per-subscriber buffer capacity.intpublish(T item)Publishes the given item to all current subscribers by invoking itsonNext() methodusingExecutorprovided as constructor parameter (or the defaultExecutorif not provided).voidsubscribe(Flow.Subscriber<? super T> subscriber)Adds the given Subscriber if possible.
-
-
-
Constructor Detail
-
JerseyPublisher
public JerseyPublisher()
Creates a new JerseyPublisher using theForkJoinPool.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 defaultJerseyPublisher.PublisherStrategy, which isJerseyPublisher.PublisherStrategy.BEST_EFFORT.
-
JerseyPublisher
public JerseyPublisher(JerseyPublisher.PublisherStrategy strategy)
Creates a new JerseyPublisher using theForkJoinPool.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 givenJerseyPublisher.PublisherStrategy.- Parameters:
strategy- publisher delivering strategy
-
JerseyPublisher
public JerseyPublisher(Executor executor)
Creates a new JerseyPublisher using the givenExecutorfor async delivery to subscribers, with the default maximum buffer size of 256 and defaultJerseyPublisher.PublisherStrategy, which isJerseyPublisher.PublisherStrategy.BEST_EFFORT.- Parameters:
executor-Executorthe executor to use for async delivery, supporting creation of at least one independent thread- Throws:
NullPointerException- if executor is nullIllegalArgumentException- if maxBufferCapacity not positive
-
JerseyPublisher
public JerseyPublisher(Executor executor, JerseyPublisher.PublisherStrategy strategy)
Creates a new JerseyPublisher using the givenExecutorfor async delivery to subscribers, with the default maximum buffer size of 256 and givenJerseyPublisher.PublisherStrategy.- Parameters:
executor-Executorthe executor to use for async delivery, supporting creation of at least one independent threadstrategy- publisher delivering strategy- Throws:
NullPointerException- if executor is nullIllegalArgumentException- if maxBufferCapacity not positive
-
JerseyPublisher
public JerseyPublisher(int maxBufferCapacity)
Creates a new JerseyPublisher using theForkJoinPool.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 defaultJerseyPublisher.PublisherStrategy, which isJerseyPublisher.PublisherStrategy.BEST_EFFORT.- Parameters:
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; methodgetMaxBufferCapacity()returns the actual value)
-
JerseyPublisher
public JerseyPublisher(Executor executor, int maxBufferCapacity, JerseyPublisher.PublisherStrategy strategy)
Creates a new JerseyPublisher using the givenExecutorfor async delivery to subscribers, with the given maximum buffer size for each subscriber and givenJerseyPublisher.PublisherStrategy.- Parameters:
executor-Executorthe 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; methodgetMaxBufferCapacity()returns the actual value)strategy- publisher delivering strategy- Throws:
NullPointerException- if executor is nullIllegalArgumentException- if maxBufferCapacity not positive
-
-
Method Detail
-
subscribe
public void subscribe(Flow.Subscriber<? super T> subscriber)
Description copied from interface:Flow.PublisherAdds the given Subscriber if possible. If already subscribed, or the attempt to subscribe fails due to policy violations or errors, the Subscriber'sonErrormethod is invoked with anIllegalStateException. Otherwise, the Subscriber'sonSubscribemethod is invoked with a newFlow.Subscription. Subscribers may enable receiving items by invoking therequestmethod of this Subscription, and may unsubscribe by invoking itscancelmethod.- Specified by:
subscribein interfaceFlow.Publisher<T>- Parameters:
subscriber- the subscriber
-
consume
public CompletableFuture<Void> consume(Consumer<? super T> consumer)
Processes all published items using the given Consumer function. Returns a CompletableFuture that is completed normally when this publisher signalsonComplete(), 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.- Parameters:
consumer- function to process all published data- Returns:
- a
CompletableFuturethat is completed normally when the publisher signals onComplete, and exceptionally upon any error or cancellation - Throws:
NullPointerException- if consumer is null
-
publish
public int publish(T item)
Publishes the given item to all current subscribers by invoking itsonNext() methodusingExecutorprovided as constructor parameter (or the defaultExecutorif not provided).Concrete behaviour is specified by
JerseyPublisher.PublisherStrategyselected uponJerseyPublishercreation.- Parameters:
item- the (non-null) item to publish.- Returns:
- if negative, the (negative) number of drops; otherwise an estimate of maximum lag.
- Throws:
IllegalStateException- if closedNullPointerException- if item is nullRejectedExecutionException- if thrown byExecutor
-
close
public void close()
Unless already closed, issuesonComplete()signals to current subscribers, and disallows subsequent attempts to publish. Upon return, this method does NOT guarantee that all subscribers have yet completed.
-
close
public void close(boolean cascading)
Same asclose()but with control as to whether registered subscribers should be closed or not.- Parameters:
cascading- Boolean controlling whether to close subscribers or not.
-
closeExceptionally
public void closeExceptionally(Throwable error)
Issues onError signals to current subscribers with the given error, and disallows subsequent attempts to publish.- Parameters:
error- theonErrorargument sent to subscribers- Throws:
NullPointerException- if error is null
-
estimateMaximumLag
public int estimateMaximumLag()
Returns an estimate of the maximum number of items produced but not yet consumed among all current subscribers.- Returns:
- estimated maximum lag
-
estimateMinimumDemand
public long estimateMinimumDemand()
Returns an estimate of the minimum number of items requested but not yet produced, among all current subscribers.- Returns:
- estimated minimum demand
-
getClosedException
public Throwable getClosedException()
Returns the exception associated withcloseExceptionally(java.lang.Throwable), or null if not closed or if closed normally.- Returns:
- exception thrown on closing or
null
-
getMaxBufferCapacity
public int getMaxBufferCapacity()
Returns the maximum per-subscriber buffer capacity.- Returns:
- the maximum per-subscriber buffer capacity
-
-