下面列出了怎么用org.springframework.http.client.AsyncClientHttpRequest的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Execute the given method on the provided URI. The
* {@link org.springframework.http.client.ClientHttpRequest}
* is processed using the {@link RequestCallback}; the response with
* the {@link ResponseExtractor}.
* @param url the fully-expanded URL to connect to
* @param method the HTTP method to execute (GET, POST, etc.)
* @param requestCallback object that prepares the request (can be {@code null})
* @param responseExtractor object that extracts the return value from the response (can
* be {@code null})
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
*/
protected <T> ListenableFuture<T> doExecute(URI url, HttpMethod method, AsyncRequestCallback requestCallback,
ResponseExtractor<T> responseExtractor) throws RestClientException {
Assert.notNull(url, "'url' must not be null");
Assert.notNull(method, "'method' must not be null");
try {
AsyncClientHttpRequest request = createAsyncRequest(url, method);
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
ListenableFuture<ClientHttpResponse> responseFuture = request.executeAsync();
return new ResponseExtractorFuture<T>(method, url, responseFuture, responseExtractor);
}
catch (IOException ex) {
throw new ResourceAccessException("I/O error on " + method.name() +
" request for \"" + url + "\":" + ex.getMessage(), ex);
}
}
/**
* Execute the given method on the provided URI. The
* {@link org.springframework.http.client.ClientHttpRequest}
* is processed using the {@link RequestCallback}; the response with
* the {@link ResponseExtractor}.
* @param url the fully-expanded URL to connect to
* @param method the HTTP method to execute (GET, POST, etc.)
* @param requestCallback object that prepares the request (can be {@code null})
* @param responseExtractor object that extracts the return value from the response (can
* be {@code null})
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
*/
protected <T> ListenableFuture<T> doExecute(URI url, HttpMethod method, AsyncRequestCallback requestCallback,
ResponseExtractor<T> responseExtractor) throws RestClientException {
Assert.notNull(url, "'url' must not be null");
Assert.notNull(method, "'method' must not be null");
try {
AsyncClientHttpRequest request = createAsyncRequest(url, method);
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
ListenableFuture<ClientHttpResponse> responseFuture = request.executeAsync();
return new ResponseExtractorFuture<T>(method, url, responseFuture, responseExtractor);
}
catch (IOException ex) {
throw new ResourceAccessException("I/O error on " + method.name() +
" request for \"" + url + "\":" + ex.getMessage(), ex);
}
}
@DataProvider(value = {
"true | spring.asyncresttemplate",
"false | spring.resttemplate"
}, splitBy = "\\|")
@Test
public void getSpanHandlerTagValue_works_as_expected_when_request_is_not_an_HttpRequestWrapper(
boolean requestIsAsyncClientHttpRequest, String expectedResult
) {
// given
HttpRequest request = (requestIsAsyncClientHttpRequest)
? mock(AsyncClientHttpRequest.class)
: mock(HttpRequest.class);
// when
String result = implSpy.getSpanHandlerTagValue(request, responseMock);
// then
assertThat(result).isEqualTo(expectedResult);
}
@DataProvider(value = {
"true | spring.asyncresttemplate",
"false | spring.resttemplate"
}, splitBy = "\\|")
@Test
public void getSpanHandlerTagValue_works_as_expected_when_request_is_an_HttpRequestWrapper(
boolean wrappedRequestIsAsyncClientHttpRequest, String expectedResult
) {
// given
HttpRequestWrapper request = mock(HttpRequestWrapper.class);
HttpRequest wrappedRequest = (wrappedRequestIsAsyncClientHttpRequest)
? mock(AsyncClientHttpRequest.class)
: mock(HttpRequest.class);
doReturn(wrappedRequest).when(request).getRequest();
// when
String result = implSpy.getSpanHandlerTagValue(request, responseMock);
// then
assertThat(result).isEqualTo(expectedResult);
}
@Override
public void doWithRequest(final AsyncClientHttpRequest request) throws IOException {
if (this.adaptee != null) {
this.adaptee.doWithRequest(new ClientHttpRequest() {
@Override
public ClientHttpResponse execute() throws IOException {
throw new UnsupportedOperationException("execute not supported");
}
@Override
public OutputStream getBody() throws IOException {
return request.getBody();
}
@Override
public HttpMethod getMethod() {
return request.getMethod();
}
@Override
public URI getURI() {
return request.getURI();
}
@Override
public HttpHeaders getHeaders() {
return request.getHeaders();
}
});
}
}
@Override
public void doWithRequest(final AsyncClientHttpRequest request) throws IOException {
if (this.adaptee != null) {
this.adaptee.doWithRequest(new ClientHttpRequest() {
@Override
public ClientHttpResponse execute() throws IOException {
throw new UnsupportedOperationException("execute not supported");
}
@Override
public OutputStream getBody() throws IOException {
return request.getBody();
}
@Override
public HttpMethod getMethod() {
return request.getMethod();
}
@Override
public URI getURI() {
return request.getURI();
}
@Override
public HttpHeaders getHeaders() {
return request.getHeaders();
}
});
}
}
/**
* Create a new {@link AsyncClientHttpRequest} via this template's {@link
* AsyncClientHttpRequestFactory}.
* @param url the URL to connect to
* @param method the HTTP method to execute (GET, POST, etc.)
* @return the created request
* @throws IOException in case of I/O errors
*/
protected AsyncClientHttpRequest createAsyncRequest(URI url, HttpMethod method)
throws IOException {
AsyncClientHttpRequest request = getAsyncRequestFactory().createAsyncRequest(url, method);
if (logger.isDebugEnabled()) {
logger.debug("Created asynchronous " + method.name() + " request for \"" + url + "\"");
}
return request;
}
@Override
public @Nullable String getSpanHandlerTagValue(@Nullable HttpRequest request, @Nullable ClientHttpResponse response) {
if (request instanceof HttpRequestWrapper) {
request = ((HttpRequestWrapper) request).getRequest();
}
if (request instanceof AsyncClientHttpRequest) {
return "spring.asyncresttemplate";
}
return "spring.resttemplate";
}
@Override
public CompletableFuture<ClientHttpResponse> execute(final RequestArguments arguments) throws IOException {
final URI uri = arguments.getRequestUri();
final HttpMethod method = arguments.getMethod();
final AsyncClientHttpRequest request = requestFactory.createAsyncRequest(uri, method);
copyTo(arguments.getHeaders(), request.getHeaders());
arguments.getEntity().writeTo(request);
return toCompletable(request.executeAsync());
}
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
Object ret) throws Throwable {
AsyncClientHttpRequest clientHttpRequest = (AsyncClientHttpRequest) ret;
if (ret != null) {
Object[] cacheValues = (Object[]) objInst.getSkyWalkingDynamicField();
ContextCarrier contextCarrier = (ContextCarrier) cacheValues[1];
CarrierItem next = contextCarrier.items();
while (next.hasNext()) {
next = next.next();
clientHttpRequest.getHeaders().set(next.getHeadKey(), next.getHeadValue());
}
}
return ret;
}
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
uri = convertToRealUri(uri);
return super.createAsyncRequest(uri, httpMethod);
}
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
return createRequestInternal(uri, httpMethod);
}
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
return super.createAsyncRequest(expand(uri), httpMethod);
}
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod)
throws IOException {
return this.factory.createAsyncRequest(uri, httpMethod);
}
/**
* Create a new {@link AsyncClientHttpRequest} via this template's
* {@link AsyncClientHttpRequestFactory}.
* @param url the URL to connect to
* @param method the HTTP method to execute (GET, POST, etc.)
* @return the created request
* @throws IOException in case of I/O errors
*/
protected AsyncClientHttpRequest createAsyncRequest(URI url, HttpMethod method) throws IOException {
AsyncClientHttpRequest request = getAsyncRequestFactory().createAsyncRequest(url, method);
if (logger.isDebugEnabled()) {
logger.debug("Created asynchronous " + method.name() + " request for \"" + url + "\"");
}
return request;
}
/**
* Gets called by {@link AsyncRestTemplate#execute} with an opened {@code ClientHttpRequest}.
* Does not need to care about closing the request or about handling errors:
* this will all be handled by the {@code RestTemplate}.
* @param request the active HTTP request
* @throws java.io.IOException in case of I/O errors
*/
void doWithRequest(AsyncClientHttpRequest request) throws IOException;
/**
* Gets called by {@link AsyncRestTemplate#execute} with an opened {@code ClientHttpRequest}.
* Does not need to care about closing the request or about handling errors:
* this will all be handled by the {@code RestTemplate}.
* @param request the active HTTP request
* @throws java.io.IOException in case of I/O errors
*/
void doWithRequest(AsyncClientHttpRequest request) throws IOException;