Java Code Examples for reactor.core.publisher.ConnectableFlux#subscribe()

The following examples show how to use reactor.core.publisher.ConnectableFlux#subscribe() . 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: GuideTests.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void advancedConnectable() throws InterruptedException {
	Flux<Integer> source = Flux.range(1, 3)
	                           .doOnSubscribe(s -> System.out.println("subscribed to source"));

	ConnectableFlux<Integer> co = source.publish();

	co.subscribe(System.out::println, e -> {}, () -> {});
	co.subscribe(System.out::println, e -> {}, () -> {});

	System.out.println("done subscribing");
	Thread.sleep(500);
	System.out.println("will now connect");

	co.connect();
}
 
Example 2
Source File: ReactorEssentialsTest.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 5 votes vote down vote up
@Test
public void connectExample() {
    Flux<Integer> source = Flux.range(0, 3)
        .doOnSubscribe(s ->
            log.info("new subscription for the cold publisher"));

    ConnectableFlux<Integer> conn = source.publish();

    conn.subscribe(e -> log.info("[Subscriber 1] onNext: {}", e));
    conn.subscribe(e -> log.info("[Subscriber 2] onNext: {}", e));

    log.info("all subscribers are ready, connecting");
    conn.connect();
}
 
Example 3
Source File: EmployeeHotStreamServiceImpl.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
@Override
public ConnectableFlux<String> freeFlowEmps() {
	 Vector<String> rosterNames = new Vector<>();
	 Function<Employee, String> familyNames = (emp) -> emp.getLastName().toUpperCase();
	 ConnectableFlux<String> flowyNames = Flux.fromIterable(employeeDaoImpl.getEmployees()).log().map(familyNames).cache().publish();
	// flowyNames.subscribe(System.out::println);
	 flowyNames.subscribe(rosterNames::add);
	 System.out.println(rosterNames);
	return flowyNames;
}
 
Example 4
Source File: CustomSourceSnippets.java    From reactor-by-example with MIT License 5 votes vote down vote up
@Test
public void create() throws InterruptedException {
	SomeFeed<PriceTick> feed = new SomeFeed<>();
	Flux<PriceTick> flux =
			Flux.create(emitter ->
			{
				SomeListener listener = new SomeListener() {
					@Override
					public void priceTick(PriceTick event) {
						emitter.next(event);
						if (event.isLast()) {
							emitter.complete();
						}
					}

					@Override
					public void error(Throwable e) {
						emitter.error(e);
					}};
				feed.register(listener);
			}, FluxSink.OverflowStrategy.BUFFER);

	ConnectableFlux<PriceTick> hot = flux.publish();

	hot.subscribe(priceTick -> System.out.printf("%s %4s %6.2f%n", priceTick
			.getDate(), priceTick.getInstrument(), priceTick.getPrice()));

	hot.subscribe(priceTick -> System.out.println(priceTick.getSequence() +
			": " + priceTick.getInstrument()));
	hot.connect();
	Thread.sleep(5000);
}
 
Example 5
Source File: ReactorIntegrationTest.java    From tutorials with MIT License 3 votes vote down vote up
@Test
public void givenConnectableFlux_whenConnected_thenShouldStream() {

    List<Integer> elements = new ArrayList<>();

    final ConnectableFlux<Integer> publish = Flux.just(1, 2, 3, 4).publish();

    publish.subscribe(elements::add);

    assertThat(elements).isEmpty();

    publish.connect();

    assertThat(elements).containsExactly(1, 2, 3, 4);
}