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

x33g5p2x  于2022-01-25 转载在 其他  
字(7.3k)|赞(0)|评价(0)|浏览(98)

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

Operators.discardLocalAdapter介绍

[英]Create an adapter for local onDiscard hooks that check the element being discarded is of a given Class. The resulting Function adds the hook to the Context, potentially chaining it to an existing hook in the Context.
[中]为本地onDiscard钩子创建一个适配器,用于检查被丢弃的元素是否属于给定的类。结果函数将钩子添加到上下文中,可能会将其链接到上下文中的现有钩子。

代码示例

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

/**
 * Modify the behavior of the <i>whole chain</i> of operators upstream of this one to
 * conditionally clean up elements that get <i>discarded</i> by these operators.
 * <p>
 * The {@code discardHook} must be idempotent and safe to use on any instance of the desired
 * type.
 * Calls to this method are additive, and the order of invocation of the {@code discardHook}
 * is the same as the order of declaration (calling {@code .filter(...).doOnDiscard(first).doOnDiscard(second)}
 * will let the filter invoke {@code first} then {@code second} handlers).
 * <p>
 * Two main categories of discarding operators exist:
 * <ul>
 *     <li>filtering operators, dropping some source elements as part of their designed behavior</li>
 *     <li>operators that prefetch a few elements and keep them around pending a request, but get cancelled/in error</li>
 * </ul>
 * These operators are identified in the javadoc by the presence of an {@code onDiscard Support} section.
 *
 * @param type the {@link Class} of elements in the upstream chain of operators that
 * this cleanup hook should take into account.
 * @param discardHook a {@link Consumer} of elements in the upstream chain of operators
 * that performs the cleanup.
 * @return a {@link Mono} that cleans up matching elements that get discarded upstream of it.
 */
public final <R> Mono<T> doOnDiscard(final Class<R> type, final Consumer<? super R> discardHook) {
  return subscriberContext(Operators.discardLocalAdapter(type, discardHook));
}

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

/**
 * Modify the behavior of the <i>whole chain</i> of operators upstream of this one to
 * conditionally clean up elements that get <i>discarded</i> by these operators.
 * <p>
 * The {@code discardHook} must be idempotent and safe to use on any instance of the desired
 * type.
 * Calls to this method are additive, and the order of invocation of the {@code discardHook}
 * is the same as the order of declaration (calling {@code .filter(...).doOnDiscard(first).doOnDiscard(second)}
 * will let the filter invoke {@code first} then {@code second} handlers).
 * <p>
 * Two main categories of discarding operators exist:
 * <ul>
 *     <li>filtering operators, dropping some source elements as part of their designed behavior</li>
 *     <li>operators that prefetch a few elements and keep them around pending a request, but get cancelled/in error</li>
 * </ul>
 * These operators are identified in the javadoc by the presence of an {@code onDiscard Support} section.
 *
 * @param type the {@link Class} of elements in the upstream chain of operators that
 * this cleanup hook should take into account.
 * @param discardHook a {@link Consumer} of elements in the upstream chain of operators
 * that performs the cleanup.
 * @return a {@link Flux} that cleans up matching elements that get discarded upstream of it.
 */
public final <R> Flux<T> doOnDiscard(final Class<R> type, final Consumer<? super R> discardHook) {
  return subscriberContext(Operators.discardLocalAdapter(type, discardHook));
}

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

@Test
public void discardAdapterIsAdditive() {
  List<String> discardOrder = Collections.synchronizedList(new ArrayList<>(2));
  Function<Context, Context> first = Operators.discardLocalAdapter(Number.class, i -> discardOrder.add("FIRST"));
  Function<Context, Context> second = Operators.discardLocalAdapter(Integer.class, i -> discardOrder.add("SECOND"));
  Context ctx = first.apply(second.apply(Context.empty()));
  Consumer<Object> test = ctx.getOrDefault(Hooks.KEY_ON_DISCARD, o -> {});
  assertThat(test).isNotNull();
  test.accept(1);
  assertThat(discardOrder).as("consumers were combined").containsExactly("FIRST", "SECOND");
}

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

@Test
public void discardAdapterRejectsNull() {
  assertThatNullPointerException().isThrownBy(() -> Operators.discardLocalAdapter(null, obj -> {}))
                  .as("type null check")
                  .withMessage("onDiscard must be based on a type");
  assertThatNullPointerException().isThrownBy(() -> Operators.discardLocalAdapter(String.class, null))
                  .as("discardHook null check")
                  .withMessage("onDiscard must be provided a discardHook Consumer");
}

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

/**
 * Modify the behavior of the <i>whole chain</i> of operators upstream of this one to
 * conditionally clean up elements that get <i>discarded</i> by these operators.
 * <p>
 * The {@code discardHook} must be idempotent and safe to use on any instance of the desired
 * type.
 * Calls to this method are additive, and the order of invocation of the {@code discardHook}
 * is the same as the order of declaration (calling {@code .filter(...).doOnDiscard(first).doOnDiscard(second)}
 * will let the filter invoke {@code first} then {@code second} handlers).
 * <p>
 * Two main categories of discarding operators exist:
 * <ul>
 *     <li>filtering operators, dropping some source elements as part of their designed behavior</li>
 *     <li>operators that prefetch a few elements and keep them around pending a request, but get cancelled/in error</li>
 * </ul>
 * These operators are identified in the javadoc by the presence of an {@code onDiscard Support} section.
 *
 * @param type the {@link Class} of elements in the upstream chain of operators that
 * this cleanup hook should take into account.
 * @param discardHook a {@link Consumer} of elements in the upstream chain of operators
 * that performs the cleanup.
 * @return a {@link Mono} that cleans up matching elements that get discarded upstream of it.
 */
public final <R> Mono<T> doOnDiscard(final Class<R> type, final Consumer<? super R> discardHook) {
  return subscriberContext(Operators.discardLocalAdapter(type, discardHook));
}

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

/**
 * Modify the behavior of the <i>whole chain</i> of operators upstream of this one to
 * conditionally clean up elements that get <i>discarded</i> by these operators.
 * <p>
 * The {@code discardHook} must be idempotent and safe to use on any instance of the desired
 * type.
 * Calls to this method are additive, and the order of invocation of the {@code discardHook}
 * is the same as the order of declaration (calling {@code .filter(...).doOnDiscard(first).doOnDiscard(second)}
 * will let the filter invoke {@code first} then {@code second} handlers).
 * <p>
 * Two main categories of discarding operators exist:
 * <ul>
 *     <li>filtering operators, dropping some source elements as part of their designed behavior</li>
 *     <li>operators that prefetch a few elements and keep them around pending a request, but get cancelled/in error</li>
 * </ul>
 * These operators are identified in the javadoc by the presence of an {@code onDiscard Support} section.
 *
 * @param type the {@link Class} of elements in the upstream chain of operators that
 * this cleanup hook should take into account.
 * @param discardHook a {@link Consumer} of elements in the upstream chain of operators
 * that performs the cleanup.
 * @return a {@link Flux} that cleans up matching elements that get discarded upstream of it.
 */
public final <R> Flux<T> doOnDiscard(final Class<R> type, final Consumer<? super R> discardHook) {
  return subscriberContext(Operators.discardLocalAdapter(type, discardHook));
}

相关文章