reactor.core.publisher.Hooks.onOperatorError()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(275)

本文整理了Java中reactor.core.publisher.Hooks.onOperatorError()方法的一些代码示例,展示了Hooks.onOperatorError()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Hooks.onOperatorError()方法的具体详情如下:
包路径:reactor.core.publisher.Hooks
类名称:Hooks
方法名:onOperatorError

Hooks.onOperatorError介绍

[英]Add or replace a named custom error mapping, overriding the default one. Custom mapping can be an accumulation of several sub-hooks each subsequently added via this method.

Note that invoking this method twice with the same key will replace the old sub-hook with that name, but keep the execution order (eg. A-h1, B-h2, A-h3 will keep A-B execution order, leading to hooks h3 then h2 being executed). Removing a particular key using #resetOnOperatorError(String) then adding it back will result in the execution order changing (the later sub-hook being executed last). Can be fully reset via #resetOnOperatorError().

For reference, the default mapping is to unwrap the exception and, if the second parameter is another exception, to add it to the first as a suppressed.
[中]添加或替换命名的自定义错误映射,覆盖默认错误映射。自定义映射可以是几个子钩子的累积,每个子钩子随后通过此方法添加。
请注意,使用相同的键调用此方法两次将用该名称替换旧的子钩子,但保留执行顺序(例如,A-h1、B-h2、A-h3将保留A-B执行顺序,导致钩子h3然后执行h2)。使用#resetOnOperatorError(String)删除一个特定的键,然后将其添加回去,将导致执行顺序发生变化(最后执行的是后面的子钩子)。可通过#resetOnOperatorError()完全重置。
作为参考,默认映射是展开异常,如果第二个参数是另一个异常,则将其添加到第一个异常中作为抑制的。

代码示例

代码示例来源:origin: reactor/reactor-core

/**
 * Add a custom error mapping, overriding the default one. Custom mapping can be an
 * accumulation of several sub-hooks each subsequently added via this method.
 * <p>
 * Note that sub-hooks are cumulative, but invoking this method twice with the same
 * instance (or any instance that has the same `toString`) will result in only a single
 * instance being applied. See {@link #onOperatorError(String, BiFunction)} for a variant
 * that allows you to name the sub-hooks (and thus replace them or remove them individually
 * later on). Can be fully reset via {@link #resetOnOperatorError()}.
 * <p>
 * For reference, the default mapping is to unwrap the exception and, if the second
 * parameter is another exception, to add it to the first as suppressed.
 *
 * @param onOperatorError an operator error {@link BiFunction} mapper, returning an arbitrary exception
 * given the failure and optionally some original context (data or error).
 *
 * @see #onOperatorError(String, BiFunction)
 * @see #resetOnOperatorError(String)
 * @see #resetOnOperatorError()
 */
public static void onOperatorError(BiFunction<? super Throwable, Object, ? extends Throwable> onOperatorError) {
  onOperatorError(onOperatorError.toString(), onOperatorError);
}

代码示例来源:origin: reactor/reactor-core

private void plugHooks() {
  Hooks.onErrorDropped(droppedErrors::offer);
  Hooks.onNextDropped(droppedElements::offer);
  Hooks.onOperatorError((t, d) -> {
    operatorErrors.offer(Tuples.of(Optional.ofNullable(t), Optional.ofNullable(d)));
    return t;
  });
}

代码示例来源:origin: reactor/reactor-core

