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

x33g5p2x  于2022-01-24 转载在 其他  
字(6.0k)|赞(0)|评价(0)|浏览(388)

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

Mono.doOnDiscard介绍

[英]Modify the behavior of the whole chain of operators upstream of this one to conditionally clean up elements that get discarded by these operators.

The 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 discardHookis the same as the order of declaration (calling .filter(...).doOnDiscard(first).doOnDiscard(second)will let the filter invoke first then second handlers).

Two main categories of discarding operators exist:

  • filtering operators, dropping some source elements as part of their designed behavior
  • operators that prefetch a few elements and keep them around pending a request, but get cancelled/in error
    These operators are identified in the javadoc by the presence of an onDiscard Support section.
    [中]修改此操作符上游的整个操作符链的行为,以有条件地清理被这些操作符丢弃的元素。
    DiscardorHook必须是幂等的,并且可以安全地用于所需类型的任何实例。对该方法的调用是加法的,调用DiscardHooke的顺序与声明的顺序相同(调用.filter(…)。杜恩迪斯卡德(第一)。doOnDiscard(第二个)将让过滤器先调用,然后再调用第二个处理程序。
    存在两类主要的报废操作员:
    *筛选操作符,删除一些源元素作为其设计行为的一部分
    *预取一些元素并将它们保留在等待请求的位置,但被取消/出错的运算符
    这些操作符在javadoc中通过onDiscard支持部分的存在来识别。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Return a new {@code DataBuffer} composed from joining together the given
 * {@code dataBuffers} elements. Depending on the {@link DataBuffer} type,
 * the returned buffer may be a single buffer containing all data of the
 * provided buffers, or it may be a zero-copy, composite with references to
 * the given buffers.
 * <p>If {@code dataBuffers} produces an error or if there is a cancel
 * signal, then all accumulated buffers will be
 * {@linkplain #release(DataBuffer) released}.
 * <p>Note that the given data buffers do <strong>not</strong> have to be
 * released. They will be released as part of the returned composite.
 * @param dataBuffers the data buffers that are to be composed
 * @return a buffer that is composed from the {@code dataBuffers} argument
 * @since 5.0.3
 */
public static Mono<DataBuffer> join(Publisher<DataBuffer> dataBuffers) {
  Assert.notNull(dataBuffers, "'dataBuffers' must not be null");
  return Flux.from(dataBuffers)
      .collectList()
      .filter(list -> !list.isEmpty())
      .map(list -> list.get(0).factory().join(list))
      .doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
}

代码示例来源:origin: org.springframework/spring-core

/**
 * Return a new {@code DataBuffer} composed from joining together the given
 * {@code dataBuffers} elements. Depending on the {@link DataBuffer} type,
 * the returned buffer may be a single buffer containing all data of the
 * provided buffers, or it may be a zero-copy, composite with references to
 * the given buffers.
 * <p>If {@code dataBuffers} produces an error or if there is a cancel
 * signal, then all accumulated buffers will be
 * {@linkplain #release(DataBuffer) released}.
 * <p>Note that the given data buffers do <strong>not</strong> have to be
 * released. They will be released as part of the returned composite.
 * @param dataBuffers the data buffers that are to be composed
 * @return a buffer that is composed from the {@code dataBuffers} argument
 * @since 5.0.3
 */
public static Mono<DataBuffer> join(Publisher<DataBuffer> dataBuffers) {
  Assert.notNull(dataBuffers, "'dataBuffers' must not be null");
  return Flux.from(dataBuffers)
      .collectList()
      .filter(list -> !list.isEmpty())
      .map(list -> list.get(0).factory().join(list))
      .doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
}

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

@Test
public void discardLocalOrder() {
  List<String> discardOrder = Collections.synchronizedList(new ArrayList<>(2));
  StepVerifier.create(Mono.just(1)
              .hide() //hide both avoid the fuseable AND tryOnNext usage
              .filter(i -> i % 2 == 0)
              .doOnDiscard(Number.class, i -> discardOrder.add("FIRST"))
              .doOnDiscard(Integer.class, i -> discardOrder.add("SECOND"))
  )
        .expectComplete()
        .verify();
  Assertions.assertThat(discardOrder).containsExactly("FIRST", "SECOND");
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.spring-core

/**
 * Return a new {@code DataBuffer} composed from joining together the given
 * {@code dataBuffers} elements. Depending on the {@link DataBuffer} type,
 * the returned buffer may be a single buffer containing all data of the
 * provided buffers, or it may be a zero-copy, composite with references to
 * the given buffers.
 * <p>If {@code dataBuffers} produces an error or if there is a cancel
 * signal, then all accumulated buffers will be
 * {@linkplain #release(DataBuffer) released}.
 * <p>Note that the given data buffers do <strong>not</strong> have to be
 * released. They will be released as part of the returned composite.
 * @param dataBuffers the data buffers that are to be composed
 * @return a buffer that is composed from the {@code dataBuffers} argument
 * @since 5.0.3
 */
public static Mono<DataBuffer> join(Publisher<DataBuffer> dataBuffers) {
  Assert.notNull(dataBuffers, "'dataBuffers' must not be null");
  return Flux.from(dataBuffers)
      .collectList()
      .filter(list -> !list.isEmpty())
      .map(list -> list.get(0).factory().join(list))
      .doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
}

代码示例来源:origin: apache/servicemix-bundles

/**
 * Return a new {@code DataBuffer} composed from joining together the given
 * {@code dataBuffers} elements. Depending on the {@link DataBuffer} type,
 * the returned buffer may be a single buffer containing all data of the
 * provided buffers, or it may be a zero-copy, composite with references to
 * the given buffers.
 * <p>If {@code dataBuffers} produces an error or if there is a cancel
 * signal, then all accumulated buffers will be
 * {@linkplain #release(DataBuffer) released}.
 * <p>Note that the given data buffers do <strong>not</strong> have to be
 * released. They will be released as part of the returned composite.
 * @param dataBuffers the data buffers that are to be composed
 * @return a buffer that is composed from the {@code dataBuffers} argument
 * @since 5.0.3
 */
public static Mono<DataBuffer> join(Publisher<DataBuffer> dataBuffers) {
  Assert.notNull(dataBuffers, "'dataBuffers' must not be null");
  return Flux.from(dataBuffers)
      .collectList()
      .filter(list -> !list.isEmpty())
      .map(list -> list.get(0).factory().join(list))
      .doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release);
}

相关文章

Mono类方法