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>
This class allows creating a reactive
Flow.Publisher from a given
Java Stream.
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).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
ConstructorsConstructorDescriptionCreates 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 newStreamPublisherusing the givenExecutorfor 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 newStreamPublisherusing the givenExecutorfor async delivery to subscribers, with the given maximum buffer size for each subscriber. -
Method Summary
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 Details
-
StreamPublisher
public StreamPublisher(Executor executor, int maxBufferCapacity, BiConsumer<? super Flow.Subscriber<? super T>, ? super Throwable> handler) Creates a newStreamPublisherusing the givenExecutorfor 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 isnullIllegalArgumentException- if maxBufferCapacity not positive
-
StreamPublisher
Creates a newStreamPublisherusing the givenExecutorfor 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 givenexecutorisnullIllegalArgumentException- 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 Details
-
attach
Attaches the given stream to the publisher. This method automatically starts the publishing of the elements read from the stream. The attachedstreamis closed, whenthispublisher is closed.- Parameters:
stream- thestreamto attach- Throws:
NullPointerException- if the givenstreamisnullIllegalStateException- if a stream is already attached to this publisher
-
close
Unless already closed, issuesonCompletesignals to current subscribers, and disallows later attempts to publish. Upon return, this method does NOT guarantee that all subscribers have already completed.- Specified by:
closein interfaceAutoCloseable- Overrides:
closein classSubmissionPublisher<T>
-