Skip to content

Instantly share code, notes, and snippets.

@tomatophobia
Created August 5, 2023 17:11
Show Gist options
  • Save tomatophobia/486eb752bce3de9f5c16027e34dc0060 to your computer and use it in GitHub Desktop.
Save tomatophobia/486eb752bce3de9f5c16027e34dc0060 to your computer and use it in GitHub Desktop.
ServerBuilder sb = ...;
sb.service("/api", (ctx, req) -> {
CompletableFuture<HttpResponse> future = new CompletableFuture<>();
HttpResponse authResponse = authClient.execute(req.headers());
// 서드파티 API의 응답을 다른 쓰레드에서 비동기로 처리한다.
authResponse.aggregate().thenAccept(aggregatedRes -> {
// 요청 컨텍스트 정보를 명시적으로 전달한다.
try (SafeCloseable ignored = ctx.push()) {
HttpResponse response = backendClient.execute(req);
future.complete(response);
}
});
return HttpResponse.from(future);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment