com.linecorp.armeria.common.HttpResponse.completionFuture()方法的使用及代码示例

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

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

HttpResponse.completionFuture介绍

暂无

代码示例

代码示例来源:origin: line/armeria

@Override
default CompletableFuture<Void> closeFuture() {
  return completionFuture();
}

代码示例来源:origin: line/armeria

ArmeriaHttpClientResponseSubscriber(HttpResponse httpResponse) {
  httpResponse.completionFuture().whenComplete(this);
  httpResponse.subscribe(this, eventLoop);
}

代码示例来源:origin: line/armeria

private static HttpResponse respond(CompletableFuture<HttpResponse> future, Disposable disposable) {
    final HttpResponse response = HttpResponse.from(future);
    response.completionFuture().exceptionally(cause -> {
      disposable.dispose();
      return null;
    });
    return response;
  }
}

代码示例来源:origin: line/armeria

/**
 * Closes the {@link HttpResponseWriter} if it is opened.
 */
private Mono<Void> cleanup(@Nullable Throwable cause) {
  if (future.isDone()) {
    return Mono.empty();
  }
  if (cause != null) {
    future.completeExceptionally(cause);
    logger.debug("{} Response future has been completed with a cause", ctx, cause);
    return Mono.empty();
  }
  final HttpResponse response = HttpResponse.of(headers);
  future.complete(response);
  logger.debug("{} Response future has been completed with an HttpResponse", ctx);
  return Mono.fromFuture(response.completionFuture());
}

代码示例来源:origin: line/armeria

/**
 * Aggregates this response. The returned {@link CompletableFuture} will be notified when the content and
 * the trailing headers of the response are received fully.
 */
default CompletableFuture<AggregatedHttpMessage> aggregate() {
  final CompletableFuture<AggregatedHttpMessage> future = new CompletableFuture<>();
  final HttpResponseAggregator aggregator = new HttpResponseAggregator(future, null);
  completionFuture().handle(aggregator);
  subscribe(aggregator);
  return future;
}

代码示例来源:origin: line/armeria

/**
 * Aggregates this response. The returned {@link CompletableFuture} will be notified when the content and
 * the trailing headers of the response are received fully. {@link AggregatedHttpMessage#content()} will
 * return a pooled object, and the caller must ensure to release it. If you don't know what this means,
 * use {@link #aggregate()}.
 */
default CompletableFuture<AggregatedHttpMessage> aggregateWithPooledObjects(ByteBufAllocator alloc) {
  requireNonNull(alloc, "alloc");
  final CompletableFuture<AggregatedHttpMessage> future = new CompletableFuture<>();
  final HttpResponseAggregator aggregator = new HttpResponseAggregator(future, alloc);
  completionFuture().handle(aggregator);
  subscribe(aggregator, true);
  return future;
}

代码示例来源:origin: line/armeria

/**
   * Aggregates this response. The returned {@link CompletableFuture} will be notified when the content and
   * the trailing headers of the request is received fully. {@link AggregatedHttpMessage#content()} will
   * return a pooled object, and the caller must ensure to release it. If you don't know what this means,
   * use {@link #aggregate()}.
   */
  default CompletableFuture<AggregatedHttpMessage> aggregateWithPooledObjects(
      EventExecutor executor, ByteBufAllocator alloc) {
    requireNonNull(executor, "executor");
    requireNonNull(alloc, "alloc");
    final CompletableFuture<AggregatedHttpMessage> future = new CompletableFuture<>();
    final HttpResponseAggregator aggregator = new HttpResponseAggregator(future, alloc);
    completionFuture().handleAsync(aggregator, executor);
    subscribe(aggregator, executor, true);
    return future;
  }
}

代码示例来源:origin: line/armeria

/**
 * Aggregates this response. The returned {@link CompletableFuture} will be notified when the content and
 * the trailing headers of the response are received fully.
 */
default CompletableFuture<AggregatedHttpMessage> aggregate(EventExecutor executor) {
  final CompletableFuture<AggregatedHttpMessage> future = new CompletableFuture<>();
  final HttpResponseAggregator aggregator = new HttpResponseAggregator(future, null);
  completionFuture().handleAsync(aggregator, executor);
  subscribe(aggregator, executor);
  return future;
}

代码示例来源:origin: line/armeria

