Java Code Examples for org.springframework.http.client.AsyncClientHttpRequestExecution#executeAsync()
The following examples show how to use
org.springframework.http.client.AsyncClientHttpRequestExecution#executeAsync() .
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: AsyncRestTemplateCircuitInterceptor.java From spring-cloud-formula with Apache License 2.0 | 6 votes |
public ListenableFuture<ClientHttpResponse> doExecuteAsync(AsyncClientHttpRequestExecution execution, HttpRequest httpRequest, byte[] body) throws IOException { try { ListenableFuture<ClientHttpResponse> response = execution.executeAsync(httpRequest, body); logger.info("http request response:" + response); if (response == null || !HttpStatus.OK.equals(response.get().getStatusCode())) { throw new IOException("response error"); } return response; } catch (Exception e) { logger.error(e.getMessage(), e); throw new IOException(e.getMessage(), e); } }
Example 2
Source File: AsyncRestTemplateIntegrationTests.java From java-technology-stack with MIT License | 6 votes |
@Override public ListenableFuture<ClientHttpResponse> intercept(HttpRequest request, byte[] body, AsyncClientHttpRequestExecution execution) throws IOException { ListenableFuture<ClientHttpResponse> future = execution.executeAsync(request, body); future.addCallback( resp -> { response = resp; this.latch.countDown(); }, ex -> { exception = ex; this.latch.countDown(); }); return future; }
Example 3
Source File: TracingAsyncClientHttpRequestInterceptor.java From brave with Apache License 2.0 | 6 votes |
@Override public ListenableFuture<ClientHttpResponse> intercept(HttpRequest req, byte[] body, AsyncClientHttpRequestExecution execution) throws IOException { HttpRequestWrapper request = new HttpRequestWrapper(req); Span span = handler.handleSend(request); // avoid context sync overhead when we are the root span TraceContext invocationContext = span.context().parentIdAsLong() != 0 ? currentTraceContext.get() : null; try (Scope ws = currentTraceContext.maybeScope(span.context())) { ListenableFuture<ClientHttpResponse> result = execution.executeAsync(req, body); result.addCallback(new TraceListenableFutureCallback(request, span, handler)); return invocationContext != null ? new TraceContextListenableFuture<>(result, currentTraceContext, invocationContext) : result; } catch (Throwable e) { handler.handleReceive(new ClientHttpResponseWrapper(request, null, e), span); throw e; } }
Example 4
Source File: AsyncRestTemplateCircuitInterceptor.java From spring-cloud-formula with Apache License 2.0 | 5 votes |
@Override public ListenableFuture<ClientHttpResponse> intercept(HttpRequest httpRequest, byte[] body, AsyncClientHttpRequestExecution execution) throws IOException { logger.debug("AsyncRestTemplateCircuitInterceptor start"); URI asUri = httpRequest.getURI(); String httpMethod = httpRequest.getMethod().toString(); String serviceName = asUri.getHost(); String url = asUri.getPath(); logger.info("http with serviceName:{}, menthod:{}, url:{}", serviceName, httpMethod, url); if (circuitBreakerCore.checkRulesExist(httpMethod, serviceName, url)) { try { Method wrappedMethod = AsyncRestTemplateCircuitInterceptor.class.getMethod( "doExecuteAsync", AsyncClientHttpRequestExecution.class, HttpRequest.class, byte[].class); Object[] args = {httpRequest, body}; ListenableFuture<ClientHttpResponse> response = (ListenableFuture<ClientHttpResponse>) circuitBreakerCore.process(httpMethod, serviceName, url, wrappedMethod, this, args); // todo 熔断返回null return response; } catch (Exception e) { logger.error(e.getMessage(), e); if (e instanceof CircuitBreakerOpenException) { throw new RuntimeException(e.getMessage()); } else if (e instanceof IOException) { throw new IOException(e.getMessage()); } else { throw new RuntimeException(e.getMessage()); } } } else { return execution.executeAsync(httpRequest, body); } }
Example 5
Source File: AsyncRestTemplateRateLimiterInterceptor.java From spring-cloud-formula with Apache License 2.0 | 5 votes |
@Override public ListenableFuture<ClientHttpResponse> intercept(HttpRequest request, byte[] body, AsyncClientHttpRequestExecution execution) throws IOException { // add system tags from env FormulaConfigUtils.getSystemTags().forEach((k, v) -> { request.getHeaders().add(k, v); }); // add service name from input request.getHeaders().add(SystemTag.SERVICE_NAME, serviceName); logger.debug("AsyncRestTemplate: insert {} into httpHeader: {}", SystemTag.SERVICE_NAME, serviceName); return execution.executeAsync(request, body); }
Example 6
Source File: MetricsClientHttpRequestInterceptor.java From summerframework with Apache License 2.0 | 5 votes |
@Override public ListenableFuture<ClientHttpResponse> intercept(HttpRequest request, byte[] body, AsyncClientHttpRequestExecution execution) throws IOException { final String urlTemplate = urlTemplateHolder.get(); urlTemplateHolder.remove(); final Clock clock = meterRegistry.config().clock(); final long startTime = clock.monotonicTime(); ListenableFuture<ClientHttpResponse> future; try { future = execution.executeAsync(request, body); } catch (IOException e) { getTimeBuilder(urlTemplate, request, null, e).register(meterRegistry) .record(clock.monotonicTime() - startTime, TimeUnit.NANOSECONDS); throw e; } future.addCallback(new ListenableFutureCallback<ClientHttpResponse>() { @Override public void onFailure(final Throwable ex) { getTimeBuilder(urlTemplate, request, null, ex).register(meterRegistry) .record(clock.monotonicTime() - startTime, TimeUnit.NANOSECONDS); } @Override public void onSuccess(final ClientHttpResponse response) { getTimeBuilder(urlTemplate, request, response, null).register(meterRegistry) .record(clock.monotonicTime() - startTime, TimeUnit.NANOSECONDS); } }); return future; }
Example 7
Source File: AsyncRestTemplateRequestInterceptor.java From sofa-tracer with Apache License 2.0 | 5 votes |
@Override public ListenableFuture<ClientHttpResponse> intercept(HttpRequest request, byte[] body, AsyncClientHttpRequestExecution execution) throws IOException { SofaTracerSpan sofaTracerSpan = restTemplateTracer.clientSend(request.getMethod().name()); appendRestTemplateRequestSpanTags(request, sofaTracerSpan); Exception exception = null; try { ListenableFuture<ClientHttpResponse> result = execution.executeAsync(request, body); result.addCallback(new SofaTraceListenableFutureCallback(restTemplateTracer, sofaTracerSpan)); return result; } catch (IOException e) { exception = e; throw e; } finally { // when error , clear tl soon if (exception != null) { SofaTracerSpan currentSpan = SofaTraceContextHolder.getSofaTraceContext() .getCurrentSpan(); currentSpan.setTag(Tags.ERROR.getKey(), exception.getMessage()); restTemplateTracer.clientReceive(String.valueOf(500)); } else { // clear current SofaTraceContextHolder.getSofaTraceContext().pop(); if (sofaTracerSpan != null && sofaTracerSpan.getParentSofaTracerSpan() != null) { // reset parent SofaTraceContextHolder.getSofaTraceContext().push( sofaTracerSpan.getParentSofaTracerSpan()); } } } }
Example 8
Source File: WingtipsAsyncClientHttpRequestInterceptor.java From wingtips with Apache License 2.0 | 5 votes |
/** * Calls {@link WingtipsSpringUtil#propagateTracingHeaders(HttpMessage, Span)} to propagate the current span's * tracing state on the given request's headers, then returns * {@link AsyncClientHttpRequestExecution#executeAsync(HttpRequest, byte[])} to execute the request. * * @return The result of calling {@link AsyncClientHttpRequestExecution#executeAsync(HttpRequest, byte[])}. */ protected ListenableFuture<ClientHttpResponse> propagateTracingHeadersAndExecute( HttpRequestWrapperWithModifiableHeaders wrapperRequest, byte[] body, AsyncClientHttpRequestExecution execution ) throws IOException { propagateTracingHeaders(wrapperRequest, Tracer.getInstance().getCurrentSpan()); // Execute the request/interceptor chain. return execution.executeAsync(wrapperRequest, body); }
Example 9
Source File: BasicAuthAsyncInterceptor.java From haven-platform with Apache License 2.0 | 4 votes |
@Override public ListenableFuture<ClientHttpResponse> intercept(HttpRequest request, byte[] body, AsyncClientHttpRequestExecution execution) throws IOException { String token = Base64Utils.encodeToString((this.username + ":" + this.password).getBytes(StandardCharsets.UTF_8)); request.getHeaders().add("Authorization", "Basic " + token); return execution.executeAsync(request, body); }