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 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 Details

    • StreamPublisher

      public StreamPublisher(Executor executor, int maxBufferCapacity, BiConsumer<? super Flow.Subscriber<? super T>,? super Throwable> handler)
      Creates a new StreamPublisher using the given Executor 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 thread
      maxBufferCapacity - the maximum capacity for each subscriber's buffer
      handler - if non-null, procedure to invoke upon exception thrown in method onNext
      Throws:
      NullPointerException - if one of the arguments is null
      IllegalArgumentException - if maxBufferCapacity not positive
    • StreamPublisher

      public StreamPublisher(Executor executor, int maxBufferCapacity)
      Creates a new StreamPublisher using the given Executor for async delivery to subscribers, with the given maximum buffer size for each subscriber, and no handler for Subscriber exceptions in method Flow.Subscriber.onNext(Object).
      Parameters:
      executor - the executor to use for async delivery, supporting creation of at least one independent thread
      maxBufferCapacity - the maximum capacity for each subscriber's buffer
      Throws:
      NullPointerException - if the given executor is null
      IllegalArgumentException - if maxBufferCapacity not positive
    • StreamPublisher

      public StreamPublisher()
      Creates a new publisher 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 Flow.defaultBufferSize(), and no handler for Subscriber exceptions in method onNext.
  • Method Details

    • 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. The attached stream is closed, when this publisher is closed.
      Parameters:
      stream - the stream to attach
      Throws:
      NullPointerException - if the given stream is null
      IllegalStateException - if a stream is already attached to this publisher
    • close

      public void close()
      Unless already closed, issues onComplete signals to current subscribers, and disallows later attempts to publish. Upon return, this method does NOT guarantee that all subscribers have already completed.
      Specified by:
      close in interface AutoCloseable
      Overrides:
      close in class SubmissionPublisher<T>