Java Code Examples for reactor.core.publisher.Hooks#onEachOperator()

The following examples show how to use reactor.core.publisher.Hooks#onEachOperator() . 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: FluxAgentIntercept.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
public static void enter() {
  if (inited.get())
    return;

  synchronized (inited) {
    if (inited.get())
      return;

    try {
      Operators.class.getMethod("liftPublisher", BiFunction.class);
    }
    catch (final NoSuchMethodException e) {
      logger.warning("Reactor version is not supported");
      inited.set(true);
      return;
    }

    final Tracer tracer = GlobalTracer.get();
    Hooks.onEachOperator(TracedSubscriber.asOperator(tracer));
    Hooks.onLastOperator(TracedSubscriber.asOperator(tracer));
    inited.set(true);
  }
}
 
Example 2
Source File: FluxTests.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void fluxFromMonoNormalCallsAssemblyHook() {
	final Mono<String> source = Mono.just("monoNormal")
	                                .hide()
	                                .map(i -> i);

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Flux.from(source);
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 3
Source File: FluxTests.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void fluxNextCallableCallsAssemblyHook() {
	Flux<Integer> source = Mono.fromCallable(() -> 1).flux();
	Assertions.assertThat(source) //smoke test that we go into the right case
	          .isInstanceOf(Callable.class)
	          .isNotInstanceOf(Mono.class)
	          .isNotInstanceOf(Fuseable.ScalarCallable.class);

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	source.next();
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 4
Source File: TraceReactorAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
@Override
public void onApplicationEvent(RefreshScopeRefreshedEvent event) {
	if (log.isDebugEnabled()) {
		log.debug("Context refreshed, will reset hooks and then re-register them");
	}
	Hooks.resetOnEachOperator(SLEUTH_TRACE_REACTOR_KEY);
	Hooks.resetOnLastOperator(SLEUTH_TRACE_REACTOR_KEY);
	if (this.reactorProperties.isDecorateOnEach()) {
		if (log.isTraceEnabled()) {
			log.trace("Decorating onEach operator instrumentation");
		}
		Hooks.onEachOperator(SLEUTH_TRACE_REACTOR_KEY,
				scopePassingSpanOperator(this.context));
	}
	else {
		if (log.isTraceEnabled()) {
			log.trace("Decorating onLast operator instrumentation");
		}
		Hooks.onLastOperator(SLEUTH_TRACE_REACTOR_KEY,
				scopePassingSpanOperator(this.context));
	}
}
 
Example 5
Source File: TraceReactorAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
static void setupHooks(ConfigurableApplicationContext springContext) {
	ConfigurableEnvironment environment = springContext.getEnvironment();
	boolean decorateOnEach = environment.getProperty(
			"spring.sleuth.reactor.decorate-on-each", Boolean.class, true);
	if (decorateOnEach) {
		if (log.isTraceEnabled()) {
			log.trace("Decorating onEach operator instrumentation");
		}
		Hooks.onEachOperator(SLEUTH_TRACE_REACTOR_KEY,
				scopePassingSpanOperator(springContext));
	}
	else {
		if (log.isTraceEnabled()) {
			log.trace("Decorating onLast operator instrumentation");
		}
		Hooks.onLastOperator(SLEUTH_TRACE_REACTOR_KEY,
				scopePassingSpanOperator(springContext));
	}
	Schedulers.setExecutorServiceDecorator(
			TraceReactorAutoConfiguration.SLEUTH_REACTOR_EXECUTOR_SERVICE_KEY,
			(scheduler,
					scheduledExecutorService) -> new TraceableScheduledExecutorService(
							springContext, scheduledExecutorService));
}
 
Example 6
Source File: MonoAgentIntercept.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
public static void enter() {
  if (inited.get())
    return;

  synchronized (inited) {
    if (inited.get())
      return;

    try {
      Operators.class.getMethod("liftPublisher", BiFunction.class);
    }
    catch (final NoSuchMethodException e) {
      logger.warning("Reactor version is not supported");
      inited.set(true);
      return;
    }

    final Tracer tracer = GlobalTracer.get();
    Hooks.onEachOperator(TracedSubscriber.asOperator(tracer));
    Hooks.onLastOperator(TracedSubscriber.asOperator(tracer));
    inited.set(true);
  }
}
 
Example 7
Source File: FluxDiscardOnCancelTest.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
@Test
void assemblyHook() {
    List<Object> publishers = new ArrayList<>();
    Hooks.onEachOperator(objectPublisher -> {
        publishers.add(objectPublisher);

        return objectPublisher;
    });

    Iterator<Integer> items = createItems(5);
    Flux<Integer> flux = Flux.fromIterable(() -> items);

    flux.transform(OperatorUtils::discardOnCancel)
        .as(StepVerifier::create)
        .expectNextCount(5)
        .verifyComplete();

    ObjectAssert<?> element = assertThat(publishers).hasSize(2).element(1);

    if (flux instanceof Fuseable) {
        element.isExactlyInstanceOf(FluxDiscardOnCancelFuseable.class);
    } else {
        element.isExactlyInstanceOf(FluxDiscardOnCancel.class);
    }
}
 
Example 8
Source File: FluxTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void fluxFromScalarEmptyCallsAssemblyHook() {
	final Mono<Object> source = Mono.empty();

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Flux.from(source);
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 9
Source File: MonoTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void monoFromMonoDoesntCallAssemblyHook() {
	final Mono<Integer> source = Mono.just(1);

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Mono.from(source);
	Assertions.assertThat(wrappedCount).hasValue(0);
}
 
Example 10
Source File: FluxTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void fluxFromMonoFuseableCallsAssemblyHook() {
	Mono<String> source = Mono.just("monoFuseable").map(i -> i);

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Flux.from(source);
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 11
Source File: MonoTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void monoFromCallableFluxCallsAssemblyHook() {
	final Flux<Integer> source = Flux.just(1);

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Mono.from(source);
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 12
Source File: MonoTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void monoFromPublisherCallsAssemblyHook() {
	final Publisher<Integer> source = TestPublisher.create();
	Assertions.assertThat(source).isNotInstanceOf(Flux.class); //smoke test this is a Publisher

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Mono.from(source);
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 13
Source File: FluxTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void fluxNextScalarErrorCallsAssemblyHook() {
	Flux<Integer> source = Flux.error(new IllegalStateException("boom"));

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	source.next();
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 14
Source File: MonoTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void monoFromDirectFluxWrappingMonoDoesntCallAssemblyHook() {
	final Flux<Integer> source = Flux.from(Mono.just(1).hide());

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Mono.fromDirect(source);
	Assertions.assertThat(wrappedCount).hasValue(0);
}
 
Example 15
Source File: MonoTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void monoFromDirectCallableFluxCallsAssemblyHook() {
	final Flux<Integer> source = Flux.just(1);

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Mono.fromDirect(source);
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 16
Source File: MonoTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void monoFromDirectFluxCallsAssemblyHook() {
	final Flux<Integer> source = Flux.just(1).hide();

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Mono.fromDirect(source);
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 17
Source File: FluxTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void fluxFromFluxSourceDoesntCallAssemblyHook() {
	final Flux<Integer> source = Flux.range(1, 10);

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Flux.from(source);
	Assertions.assertThat(wrappedCount).hasValue(0);
}
 
Example 18
Source File: FluxTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void fluxNextNormalCallsAssemblyHook() {
	Flux<Integer> source = Flux.range(1, 10);

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	source.next();
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 19
Source File: FluxTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void fluxFromScalarErrorCallsAssemblyHook() {
	final Mono<Object> source = Mono.error(new IllegalStateException("scalarError"));

	//set the hook AFTER the original operators have been invoked (since they trigger assembly themselves)
	AtomicInteger wrappedCount = new AtomicInteger();
	Hooks.onEachOperator(p -> {
		wrappedCount.incrementAndGet();
		return p;
	});

	Flux.from(source);
	Assertions.assertThat(wrappedCount).hasValue(1);
}
 
Example 20
Source File: HooksTraceTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Test
public void eachOperatorTest() {
	Hooks.onEachOperator(Operators.lift((sc, sub) ->
			new CoreSubscriber<Object>(){
				@Override
				public void onSubscribe(Subscription s) {
					sub.onSubscribe(s);
				}

				@Override
				public void onNext(Object o) {
					sub.onNext(((Integer)o) + 1);
				}

				@Override
				public void onError(Throwable t) {
					sub.onError(t);
				}

				@Override
				public void onComplete() {
					sub.onComplete();
				}
			}));

	StepVerifier.create(Flux.just(1, 2, 3)
	                        .log()
	                        .log())
	            .expectNext(4, 5, 6)
	            .verifyComplete();

	StepVerifier.create(Mono.just(1)
	                        .log()
	                        .log())
	            .expectNext(4)
	            .verifyComplete();

	StepVerifier.create(ParallelFlux.from(Mono.just(1), Mono.just(1))
	                                .log()
	                                .log())
	            .expectNext(7, 7) //from now counts as an additional one
	            .verifyComplete();
}