private Mono<Void> write(Flux<? extends DataBuffer> publisher) {
  return Mono.defer(() -> {
    final HttpResponse response = HttpResponse.of(
        Flux.concat(Mono.just(headers), publisher.map(factoryWrapper::toHttpData))
          // Publish the response stream on the event loop in order to avoid the possibility of
          // calling subscription.request() from multiple threads while publishing messages
          // with onNext signals or starting the subscription with onSubscribe signal.
          .publishOn(Schedulers.fromExecutor(ctx.eventLoop())));
    future.complete(response);
    return Mono.fromFuture(response.completionFuture());
  });
}

代码示例来源:origin: line/armeria

private static ServerBuilder configureService(ServerBuilder sb, HttpHandler httpHandler,
                       DataBufferFactoryWrapper<?> factoryWrapper,
                       @Nullable String serverHeader) {
  final ArmeriaHttpHandlerAdapter handler =
      new ArmeriaHttpHandlerAdapter(httpHandler, factoryWrapper);
  return sb.service(PathMapping.ofCatchAll(), (ctx, req) -> {
    final CompletableFuture<HttpResponse> future = new CompletableFuture<>();
    final HttpResponse response = HttpResponse.from(future);
    final Disposable disposable = handler.handle(ctx, req, future, serverHeader).subscribe();
    response.completionFuture().whenComplete((unused, cause) -> {
      if (cause != null) {
        logger.debug("{} Response stream has been cancelled.", ctx, cause);
        disposable.dispose();
      }
    });
    return response;
  });
}

代码示例来源:origin: line/armeria

@Test
public void returnHeadersOnly() throws Exception {
  final CompletableFuture<HttpResponse> future = new CompletableFuture<>();
  final ArmeriaServerHttpResponse response = response(ctx, future);
  response.setStatusCode(HttpStatus.NOT_FOUND);
  assertThat(future.isDone()).isFalse();
  // Create HttpResponse.
  response.setComplete().subscribe();
  await().until(future::isDone);
  assertThat(future.isCompletedExceptionally()).isFalse();
  final HttpResponse httpResponse = future.get();
  // Every message has not been consumed yet.
  assertThat(httpResponse.completionFuture().isDone()).isFalse();
  StepVerifier.create(httpResponse)
        .assertNext(o -> {
          assertThat(o.isEndOfStream()).isFalse();
          assertThat(o).isInstanceOf(HttpHeaders.class);
          final HttpHeaders headers = (HttpHeaders) o;
          assertThat(headers.status())
              .isEqualTo(com.linecorp.armeria.common.HttpStatus.NOT_FOUND);
        })
        .expectComplete()
        .verify();
  await().until(() -> httpResponse.completionFuture().isDone());
}

代码示例来源:origin: line/armeria

@Test
public void getCookies() {
  final HttpHeaders httpHeaders = HttpHeaders.of(HttpStatus.OK)
                        .add(HttpHeaderNames.of("blahblah"), "armeria")
                        .add(HttpHeaderNames.SET_COOKIE, "a=1; b=2");
  final HttpResponse httpResponse = HttpResponse.of(httpHeaders);
  final ArmeriaClientHttpResponse response =
      response(new ArmeriaHttpClientResponseSubscriber(httpResponse), httpHeaders);
  // HttpResponse would be completed after Httpheaders is completed, because there's no body.
  assertThat(httpResponse.completionFuture().isDone()).isTrue();
  assertThat(response.getStatusCode()).isEqualTo(org.springframework.http.HttpStatus.OK);
  assertThat(response.getHeaders().getFirst("blahblah")).isEqualTo("armeria");
  final ResponseCookie cookie = response.getCookies().getFirst("a");
  assertThat(cookie).isNotNull();
  assertThat(cookie.getValue()).isEqualTo("1");
}

代码示例来源:origin: line/armeria

@Test
public void cancel() {
  final AtomicBoolean completedWithError = new AtomicBoolean();
  final Flux<HttpData> bodyPub = Flux.just("a", "b", "c", "d", "e")
                    .map(HttpData::ofUtf8)
                    .doOnCancel(() -> completedWithError.set(true));
  final HttpHeaders httpHeaders = HttpHeaders.of(HttpStatus.OK);
  final HttpResponse httpResponse = HttpResponse.of(Flux.concat(Mono.just(httpHeaders), bodyPub));
  final ArmeriaClientHttpResponse response =
      response(new ArmeriaHttpClientResponseSubscriber(httpResponse), httpHeaders);
  assertThat(response.getStatusCode()).isEqualTo(org.springframework.http.HttpStatus.OK);
  assertThat(httpResponse.completionFuture().isDone()).isFalse();
  final Flux<String> body = response.getBody().map(TestUtil::bufferToString);
  StepVerifier.create(body, 1)
        .expectNext("a").thenRequest(1)
        .expectNext("b")
        .thenCancel()
        .verify();
  final CompletableFuture<Void> f = httpResponse.completionFuture();
  await().until(f::isDone);
  assertThat(f.isCompletedExceptionally()).isTrue();
  assertThatThrownBy(f::get).isInstanceOf(ExecutionException.class)
               .hasCauseInstanceOf(CancelledSubscriptionException.class);
  // Check whether the cancellation has been propagated to the original publisher.
  await().untilTrue(completedWithError);
}

