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

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

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

Mono.defer介绍

[英]Create a Mono provider that will Supplier#get a target Mono to subscribe to for each Subscriber downstream.
[中]创建一个Mono提供商,该提供商将为下游的每个订户获得一个要订阅的目标Mono。

代码示例

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

@Override
public Mono<Void> setComplete() {
  return doCommit(() -> Mono.defer(() -> this.writeHandler.apply(Flux.empty())));
}

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

@Override
public Mono<HandlerFunction<ServerResponse>> route(ServerRequest request) {
  return this.first.route(request)
      .map(RouterFunctions::cast)
      .switchIfEmpty(Mono.defer(() -> this.second.route(request).map(RouterFunctions::cast)));
}

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

@Override
public Mono<Void> filter(ServerWebExchange exchange) {
  return Mono.defer(() ->
      this.currentFilter != null && this.next != null ?
          this.currentFilter.filter(exchange, this.next) :
          this.handler.handle(exchange));
}

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

private Mono<DataBuffer> encodeText(CharSequence text, MediaType mediaType, DataBufferFactory bufferFactory) {
  Assert.notNull(mediaType.getCharset(), "Expected MediaType with charset");
  byte[] bytes = text.toString().getBytes(mediaType.getCharset());
  return Mono.defer(() ->
      Mono.just(bufferFactory.allocateBuffer(bytes.length).write(bytes)));
}

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

private <R> Mono<R> createNotFoundError() {
  return Mono.defer(() -> {
    Exception ex = new ResponseStatusException(HttpStatus.NOT_FOUND, "No matching handler");
    return Mono.error(ex);
  });
}

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

@Override
protected Mono<Resource> resolveResourceInternal(@Nullable ServerWebExchange exchange,
    String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
  return chain.resolveResource(exchange, requestPath, locations)
      .switchIfEmpty(Mono.defer(() ->
          resolveVersionedResource(exchange, requestPath, locations, chain)));
}

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

@Override
public Mono<HandlerFunction<T>> route(ServerRequest request) {
  return this.first.route(request)
      .switchIfEmpty(Mono.defer(() -> this.second.route(request)));
}

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

private <T> Mono<ResponseEntity<T>> toEntityInternal(Mono<T> bodyMono) {
  HttpHeaders headers = headers().asHttpHeaders();
  int status = rawStatusCode();
  return bodyMono
      .map(body -> createEntity(body, headers, status))
      .switchIfEmpty(Mono.defer(
          () -> Mono.just(createEntity(headers, status))));
}

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

private Mono<DataBuffer> stringBuffer(String value) {
  return Mono.defer(() -> {
    byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
    DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
    buffer.write(bytes);
    return Mono.just(buffer);
  });
}

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

private Mono<DataBuffer> stringBuffer(String value) {
  return Mono.defer(() -> {
    byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
    DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
    buffer.write(bytes);
    return Mono.just(buffer);
  });
}

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

private Mono<DataBuffer> stringBuffer(String value) {
  return Mono.defer(() -> {
    byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
    DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
    buffer.write(bytes);
    return Mono.just(buffer);
  });
}

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

/**
 * Creates a deferred {@link DataBuffer} containing the given bytes.
 * @param bytes the bytes that are to be stored in the buffer
 * @return the deferred buffer
 */
protected Mono<DataBuffer> dataBuffer(byte[] bytes) {
  return Mono.defer(() -> {
    DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(bytes.length);
    dataBuffer.write(bytes);
    return Mono.just(dataBuffer);
  });
}

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

@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
  return doCommit(() -> Mono.defer(() -> this.writeHandler.apply(Flux.from(body))));
}

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

private Mono<DataBuffer> dataBuffer(Msg msg) {
  return Mono.defer(() -> {
    byte[] bytes = msg.toByteArray();
    DataBuffer buffer = this.bufferFactory.allocateBuffer(bytes.length);
    buffer.write(bytes);
    return Mono.just(buffer);
  });
}

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

@Override
public Mono<WebSession> getSession(ServerWebExchange exchange) {
  return Mono.defer(() -> retrieveSession(exchange)
      .switchIfEmpty(this.sessionStore.createWebSession())
      .doOnNext(session -> exchange.getResponse().beforeCommit(() -> save(exchange, session))));
}

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

Mono<Void> responseMonoVoid(ServerHttpResponse response) {
  return Mono.delay(Duration.ofMillis(100))
      .thenEmpty(Mono.defer(() -> response.writeWith(getBody("body"))));
}

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

Mono<Void> exchangeMonoVoid(ServerWebExchange exchange) {
  return Mono.delay(Duration.ofMillis(100))
      .thenEmpty(Mono.defer(() -> exchange.getResponse().writeWith(getBody("body"))));
}

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

@Test
public void takeUntilByteCountError() {
  Flux<DataBuffer> source = Flux.concat(
      Mono.defer(() -> Mono.just(stringBuffer("foo"))),
      Mono.error(new RuntimeException())
  );
  Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount(source, 5L);
  StepVerifier.create(result)
      .consumeNextWith(stringConsumer("foo"))
      .expectError(RuntimeException.class)
      .verify(Duration.ofSeconds(5));
}

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

@Override
public void encode() throws Exception {
  Flux<DataBuffer> input = Flux.just(this.fooBytes, this.barBytes)
      .flatMap(bytes -> Mono.defer(() -> {
        DataBuffer dataBuffer = this.bufferFactory.allocateBuffer(bytes.length);
        dataBuffer.write(bytes);
        return Mono.just(dataBuffer);
      }));
  testEncodeAll(input, DataBuffer.class, step -> step
      .consumeNextWith(expectBytes(this.fooBytes))
      .consumeNextWith(expectBytes(this.barBytes))
      .verifyComplete());
}

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

@Test  // SPR-16231
public void responseCommitted() {
  Throwable ex = new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Oops");
  this.exchange.getResponse().setStatusCode(HttpStatus.CREATED);
  Mono<Void> mono = this.exchange.getResponse().setComplete()
      .then(Mono.defer(() -> this.handler.handle(this.exchange, ex)));
  StepVerifier.create(mono).consumeErrorWith(actual -> assertSame(ex, actual)).verify();
}

相关文章

Mono类方法