Java Code Examples for com.linecorp.armeria.common.RequestContext#makeContextAware()

The following examples show how to use com.linecorp.armeria.common.RequestContext#makeContextAware() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Java9RequestContextAwareFutureTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void minimalCompletionStageUsingWhenComplete() throws Exception {
    final RequestContext context =
            ServiceRequestContext.builder(HttpRequest.of(HttpMethod.GET, "/")).build();
    final CompletableFuture<Integer> originalFuture = new CompletableFuture<>();
    final CompletableFuture<Integer> contextAwareFuture = context.makeContextAware(originalFuture);
    final CompletionStage<Integer> completionStage = contextAwareFuture.minimalCompletionStage();

    final AtomicInteger atomicInteger = new AtomicInteger();
    final AtomicReference<Throwable> causeCaptor = new AtomicReference<>();
    completionStage.whenComplete((result, error) -> {
        if (error != null) {
            causeCaptor.set(error);
        } else {
            atomicInteger.set(result);
        }
    });
    contextAwareFuture.complete(1);

    assertThat(contextAwareFuture.join()).isEqualTo(1);
    assertThat(contextAwareFuture.getNow(null)).isEqualTo(1);
    assertThat(contextAwareFuture.get()).isEqualTo(1);
    assertThat(atomicInteger.get()).isEqualTo(1);
    assertThat(causeCaptor.get()).isNull();
}
 
Example 2
Source File: CurrentRequestContextForwardingExecutorService.java    From curiostack with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("ParameterPackage")
public <T> ListenableFuture<T> submit(Callable<T> task) {
  RequestContext ctx = RequestContext.mapCurrent(Function.identity(), null);
  if (ctx != null) {
    task = ctx.makeContextAware(task);
  }
  return delegate().submit(task);
}
 
Example 3
Source File: CurrentRequestContextForwardingExecutorService.java    From curiostack with MIT License 5 votes vote down vote up
@Override
public ListenableFuture<?> submit(Runnable task) {
  RequestContext ctx = RequestContext.mapCurrent(Function.identity(), null);
  if (ctx != null) {
    task = ctx.makeContextAware(task);
  }
  return delegate().submit(task);
}
 
Example 4
Source File: CurrentRequestContextForwardingExecutorService.java    From curiostack with MIT License 5 votes vote down vote up
@Override
public <T> ListenableFuture<T> submit(Runnable task, T result) {
  RequestContext ctx = RequestContext.mapCurrent(Function.identity(), null);
  if (ctx != null) {
    task = ctx.makeContextAware(task);
  }
  return delegate().submit(task, result);
}
 
Example 5
Source File: Java9RequestContextAwareFutureTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void minimalCompletionStageUsingToCompletableFutureMutable() throws Exception {
    final RequestContext context =
            ServiceRequestContext.builder(HttpRequest.of(HttpMethod.GET, "/")).build();
    final CompletableFuture<Integer> originalFuture = new CompletableFuture<>();
    final CompletableFuture<Integer> contextAwareFuture = context.makeContextAware(originalFuture);
    final CompletionStage<Integer> completionStage = contextAwareFuture.minimalCompletionStage();

    assertThat(contextAwareFuture.complete(1)).isTrue();
    assertThat(contextAwareFuture.join()).isEqualTo(1);
    assertThat(contextAwareFuture.getNow(null)).isEqualTo(1);
    assertThat(contextAwareFuture.get()).isEqualTo(1);
    assertThat(completionStage.toCompletableFuture().get()).isEqualTo(1);
}
 
Example 6
Source File: Java9RequestContextAwareFutureTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void makeContextAwareCompletableFutureUsingCompleteAsync() throws Exception {
    final RequestContext context =
            ServiceRequestContext.builder(HttpRequest.of(HttpMethod.GET, "/")).build();
    final CompletableFuture<String> originalFuture = new CompletableFuture<>();
    final CompletableFuture<String> contextAwareFuture = context.makeContextAware(originalFuture);
    final CompletableFuture<String> resultFuture = contextAwareFuture.completeAsync(() -> "success");

    originalFuture.complete("success");
    assertThat(resultFuture.get()).isEqualTo("success");
}
 
Example 7
Source File: Java9RequestContextAwareFutureTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void makeContextAwareCompletableFutureUsingCompleteAsyncWithExecutor() throws Exception {
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    final RequestContext context =
            ServiceRequestContext.builder(HttpRequest.of(HttpMethod.GET, "/")).build();
    final CompletableFuture<String> originalFuture = new CompletableFuture<>();
    final CompletableFuture<String> contextAwareFuture = context.makeContextAware(originalFuture);
    final CompletableFuture<String> resultFuture = contextAwareFuture.completeAsync(() -> "success",
                                                                                    executor);

    originalFuture.complete("success");
    assertThat(resultFuture.get()).isEqualTo("success");
}