代码示例来源:origin: line/armeria

@Test
public void readBodyStream() {
  final HttpHeaders httpHeaders = HttpHeaders.of(HttpStatus.OK);
  final HttpResponse httpResponse = HttpResponse.of(
      Flux.concat(Mono.just(httpHeaders),
            Flux.just("a", "b", "c", "d", "e")
              .map(HttpData::ofUtf8)));
  final ArmeriaClientHttpResponse response =
      response(new ArmeriaHttpClientResponseSubscriber(httpResponse), httpHeaders);
  assertThat(response.getStatusCode()).isEqualTo(org.springframework.http.HttpStatus.OK);
  assertThat(httpResponse.completionFuture().isDone()).isFalse();
  final Flux<String> body = response.getBody().map(TestUtil::bufferToString);
  StepVerifier.create(body, 1)
        .expectNext("a").thenRequest(1)
        .expectNext("b").thenRequest(1)
        .expectNext("c").thenRequest(1)
        .expectNext("d").thenRequest(1)
        .expectNext("e").thenRequest(1)
        .expectComplete()
        .verify();
  await().until(() -> httpResponse.completionFuture().isDone());
}

代码示例来源:origin: line/armeria

assertThat(httpResponse.completionFuture().isDone()).isFalse();
      .verify();
await().until(() -> httpResponse.completionFuture().isDone());

代码示例来源:origin: line/armeria

}).exceptionally(CompletionActions::log);
res.completionFuture().handleAsync((ret, cause) -> {
  req.abort();

代码示例来源:origin: line/armeria

assertThat(httpResponse.completionFuture().isDone()).isFalse();
      .verify();
await().until(() -> httpResponse.completionFuture().isDone());

代码示例来源:origin: line/armeria

@Override
public void start(Listener<O> responseListener, Metadata unused) {
  requireNonNull(responseListener, "responseListener");
  final Compressor compressor;
  if (callOptions.getCompressor() != null) {
    compressor = compressorRegistry.lookupCompressor(callOptions.getCompressor());
    if (compressor == null) {
      responseListener.onClose(
          Status.INTERNAL.withDescription(
              "Unable to find compressor by name " + callOptions.getCompressor()),
          EMPTY_METADATA);
      return;
    }
  } else {
    compressor = Identity.NONE;
  }
  messageFramer.setCompressor(compressor);
  prepareHeaders(req.headers(), compressor);
  listener = responseListener;
  final HttpResponse res;
  try (SafeCloseable ignored = ctx.push()) {
    res = httpClient.execute(ctx, req);
  } catch (Exception e) {
    close(Status.fromThrowable(e));
    return;
  }
  res.subscribe(responseReader, ctx.eventLoop(), true);
  res.completionFuture().handleAsync(responseReader, ctx.eventLoop());
}

代码示例来源:origin: line/armeria

assertThat(httpResponse.completionFuture().isDone()).isFalse();
      .verify();
await().until(() -> httpResponse.completionFuture().isDone());

代码示例来源:origin: com.linecorp.armeria/armeria-grpc

@Override
public void start(Listener<O> responseListener, Metadata unused) {
  requireNonNull(responseListener, "responseListener");
  final Compressor compressor;
  if (callOptions.getCompressor() != null) {
    compressor = compressorRegistry.lookupCompressor(callOptions.getCompressor());
    if (compressor == null) {
      responseListener.onClose(
          Status.INTERNAL.withDescription(
              "Unable to find compressor by name " + callOptions.getCompressor()),
          EMPTY_METADATA);
      return;
    }
  } else {
    compressor = Identity.NONE;
  }
  messageFramer.setCompressor(compressor);
  prepareHeaders(req.headers(), compressor);
  listener = responseListener;
  final HttpResponse res;
  try (SafeCloseable ignored = ctx.push()) {
    res = httpClient.execute(ctx, req);
  } catch (Exception e) {
    close(Status.fromThrowable(e));
    return;
  }
  res.subscribe(responseReader, ctx.eventLoop(), true);
  res.completionFuture().handleAsync(responseReader, ctx.eventLoop());
}

相关文章