java.util.concurrent.Flow Java Examples

The following examples show how to use java.util.concurrent.Flow. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: Java-Concurrency-Multithreading-in-Practice   Author: PacktPublishing   File: Lesson4.java    License: MIT License 7 votes vote down vote up
public static void main(String[] args) {
	SubmissionPublisher<WeatherForecast> weatherForecastPublisher = new WeatherForecastPublisher();

	weatherForecastPublisher.subscribe(new DatabaseSubscriber());
	weatherForecastPublisher.subscribe(new TwitterSubscriber<WeatherForecast>());

	Flow.Processor<WeatherForecast, MetricWeatherForecast> metricConverter = new UsToMetricProcessor();
	weatherForecastPublisher.subscribe(metricConverter);
	metricConverter.subscribe(new TwitterSubscriber<MetricWeatherForecast>());

	// close the publisher and associated resources after 10 seconds
	try {
		TimeUnit.SECONDS.sleep(10);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	weatherForecastPublisher.close();
}
 
Example #2
Source Project: pgadba   Author: pgjdbc   File: PgRowPublisherOperation.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public ParameterizedRowPublisherOperation<R> subscribe(Flow.Subscriber<? super Result.RowColumn> subscriber,
    CompletionStage<? extends R> result) {
  if (result == null) {
    throw new IllegalArgumentException("result is not allowed to be null");
  }
  if (subscriber == null) {
    throw new IllegalArgumentException("subscriber is not allowed to be null");
  }

  publisher.subscribe(subscriber);
  this.result = result;
  result.thenAccept(r -> {
    if (groupSubmission != null) {
      groupSubmission.addGroupResult(r);
    }
    submission.getCompletionStage().toCompletableFuture().complete(r);
  });
  return this;
}
 
Example #3
Source Project: spring-reactive-sample   Author: hantsy   File: PostController.java    License: GNU General Public License v3.0 6 votes vote down vote up
@GetMapping
    public Flow.Publisher<Post> all() {
//        Executor proxyExecutor = (Runnable command)-> ForkJoinPool.commonPool().execute(command);
//        SubmissionPublisher publisher  = new SubmissionPublisher(proxyExecutor, Flow.defaultBufferSize());
//        publisher.submit(new Post(1L, "post one", "content of post one"));
//        publisher.submit(new Post(2L, "post two", "content of post two"));
//
//        return publisher;
        // see: https://stackoverflow.com/questions/46597924/spring-5-supports-java-9-flow-apis-in-its-reactive-feature
        return JdkFlowAdapter.publisherToFlowPublisher(
                Flux.just(
                        new Post(1L, "post one", "content of post one"),
                        new Post(2L, "post two", "content of post two")
                )
        );
    }
 
Example #4
Source Project: Fibry   Author: lucav76   File: TestReactiveSubscribers.java    License: MIT License 5 votes vote down vote up
@Override
public void subscribe(Flow.Subscriber<? super Integer> subscriber) {
    subscriber.onSubscribe(new Flow.Subscription() {
        private final AtomicBoolean completed = new AtomicBoolean(false);
        private final AtomicInteger numMessagesToSend = new AtomicInteger();
        private final Actor<Flow.Subscriber<? super Integer>, Void, Void> actorRefill = ActorSystem.anonymous().newActor(sub -> {
            while (numSent.get() < numMax && numMessagesToSend.get() > 0) {
                subscriber.onNext(numSent.incrementAndGet());
                numMessagesToSend.decrementAndGet();
            }

            if (numSent.get() >= numMax) {
                if (completed.compareAndSet(false, true))
                    subscriber.onComplete();
            }
        });

        @Override
        public void request(long n) {
            if (numSent.get() >= numMax)
                return;

            numMessagesToSend.accumulateAndGet((int) n, Math::max);

            actorRefill.sendMessage(subscriber);
        }

        @Override
        public void cancel() {
            numSent.set(numMax);
        }
    });
}
 
Example #5
Source Project: Java-Concurrency-Multithreading-in-Practice   Author: PacktPublishing   File: Lesson2.java    License: MIT License 5 votes vote down vote up
WeatherForecastPublisher() {
	super(Executors.newFixedThreadPool(2), Flow.defaultBufferSize());
	scheduler = new ScheduledThreadPoolExecutor(1);
	periodicTask = scheduler.scheduleAtFixedRate( //
			// runs submit()
			() -> submit(WeatherForecast.nextRandomWeatherForecast()), //
			500, 500, TimeUnit.MILLISECONDS);
}
 
Example #6
public void onSubscribe(Flow.Subscription subscription) {
    //count = bufferSize - bufferSize / 2;// 当消费一半的时候重新请求
    (this.subscription = subscription).request(bufferSize);
    System.out.println("开始onSubscribe订阅");
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
 
Example #7
Source Project: Hands-On-Reactive-Programming-in-Spring-5   Author: PacktPublishing   File: AdapterExample.java    License: MIT License 5 votes vote down vote up
public static void main(String[] args) {
    Flow.Publisher jdkPublisher = FlowAdapters.toFlowPublisher(new NewsServicePublisher(smp ->
        Flowable.intervalRange(0, 10, 0, 10, TimeUnit.MILLISECONDS, Schedulers.computation())
                .map(e -> NewsLetter.template()
                                    .title(String.valueOf(e))
                                    .digest(Collections.emptyList())
                                    .build())
                .subscribe(smp)
    ));
    Publisher external = FlowAdapters.toPublisher(jdkPublisher);
    Flow.Publisher jdkPublisher2 = FlowAdapters.toFlowPublisher(
            external
    );

    NewsServiceSubscriber newsServiceSubscriber = new NewsServiceSubscriber(2);
    jdkPublisher2.subscribe(FlowAdapters.toFlowSubscriber(newsServiceSubscriber));



    while (true) {
        Optional<NewsLetter> letterOptional = newsServiceSubscriber.eventuallyReadDigest();

        if (letterOptional.isPresent()) {
            NewsLetter letter = letterOptional.get();
            System.out.println(letter);

            if (letter.getTitle().equals("9")) {
                break;
            }
        }
    }
}
 
Example #8
Source Project: java-async-util   Author: IBM   File: FlowAdapter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(final Flow.Subscriber<? super T> subscriber) {
  if ((boolean) SUBSCRIBED_HANDLE.getAndSet(this, true)) {
    subscriber.onError(new IllegalStateException(
        "Publisher " + this + " does not support multiple subscribers"));
    return;
  }
  subscriber.onSubscribe(new IteratorBackedSubscription<>(this.asyncIterator, subscriber));
}
 
Example #9
Source Project: java-async-util   Author: IBM   File: FlowAdapter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(final Flow.Subscription subscription) {
  Objects.requireNonNull(subscription);
  if (this.subscription != null) {
    subscription.cancel();
    return;
  }
  this.subscription = subscription;
}
 
Example #10
Source Project: java-async-util   Author: IBM   File: FlowAdapterTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testDoubleSubscription() throws Throwable {
  final Flow.Publisher<Long> publisher = FlowAdapter.toPublisher(AsyncIterator.range(0, 5));
  final ConsumingSubscriber<Long> s = new ConsumingSubscriber<>();
  publisher.subscribe(s);
  s.join();

  final ConsumingSubscriber<Long> s2 = new ConsumingSubscriber<>();
  try {
    publisher.subscribe(s2);
  } catch (final Throwable e) {
    Assert.fail("failure should be notified via onError, got: " + e);
  }
  FlowAdapterTest.unwrap(s2);
}
 
Example #11
@Override
public Subscriber<Integer> createFlowSubscriber() {
  return new FlowAdapter.SubscribingIterator<Integer>() {
    @Override
    public void onSubscribe(final Flow.Subscription subscription) {
      super.onSubscribe(subscription);
      consume();
    }
  };
}
 
Example #12
Source Project: Java-9-Spring-Webflux   Author: kkTranslation   File: DockerXDemoSubscriber.java    License: Apache License 2.0 5 votes vote down vote up
public void onSubscribe(Flow.Subscription subscription) {
    //count = bufferSize - bufferSize / 2;// 当消费一半的时候重新请求
    (this.subscription = subscription).request(bufferSize);
    System.out.println("开始onSubscribe订阅");
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
 
Example #13
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: DefaultPublisher.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void subscribe(Flow.Subscriber<? super T> subscriber) {
    try {
        subscriber.onSubscribe(new Subscription(subscriber));
    } catch (RejectedExecutionException e) {
        subscriber.onError(new IllegalStateException(e));
    }
}
 
Example #14
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: Stream.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onSubscribe(Flow.Subscription subscription) {
    if (this.subscription != null) {
        throw new IllegalStateException();
    }
    this.subscription = subscription;
    subscription.request(1);
}
 
Example #15
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: Http1Request.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onSubscribe(Flow.Subscription subscription) {
    if (this.subscription != null) {
        throw new IllegalStateException("already subscribed");
    }
    this.subscription = subscription;
    subscription.request(1);
}
 
Example #16
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: Http1Request.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onSubscribe(Flow.Subscription subscription) {
    if (this.subscription != null) {
        throw new IllegalStateException("already subscribed");
    }
    this.subscription = subscription;
    subscription.request(1);
}
 
Example #17
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ResponseProcessors.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onSubscribe(Flow.Subscription subscription) {
    this.subscription = subscription;
    try {
        out = FileChannel.open(file, options);
    } catch (IOException e) {
        result.completeExceptionally(e);
        subscription.cancel();
        return;
    }
    subscription.request(1);
}
 
Example #18
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: ResponseProcessors.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onSubscribe(Flow.Subscription subscription) {
    if (this.subscription != null) {
        subscription.cancel();
        return;
    }
    this.subscription = subscription;
    // We can handle whatever you've got
    subscription.request(Long.MAX_VALUE);
}
 
Example #19
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: RequestProcessors.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public synchronized void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
    if (!(subscriber instanceof ProcessorBase)) {
        throw new UnsupportedOperationException();
    }
    ProcessorBase base = (ProcessorBase)subscriber;
    HttpClientImpl client = base.getClient();
    InputStream is = streamSupplier.get();
    if (is == null) {
        throw new UncheckedIOException(new IOException("no inputstream supplied"));
    }
    this.delegate = new PullPublisher<>(() -> new StreamIterator(is));
    delegate.subscribe(subscriber);
}
 
Example #20
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: SubmissionPublisherTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * A default-constructed SubmissionPublisher has no subscribers,
 * is not closed, has default buffer size, and uses the
 * defaultExecutor
 */
public void testConstructor1() {
    SubmissionPublisher<Integer> p = new SubmissionPublisher<>();
    checkInitialState(p);
    assertEquals(p.getMaxBufferCapacity(), Flow.defaultBufferSize());
    Executor e = p.getExecutor(), c = ForkJoinPool.commonPool();
    if (ForkJoinPool.getCommonPoolParallelism() > 1)
        assertSame(e, c);
    else
        assertNotSame(e, c);
}
 
Example #21
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: HttpInputStreamTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void close() throws IOException {
    synchronized (this) {
        closed = true;
        Flow.Subscription s = subscription;
        if (s != null) {
            s.cancel();
        }
        subscription = null;
    }
    super.close();
}
 
Example #22
Source Project: Fibry   Author: lucav76   File: Actor.java    License: MIT License 4 votes vote down vote up
Flow.Subscriber<T> asReactiveSubscriber(int optimalQueueLength, Consumer<Throwable> onErrorHandler, Consumer<PartialActor<T, S>> onCompleteHandler) {
    AtomicReference<Flow.Subscription> sub = new AtomicReference<>();
    return new Flow.Subscriber<T>() {
        private void askRefill() {
            int messagesRequired = optimalQueueLength - queue.size();

            if (messagesRequired > 0)
                sub.get().request(messagesRequired);
        }

        @Override
        public void onSubscribe(Flow.Subscription subscription) {
            if (sub.get() != null)
                subscription.cancel();
            else {
                sub.set(subscription);
                askRefill();
            }
        }

        @Override
        public void onNext(T item) {
            Objects.requireNonNull(item);

            execAsync(() -> {
                actorLogic.accept(item);
                askRefill();
            });
        }

        @Override
        public void onError(Throwable throwable) {
            Objects.requireNonNull(throwable);
            execAsync(() -> {
                if (onErrorHandler != null)
                    onErrorHandler.accept(throwable);
            });

            sendPoisonPill();
        }

        @Override
        public void onComplete() {
            execAsync(() -> {
                if (onCompleteHandler != null)
                    onCompleteHandler.accept(Actor.this);
            });
            sendPoisonPill();
        }
    };
}
 
Example #23
Source Project: Fibry   Author: lucav76   File: TestReactiveSubscribersBlackBox.java    License: MIT License 4 votes vote down vote up
@Override
public Flow.Subscriber<Integer> createFlowSubscriber() {
    return ActorSystem.anonymous().newActor((Integer n) -> {
    }).asReactiveSubscriber(100, null, null);
}
 
Example #24
Source Project: Fibry   Author: lucav76   File: TestReactiveSubscribersWhiteBox.java    License: MIT License 4 votes vote down vote up
@Override
public Flow.Subscriber<Integer> createFlowSubscriber(final WhiteboxSubscriberProbe<Integer> probe) {
    var realSubscriber = ActorSystem.anonymous().newActor((Integer n) -> {
    }).asReactiveSubscriber(100, null, null);

    return new Flow.Subscriber<Integer>() {
        @Override
        public void onSubscribe(Flow.Subscription subscription) {
            realSubscriber.onSubscribe(subscription);

            // register a successful Subscription, and create a Puppet,
            // for the WhiteboxVerification to be able to drive its tests:
            probe.registerOnSubscribe(new SubscriberPuppet() {
                @Override
                public void triggerRequest(long elements) {
                    subscription.request(elements);
                }

                @Override
                public void signalCancel() {
                    subscription.cancel();
                }
            });
        }

        @Override
        public void onNext(Integer item) {
            realSubscriber.onNext(item);
            probe.registerOnNext(item);
        }

        @Override
        public void onError(Throwable throwable) {
            realSubscriber.onError(throwable);
            probe.registerOnError(throwable);
        }

        @Override
        public void onComplete() {
            realSubscriber.onComplete();
            probe.registerOnComplete();
        }
    };
}
 
Example #25
Source Project: Java-Concurrency-Multithreading-in-Practice   Author: PacktPublishing   File: Lesson4.java    License: MIT License 4 votes vote down vote up
@Override
public void onSubscribe(Flow.Subscription subscription) {
	this.subscription = subscription;
	subscription.request(1);
}
 
Example #26
Source Project: Java-programming-methodology-Rxjava-articles   Author: muyinchen   File: StockMaintain.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void onSubscribe(Flow.Subscription subscription) {
    log.info("******调用 onSubscribe******");
    subscription.request(3);
    this.subscription = subscription;
}
 
Example #27
public Flow.Subscription getSubscription() {
    return subscription;
}
 
Example #28
Source Project: java-async-util   Author: IBM   File: FlowAdapter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void subscribe(final Flow.Subscriber<? super T> subscriber) {
  subscriber
      .onSubscribe(
          new IteratorBackedSubscription<>(this.asyncIteratorSupplier.get(), subscriber));
}
 
Example #29
Source Project: java-async-util   Author: IBM   File: FlowAdapter.java    License: Apache License 2.0 4 votes vote down vote up
SubscribingIterator(final Flow.Publisher<? extends T> publisher) {
  this.publisher = Objects.requireNonNull(publisher);
}
 
Example #30
Source Project: java-async-util   Author: IBM   File: FlowAdapter.java    License: Apache License 2.0 4 votes vote down vote up
IteratorBackedSubscription(final AsyncIterator<T> iterator,
    final Flow.Subscriber<? super T> subscriber) {
  this.iterator = Objects.requireNonNull(iterator);
  this.subscriber = Objects.requireNonNull(subscriber);
}