java.util.concurrent.Flow.Subscription Java Examples

The following examples show how to use java.util.concurrent.Flow.Subscription. 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 File: JdkFlowAdaptersTest.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
private Publisher<Integer> newMockFlowPublisher(
        BiConsumer<Subscriber<? super Integer>, Subscription> subscriberTerminator) {
    @SuppressWarnings("unchecked")
    Publisher<Integer> flowPublisher = mock(Publisher.class);
    doAnswer(invocation -> {
        Subscriber<? super Integer> subscriber = invocation.getArgument(0);
        Subscription subscription = mock(Subscription.class);
        doAnswer(invocation1 -> {
            subscriberTerminator.accept(subscriber, subscription);
            return null;
        }).when(subscription).request(anyLong());
        subscriber.onSubscribe(subscription);
        return null;
    }).when(flowPublisher).subscribe(any());
    return flowPublisher;
}
 
Example #2
Source File: Subscriber.java    From clouditor with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
  this.subscription = subscription;

  LOGGER.info("Successfully subscribed to publisher.");

  this.subscription.request(1);
}
 
Example #3
Source File: Stream.java    From openjdk-jdk9 with 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 #4
Source File: ProgMainReactiveStreamsWithProcessor.java    From javase with MIT License 5 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
	System.out.println("Subscribed for Freelancer");
	this.subscription = subscription;
	this.subscription.request(1); //requesting data from publisher
	System.out.println("onSubscribe requested 1 item for Freelancer");
}
 
Example #5
Source File: ProgMainReactiveStreamsWithProcessor.java    From javase with MIT License 5 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
	System.out.println("Subscribed");
	this.subscription = subscription;
	this.subscription.request(1); //requesting data from publisher
	System.out.println("onSubscribe requested 1 item");
}
 
Example #6
Source File: BackPressureTest.java    From pgadba with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
  log("Subscribed");
  this.subscription = subscription;

  requestItems(demand);
}
 
Example #7
Source File: LoggingRandomDelaySubscriber.java    From demo-java-x with MIT License 5 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
	log("Subscribed...");
	this.subscription = subscription;
	this.buffer = new AtomicInteger();
	requestItems();
}
 
Example #8
Source File: Examples.java    From pgadba with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
  ds = factory.builder()
          .url("//host.oracle.com:5521/example")
          .username("scott")
          .password("tiger")
          .requestHook(subscription::request)
          .build();
}
 
Example #9
Source File: Examples.java    From pgadba with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
  group = session.<Long, Long>operationGroup()
          .independent()
          .collect(Collectors.summingLong(c -> c));
  group.submit();
  session.requestHook(subscription::request);
}
 
Example #10
Source File: MyFreelancerSubscriber.java    From journaldev with MIT License 5 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
	System.out.println("Subscribed for Freelancer");
	this.subscription = subscription;
	this.subscription.request(1); //requesting data from publisher
	System.out.println("onSubscribe requested 1 item for Freelancer");
}
 
Example #11
Source File: FlowAdapter.java    From java-async-util with 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 #12
Source File: JdkFlowAdaptersTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
private Subscriber<Integer> subscribeToFlowPublisher(final Publisher<Integer> flowPublisher) {
    @SuppressWarnings("unchecked")
    Subscriber<Integer> subscriber = mock(Subscriber.class);
    flowPublisher.subscribe(subscriber);
    ArgumentCaptor<Subscription> subscriptionCaptor = ArgumentCaptor.forClass(Subscription.class);
    verify(subscriber).onSubscribe(subscriptionCaptor.capture());
    subscriptionCaptor.getValue().request(1);
    return subscriber;
}
 
Example #13
Source File: JdkFlowAdaptersTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void toFlowCancel() {
    TestPublisher<Integer> stPublisher = new TestPublisher<>();
    Subscriber<Integer> subscriber = toFlowPublisherAndSubscribe(stPublisher);
    TestSubscription subscription = new TestSubscription();
    stPublisher.onSubscribe(subscription);
    assertThat("Source not subscribed.", stPublisher.isSubscribed(), is(true));
    ArgumentCaptor<Subscription> subscriptionCaptor = ArgumentCaptor.forClass(Subscription.class);
    verify(subscriber).onSubscribe(subscriptionCaptor.capture());
    subscriptionCaptor.getValue().cancel();
    assertThat("Subscription not cancelled.", subscription.isCancelled(), is(true));
}
 
Example #14
Source File: MySubscriber.java    From journaldev with MIT License 5 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
	System.out.println("Subscribed");
	this.subscription = subscription;
	this.subscription.request(1); //requesting data from publisher
	System.out.println("onSubscribe requested 1 item");
}
 
Example #15
Source File: AbstractClient.java    From enmasse with Apache License 2.0 5 votes vote down vote up
private void setLinkAttachedProbe() {
    var linkAttachedProbe = linkAttachedProbeFactory().get();
    executor.setStdErrProcessor(new Subscriber<String>() {

        @Override
        public void onSubscribe(Subscription subscription) {
            //empty
        }

        @Override
        public void onNext(String item) {
            if (!linkAttached.isDone()) {
                if (linkAttachedProbe.test(item)) {
                    log.info("Client is attached!!");
                    linkAttached.complete(null);
                }
            }
        }

        @Override
        public void onError(Throwable throwable) {
            linkAttached.completeExceptionally(throwable);
        }

        @Override
        public void onComplete() {
            linkAttached.complete(null);
        }
    });
}
 
Example #16
Source File: JdkFlowAdaptersTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void toFlowFromSourceCancel() {
    PublisherSource.Subscription srcSubscription = mock(PublisherSource.Subscription.class);
    PublisherSource<Integer> source = s -> s.onSubscribe(srcSubscription);
    Subscriber<Integer> subscriber = toFlowPublisherFromSourceAndSubscribe(source);
    ArgumentCaptor<Subscription> flowSubscriptionCaptor = ArgumentCaptor.forClass(Subscription.class);
    verify(subscriber).onSubscribe(flowSubscriptionCaptor.capture());
    flowSubscriptionCaptor.getValue().cancel();
    verify(srcSubscription).cancel();
}
 
Example #17
Source File: Lesson4.java    From Java-Concurrency-Multithreading-in-Practice with MIT License 5 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
	System.out.println(name + " subscribed!");
	this.subscription = subscription;
	subscription.request(1);
}
 
Example #18
Source File: NumberSubscriber.java    From Reactive-Programming-With-Java-9 with MIT License 4 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
	(this.subscription = subscription).request(1);
}
 
Example #19
Source File: EndSubscriber.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
    this.subscription = subscription;
    subscription.request(1);
}
 
Example #20
Source File: ReactivePost.java    From demo-java-x with MIT License 4 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
	log("Subscription registered: " + subscription);
	subscriber.onSubscribe(new LoggingSubscription(subscription));
}
 
Example #21
Source File: ReactivePost.java    From demo-java-x with MIT License 4 votes vote down vote up
private LoggingSubscription(Subscription subscription) {
	this.subscription = subscription;
}
 
Example #22
Source File: Http2Api.java    From demo-java-x with MIT License 4 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
	this.subscription = subscription;
	this.subscription.request(1);
}
 
Example #23
Source File: BaeldungBatchSubscriberImpl.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
    this.subscription = subscription;
    subscription.request(BUFFER_SIZE);
}
 
Example #24
Source File: ReactivePageSearch.java    From demo-java-x with MIT License 4 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
	this.subscription = subscription;
	requestLine();
}
 
Example #25
Source File: BaeldungSubscriberImpl.java    From tutorials with MIT License 4 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
    this.subscription = subscription;
    subscription.request(1);
}
 
