Package io.jenetics.util
Class StreamPublisher<T>
- java.lang.Object
-
- java.util.concurrent.SubmissionPublisher<T>
-
- io.jenetics.util.StreamPublisher<T>
-
- Type Parameters:
T
- the element type of the publisher
- All Implemented Interfaces:
AutoCloseable
,Flow.Publisher<T>
public class StreamPublisher<T> extends SubmissionPublisher<T>
This class allows to create a reactiveFlow.Publisher
from a given JavaStream
.final Stream<Long> stream = engine.stream() .limit(33) .map(EvolutionResult::generation); try (var publisher = new StreamPublisher<Long>()) { publisher.subscribe(new Subscriber<>() { private Subscription subscription; @Override public void onSubscribe(final Subscription subscription) { this.subscription = subscription; this.subscription.request(1); } @Override public void onNext(final Long g) { System.out.println("Got new generation: " + g); subscription.request(1); } @Override public void onError(final Throwable throwable) { } @Override public void onComplete() { System.out.println("Evolution completed."); } }); // Attaching the stream, starts the element publishing. publisher.attach(stream); ... }
- Since:
- 6.0
- Version:
- 6.0
-
-
Constructor Summary
Constructors Constructor Description StreamPublisher()
Creates a new publisher 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 ofFlow.defaultBufferSize()
, and no handler for Subscriber exceptions in method onNext.StreamPublisher(Executor executor, int maxBufferCapacity)
Creates a newStreamPublisher
using the givenExecutor
for async delivery to subscribers, with the given maximum buffer size for each subscriber, and no handler for Subscriber exceptions in methodFlow.Subscriber.onNext(Object)
.StreamPublisher(Executor executor, int maxBufferCapacity, BiConsumer<? super Flow.Subscriber<? super T>,? super Throwable> handler)
Creates a newStreamPublisher
using the givenExecutor
for async delivery to subscribers, with the given maximum buffer size for each subscriber.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
attach(Stream<? extends T> stream)
Attaches the given stream to the publisher.void
close()
Unless already closed, issuesonComplete
signals to current subscribers, and disallows subsequent attempts to publish.-
Methods inherited from class java.util.concurrent.SubmissionPublisher
closeExceptionally, consume, estimateMaximumLag, estimateMinimumDemand, getClosedException, getExecutor, getMaxBufferCapacity, getNumberOfSubscribers, getSubscribers, hasSubscribers, isClosed, isSubscribed, offer, offer, submit, subscribe
-
-
-
-
Constructor Detail
-
StreamPublisher
public StreamPublisher(Executor executor, int maxBufferCapacity, BiConsumer<? super Flow.Subscriber<? super T>,? super Throwable> handler)
Creates a newStreamPublisher
using the givenExecutor
for async delivery to subscribers, with the given maximum buffer size for each subscriber.- Parameters:
executor
- the executor to use for async delivery, supporting creation of at least one independent threadmaxBufferCapacity
- the maximum capacity for each subscriber's bufferhandler
- if non-null, procedure to invoke upon exception thrown in methodonNext
- Throws:
NullPointerException
- if one of the arguments isnull
IllegalArgumentException
- if maxBufferCapacity not positive
-
StreamPublisher
public StreamPublisher(Executor executor, int maxBufferCapacity)
Creates a newStreamPublisher
using the givenExecutor
for async delivery to subscribers, with the given maximum buffer size for each subscriber, and no handler for Subscriber exceptions in methodFlow.Subscriber.onNext(Object)
.- Parameters:
executor
- the executor to use for async delivery, supporting creation of at least one independent threadmaxBufferCapacity
- the maximum capacity for each subscriber's buffer- Throws:
NullPointerException
- if the givenexecutor
isnull
IllegalArgumentException
- if maxBufferCapacity not positive
-
StreamPublisher
public StreamPublisher()
Creates a new publisher 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 ofFlow.defaultBufferSize()
, and no handler for Subscriber exceptions in method onNext.
-
-
Method Detail
-
attach
public void attach(Stream<? extends T> stream)
Attaches the given stream to the publisher. This method automatically starts the publishing of the elements read from the stream.- Parameters:
stream
- thestream
to attach- Throws:
NullPointerException
- if the givenstream
isnull
IllegalStateException
- if a stream is already attached to this publisher
-
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.- Specified by:
close
in interfaceAutoCloseable
- Overrides:
close
in classSubmissionPublisher<T>
-
-