|
| 1 | +/** |
| 2 | + * Copyright 2012-2020 The Feign Authors |
| 3 | + * |
| 4 | + * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | + * except in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * <p>http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * <p>Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | + * express or implied. See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package feign; |
| 15 | + |
| 16 | +import feign.Request.Options; |
| 17 | +import java.util.Optional; |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | +import java.util.concurrent.ExecutorService; |
| 20 | +import java.util.concurrent.Future; |
| 21 | + |
| 22 | +/** Submits HTTP {@link Request requests} asynchronously, with an optional context. */ |
| 23 | +@Experimental |
| 24 | +public interface AsyncClient<C> { |
| 25 | + |
| 26 | + /** |
| 27 | + * Executes the request asynchronously. Calling {@link CompletableFuture#cancel(boolean)} on the |
| 28 | + * result may cause the execution to be cancelled / aborted, but this is not guaranteed. |
| 29 | + * |
| 30 | + * @param request safe to replay |
| 31 | + * @param options options to apply to this request |
| 32 | + * @param requestContext - the optional context, for example for storing session cookies. The |
| 33 | + * client should update this appropriately based on the received response before completing |
| 34 | + * the result. |
| 35 | + * @return a {@link CompletableFuture} to be completed with the response, or completed |
| 36 | + * exceptionally otherwise, for example with an {@link java.io.IOException} on a network error |
| 37 | + * connecting to {@link Request#url()}. |
| 38 | + */ |
| 39 | + CompletableFuture<Response> execute(Request request, Options options, Optional<C> requestContext); |
| 40 | + |
| 41 | + class Default<C> implements AsyncClient<C> { |
| 42 | + |
| 43 | + private final Client client; |
| 44 | + private final ExecutorService executorService; |
| 45 | + |
| 46 | + public Default(Client client, ExecutorService executorService) { |
| 47 | + this.client = client; |
| 48 | + this.executorService = executorService; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public CompletableFuture<Response> execute( |
| 53 | + Request request, Options options, Optional<C> requestContext) { |
| 54 | + final CompletableFuture<Response> result = new CompletableFuture<>(); |
| 55 | + final Future<?> future = |
| 56 | + executorService.submit( |
| 57 | + () -> { |
| 58 | + try { |
| 59 | + result.complete(client.execute(request, options)); |
| 60 | + } catch (final Exception e) { |
| 61 | + result.completeExceptionally(e); |
| 62 | + } |
| 63 | + }); |
| 64 | + result.whenComplete( |
| 65 | + (response, throwable) -> { |
| 66 | + if (result.isCancelled()) { |
| 67 | + future.cancel(true); |
| 68 | + } |
| 69 | + }); |
| 70 | + return result; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * A synchronous implementation of {@link AsyncClient} |
| 76 | + * |
| 77 | + * @param <C> - unused context; synchronous clients handle context internally |
| 78 | + */ |
| 79 | + class Pseudo<C> implements AsyncClient<C> { |
| 80 | + |
| 81 | + private final Client client; |
| 82 | + |
| 83 | + public Pseudo(Client client) { |
| 84 | + this.client = client; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public CompletableFuture<Response> execute( |
| 89 | + Request request, Options options, Optional<C> requestContext) { |
| 90 | + final CompletableFuture<Response> result = new CompletableFuture<>(); |
| 91 | + try { |
| 92 | + result.complete(client.execute(request, options)); |
| 93 | + } catch (final Exception e) { |
| 94 | + result.completeExceptionally(e); |
| 95 | + } |
| 96 | + |
| 97 | + return result; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments