use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class CreatingMultiTest method subscription.
@Test
void subscription(SystemOut out) {
Multi<Integer> multi = Multi.createFrom().item(1);
// tag::subscription[]
Cancellable cancellable = multi.subscribe().with(item -> System.out.println(item), failure -> System.out.println("Failed with " + failure), () -> System.out.println("Completed"));
// end::subscription[]
assertThat(cancellable).isNotNull();
assertThat(out.get()).contains("1", "Completed").doesNotContain("Failed");
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class MergeConcatTest method testMergeTicks.
@Test
public void testMergeTicks(SystemOut out) {
// tag::merge-ticks[]
Multi<String> first = Multi.createFrom().ticks().every(Duration.ofMillis(10)).onItem().transform(l -> "Stream 1 - " + l);
Multi<String> second = Multi.createFrom().ticks().every(Duration.ofMillis(15)).onItem().transform(l -> "Stream 2 - " + l);
Multi<String> third = Multi.createFrom().ticks().every(Duration.ofMillis(5)).onItem().transform(l -> "Stream 3 - " + l);
Cancellable cancellable = Multi.createBy().merging().streams(first, second, third).subscribe().with(s -> System.out.println("Got item: " + s));
// end::merge-ticks[]
await().until(() -> out.get().contains("Got item: Stream 3 - 10"));
cancellable.cancel();
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class PollableSourceTest method test.
@Test
public void test(SystemOut out) {
// NOSONAR
// tag::code[]
PollableDataSource source = new PollableDataSource();
// First creates a uni that emit the polled item.
// Because `poll` blocks, let's use a specific executor
Uni<String> pollItemFromSource = Uni.createFrom().item(source::poll).runSubscriptionOn(executor);
// To get the stream of items, just repeat the uni indefinitely
Multi<String> stream = pollItemFromSource.repeat().indefinitely();
Cancellable cancellable = stream.subscribe().with(item -> System.out.println("Polled item: " + item));
// end::code[]
await().until(() -> source.counter.get() >= 4);
// tag::code[]
// ... later ..
// when you don't want the items anymore, cancel the subscription and close the source if needed.
cancellable.cancel();
source.close();
// end::code[]
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-mutiny by smallrye.
the class MultiSubscribeTest method testWith4CallbacksAndCancellation.
@Test
public void testWith4CallbacksAndCancellation() {
List<Integer> items = new CopyOnWriteArrayList<>();
AtomicBoolean completion = new AtomicBoolean();
AtomicReference<Throwable> failure = new AtomicReference<>();
Cancellable cancellable = Multi.createFrom().range(1, 100).subscribe().with(s -> s.request(3), items::add, failure::set, () -> completion.set(true));
cancellable.cancel();
// Called twice
cancellable.cancel();
assertThat(items).contains(1, 2, 3);
assertThat(failure.get()).isNull();
assertThat(completion).isFalse();
}
use of io.smallrye.mutiny.subscription.Cancellable in project smallrye-reactive-messaging by smallrye.
the class ReactiveKafkaBatchConsumerTest method subscribeBatch.
private void subscribeBatch(Multi<IncomingKafkaRecordBatch<Integer, String>> stream, CountDownLatch... latches) throws Exception {
Cancellable cancellable = stream.onItem().invoke(record -> {
onReceive(record);
for (CountDownLatch latch : latches) {
for (KafkaRecord<Integer, String> ignored : record.getRecords()) {
latch.countDown();
}
}
}).subscribe().with(ignored -> {
// Ignored.
});
subscriptions.add(cancellable);
waitForPartitionAssignment();
}
Aggregations