@Test
public void onOperatorErrorSameLambdaSameNameAppliedOnce() {
  AtomicInteger applied = new AtomicInteger();
  BiFunction<Throwable, Object, Throwable> hook = (t, s) -> {
    applied.incrementAndGet();
    return t;
  };
  Hooks.onOperatorError(hook);
  Hooks.onOperatorError(hook);
  Hooks.onOperatorErrorHook.apply(new IllegalStateException("boom"), "foo");
  assertThat(applied.get()).isEqualTo(1);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void onOperatorErrorSameLambdaDifferentNamesAppliedTwice() {
  AtomicInteger applied = new AtomicInteger();
  BiFunction<Throwable, Object, Throwable> hook = (t, s) -> {
    applied.incrementAndGet();
    return t;
  };
  Hooks.onOperatorError(hook);
  Hooks.onOperatorError("other", hook);
  Hooks.onOperatorErrorHook.apply(new IllegalStateException("boom"), "foo");
  assertThat(applied.get()).isEqualTo(2);
}

代码示例来源:origin: reactor/reactor-core

@Before
public void before() {
  this.droppedValue = null;
  this.hookCapturedError = null;
  this.hookCapturedValue = null;
  Hooks.onOperatorError(this);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void onOperatorErrorOneHookNoComposite() {
  BiFunction<Throwable, Object, Throwable> hook = (t, s) -> t;
  Hooks.onOperatorError(hook);
  assertThat(Hooks.onOperatorErrorHook).isSameAs(hook);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void onOperatorErrorResetSpecific() {
  List<String> applied = new ArrayList<>(3);
  BiFunction<Throwable, Object, Throwable> hook1 = (t, s) -> {
    applied.add("h1");
    return t;
  };
  BiFunction<Throwable, Object, Throwable> hook2 = (t, s) -> {
    applied.add("h2");
    return t;
  };
  Hooks.onOperatorError("1", hook1);
  Hooks.onOperatorError(hook2);
  Hooks.onOperatorErrorHook.apply(new IllegalStateException("boom"), "foo");
  assertThat(Hooks.getOnOperatorErrorHooks()).hasSize(2);
  assertThat(applied).containsExactly("h1", "h2");
  applied.clear();
  Hooks.resetOnOperatorError("1");
  Hooks.onOperatorErrorHook.apply(new IllegalStateException("boom2"), "bar");
  assertThat(Hooks.getOnOperatorErrorHooks()).hasSize(1);
  assertThat(applied).containsExactly("h2");
}

代码示例来源:origin: reactor/reactor-core

};
Hooks.onOperatorError("1", hook1);
Hooks.onOperatorError("2", hook2);
Hooks.onOperatorErrorHook.apply(new IllegalStateException("boom"), "foo");
Hooks.onOperatorError("1", hook3);
Hooks.onOperatorErrorHook.apply(new IllegalStateException("boom2"), "bar");

代码示例来源:origin: reactor/reactor-core

@Before
public void setUp() {
  scheduler = new BoundedScheduler(Schedulers.newSingle("bounded-single"));
  Hooks.onNextDropped(o -> onNextDropped.add(o));
  Hooks.onErrorDropped(e -> onErrorDropped.add(e));
  Hooks.onOperatorError((e, o) -> {
    onOperatorError.add(e);
    if (o instanceof Long)
      onOperatorErrorData.add((Long) o);
    else if (o != null) {
      System.out.println(o);
    }
    return e;
  });
  Schedulers.onHandleError((thread, t) -> onSchedulerHandleError.add(t));
}

代码示例来源:origin: reactor/reactor-core

@Test
public void onOperatorErrorReset() {
  Hooks.onOperatorError("some", (t, v) -> t);
  assertThat(Hooks.onOperatorErrorHook).isNotNull();
  assertThat(Hooks.getOnOperatorErrorHooks()).hasSize(1);
  Hooks.resetOnOperatorError();
  assertThat(Hooks.onOperatorErrorHook).isNull();
  assertThat(Hooks.getOnOperatorErrorHooks()).isEmpty();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void onOperatorErrorClearByName() {
  Hooks.onOperatorError("some", (t, v) -> t);
  Hooks.onOperatorError("other", (t, v) -> t);
  assertThat(Hooks.onOperatorErrorHook).isNotNull();
  assertThat(Hooks.getOnOperatorErrorHooks()).hasSize(2);
  Hooks.resetOnOperatorError("some");
  assertThat(Hooks.onOperatorErrorHook).isNotNull();
  assertThat(Hooks.getOnOperatorErrorHooks())
      .hasSize(1)
      .containsOnlyKeys("other");
  Hooks.resetOnOperatorError("other");
  assertThat(Hooks.onOperatorErrorHook).isNull();
  assertThat(Hooks.getOnOperatorErrorHooks()).isEmpty();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void onOperatorError() {
  AtomicReference<Object> errorValue = new AtomicReference<Object>();
  Hooks.onOperatorError((error, d) -> {
    errorValue.set(d);
    return error;
  });
  Flux<Integer> f1 = Mono.just(1).flatMapMany(i -> Flux.error(new Exception("test")));
  StepVerifier.create(f1).verifyErrorMessage("test");
  assertThat(errorValue.get()).isEqualTo(1);
  Flux<Integer> f2 = Mono.just(2).flatMapMany(i -> {
    throw new RuntimeException("test");
  });
  StepVerifier.create(f2).verifyErrorMessage("test");
  assertThat(errorValue.get()).isEqualTo(2);
  Flux<Integer> f3 = Flux.just(3, 6, 9).flatMap(i -> Flux.error(new Exception("test")));
  StepVerifier.create(f3).verifyErrorMessage("test");
  assertThat(errorValue.get()).isEqualTo(3);
  Flux<Integer> f4 = Flux.just(4, 8, 12).flatMap(i -> {
    throw new RuntimeException("test");
  });
  StepVerifier.create(f4).verifyErrorMessage("test");
  assertThat(errorValue.get()).isEqualTo(4);
  Hooks.resetOnOperatorError();
}

代码示例来源:origin: reactor/reactor-core

Hooks.onOperatorError((t, d) -> {
  throwableInOnOperatorError.set(t);
  dataInOnOperatorError.set(d);

代码示例来源:origin: reactor/reactor-core

@Test
public void errorPropagated() {
  int data = 1;
  IllegalStateException exception = new IllegalStateException();
  final AtomicReference<Throwable> throwableInOnOperatorError =
      new AtomicReference<>();
  final AtomicReference<Object> dataInOnOperatorError = new AtomicReference<>();
  Hooks.onOperatorError((t, d) -> {
    throwableInOnOperatorError.set(t);
    dataInOnOperatorError.set(d);
    return t;
  });
  AssertSubscriber<Integer> ts = AssertSubscriber.create();
  Flux.just(data).<Integer>handle((v, s) -> {
    throw exception;
  }).subscribe(ts);
  ts.await()
   .assertNoValues()
   .assertError(IllegalStateException.class)
   .assertNotComplete();
  Assert.assertSame(throwableInOnOperatorError.get(), exception);
  Assert.assertSame(dataInOnOperatorError.get(), data);
}

代码示例来源:origin: reactor/reactor-core

Hooks.onOperatorError((t, d) -> {
  throwableInOnOperatorError.set(t);
  dataInOnOperatorError.set(d);

代码示例来源:origin: reactor/reactor-core

@Test
public void errorSignal() {
  int data = 1;
  Exception exception = new IllegalStateException();
  final AtomicReference<Throwable> throwableInOnOperatorError =
      new AtomicReference<>();
  final AtomicReference<Object> dataInOnOperatorError = new AtomicReference<>();
  Hooks.onOperatorError((t, d) -> {
    throwableInOnOperatorError.set(t);
    dataInOnOperatorError.set(d);
    return t;
  });
  AssertSubscriber<Integer> ts = AssertSubscriber.create();
  Flux.just(data).
      <Integer>handle((v, s) -> s.error(exception))
      .subscribe(ts);
  ts.await()
   .assertNoValues()
   .assertError(IllegalStateException.class)
   .assertNotComplete();
  Assert.assertSame(throwableInOnOperatorError.get(), exception);
  Assert.assertSame(dataInOnOperatorError.get(), data);
}

代码示例来源:origin: reactor/reactor-core

Hooks.onOperatorError((t, d) -> {
  throwableInOnOperatorError.set(t);
  dataInOnOperatorError.set(d);

代码示例来源:origin: reactor/reactor-core

Hooks.onOperatorError((t, d) -> {
  throwableInOnOperatorError.set(t);
  dataInOnOperatorError.set(d);

代码示例来源:origin: reactor/reactor-core

@Test
public void errorHooks() throws Exception {
  Hooks.onOperatorError((e, s) -> new TestException(s.toString()));
  Hooks.onNextDropped(d -> {
    throw new TestException(d.toString());

代码示例来源:origin: reactor/reactor-core

Hooks.onOperatorError((t, d) -> {
  assertTrue(t instanceof RejectedExecutionException);
  assertTrue(d != null);

相关文章