Example #26
Source File: StreamPublisherTest.java    From jenetics with Apache License 2.0 4 votes vote down vote up
@Test
public void publishClosingPublisher() throws InterruptedException {
	final int generations = 20;
	final var publisher = new StreamPublisher<EvolutionResult<IntegerGene, Integer>>();
	final var stream = _engine.stream();

	final var lock = new ReentrantLock();
	final var finished = lock.newCondition();
	final AtomicBoolean running = new AtomicBoolean(true);
	final AtomicBoolean completed = new AtomicBoolean(false);

	final AtomicInteger count = new AtomicInteger();
	publisher.subscribe(new Subscriber<>() {
		private Subscription _subscription;
		@Override
		public void onSubscribe(final Subscription subscription) {
			_subscription = requireNonNull(subscription);
			_subscription.request(1);
		}
		@Override
		public void onNext(final EvolutionResult<IntegerGene, Integer> er) {
			count.incrementAndGet();
			lock.lock();
			try {
				running.set(er.generation() < generations);
				finished.signal();
			} finally {
				lock.unlock();
			}
			_subscription.request(1);
		}
		@Override
		public void onComplete() {
			lock.lock();
			try {
				completed.set(true);
				finished.signalAll();
			} finally {
				lock.unlock();
			}
		}
		@Override
		public void onError(final Throwable throwable) {}
	});

	publisher.attach(stream);

	lock.lock();
	try {
		while (running.get()) {
			finished.await();
		}
	} finally {
		lock.unlock();
	}

	publisher.close();

	lock.lock();
	try {
		while (!completed.get()) {
			finished.await();
		}
	} finally {
		lock.unlock();
	}

	Assert.assertEquals(count.get(), generations);
	Assert.assertTrue(completed.get());
}
 
Example #27
Source File: StreamPublisherTest.java    From jenetics with Apache License 2.0 4 votes vote down vote up
@Test
public void publishLimitedStream() throws InterruptedException {
	final int generations = 20;
	final var publisher = new StreamPublisher<EvolutionResult<IntegerGene, Integer>>();
	final var stream = _engine.stream().limit(generations);

	final var lock = new ReentrantLock();
	final var finished = lock.newCondition();
	final AtomicBoolean running = new AtomicBoolean(true);
	final AtomicBoolean completed = new AtomicBoolean(false);

	final AtomicInteger count = new AtomicInteger();
	publisher.subscribe(new Subscriber<>() {
		private Subscription _subscription;
		@Override
		public void onSubscribe(final Subscription subscription) {
			_subscription = requireNonNull(subscription);
			_subscription.request(1);
		}
		@Override
		public void onNext(final EvolutionResult<IntegerGene, Integer> er) {
			count.incrementAndGet();
			_subscription.request(1);
		}
		@Override
		public void onComplete() {
			lock.lock();
			try {
				running.set(false);
				completed.set(true);
				finished.signal();
			} finally {
				lock.unlock();
			}
		}
		@Override
		public void onError(final Throwable throwable) {}
	});

	publisher.attach(stream);

	lock.lock();
	try {
		while (running.get()) {
			finished.await();
		}
	} finally {
		lock.unlock();
	}

	publisher.close();

	Assert.assertEquals(count.get(), generations);
	Assert.assertTrue(completed.get());
}
 
Example #28
Source File: StreamPublisherTest.java    From jenetics with Apache License 2.0 4 votes vote down vote up
@Test
public void creation() throws InterruptedException {
	final var lock = new ReentrantLock();
	final var finished = lock.newCondition();
	final var running = new AtomicBoolean(true);
	final var generation = new AtomicLong();


	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) {
				_subscription = subscription;
				_subscription.request(1);
			}
			@Override
			public void onNext(final Long g) {
				generation.set(g);
				_subscription.request(1);
			}
			@Override
			public void onError(final Throwable throwable) {
			}
			@Override
			public void onComplete() {
				lock.lock();
				try {
					running.set(false);
					finished.signal();
				} finally {
					lock.unlock();
				}
			}
		});

		publisher.attach(stream);

		lock.lock();
		try {
			while (running.get()) {
				finished.await();
			}
		} finally {
			lock.unlock();
		}
	}

	Assert.assertEquals(generation.get(), 33);
}
 
Example #29
Source File: DefaultSubscriber.java    From jdk9-jigsaw with Creative Commons Zero v1.0 Universal 4 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
	// TODO
}
 
Example #30
Source File: MyProcessor.java    From journaldev with MIT License 4 votes vote down vote up
@Override
public void onSubscribe(Subscription subscription) {
	this.subscription = subscription;
	subscription.request(1);
}