下面列出了怎么用org.apache.http.nio.client.HttpAsyncClient的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpAsyncClient asyncClient = getHttpAsyncClient();
startAsyncClient();
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
if (context == null) {
context = HttpClientContext.create();
}
// Request configuration not set in the context
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
// Use request configuration given by the user, when available
RequestConfig config = null;
if (httpRequest instanceof Configurable) {
config = ((Configurable) httpRequest).getConfig();
}
if (config == null) {
config = createRequestConfig(asyncClient);
}
if (config != null) {
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
}
return new HttpComponentsAsyncClientHttpRequest(asyncClient, httpRequest, context);
}
private HttpAsyncClient startAsyncClient() {
HttpAsyncClient client = getAsyncClient();
if (client instanceof CloseableHttpAsyncClient) {
CloseableHttpAsyncClient closeableAsyncClient = (CloseableHttpAsyncClient) client;
if (!closeableAsyncClient.isRunning()) {
closeableAsyncClient.start();
}
}
return client;
}
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpAsyncClient client = startAsyncClient();
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
if (context == null) {
context = HttpClientContext.create();
}
// Request configuration not set in the context
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
// Use request configuration given by the user, when available
RequestConfig config = null;
if (httpRequest instanceof Configurable) {
config = ((Configurable) httpRequest).getConfig();
}
if (config == null) {
config = createRequestConfig(client);
}
if (config != null) {
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
}
return new HttpComponentsAsyncClientHttpRequest(client, httpRequest, context);
}
@Override
public void destroy() throws Exception {
try {
super.destroy();
}
finally {
HttpAsyncClient asyncClient = getAsyncClient();
if (asyncClient instanceof Closeable) {
((Closeable) asyncClient).close();
}
}
}
private HttpAsyncClient startAsyncClient() {
HttpAsyncClient client = getAsyncClient();
if (client instanceof CloseableHttpAsyncClient) {
CloseableHttpAsyncClient closeableAsyncClient = (CloseableHttpAsyncClient) client;
if (!closeableAsyncClient.isRunning()) {
closeableAsyncClient.start();
}
}
return client;
}
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpAsyncClient client = startAsyncClient();
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
HttpContext context = createHttpContext(httpMethod, uri);
if (context == null) {
context = HttpClientContext.create();
}
// Request configuration not set in the context
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
// Use request configuration given by the user, when available
RequestConfig config = null;
if (httpRequest instanceof Configurable) {
config = ((Configurable) httpRequest).getConfig();
}
if (config == null) {
config = createRequestConfig(client);
}
if (config != null) {
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
}
}
return new HttpComponentsAsyncClientHttpRequest(client, httpRequest, context);
}
@Override
public void destroy() throws Exception {
try {
super.destroy();
}
finally {
HttpAsyncClient asyncClient = getAsyncClient();
if (asyncClient instanceof Closeable) {
((Closeable) asyncClient).close();
}
}
}
private void startAsyncClient() {
HttpAsyncClient asyncClient = getAsyncClient();
if (asyncClient instanceof CloseableHttpAsyncClient) {
CloseableHttpAsyncClient closeableAsyncClient = (CloseableHttpAsyncClient) asyncClient;
if (!closeableAsyncClient.isRunning()) {
closeableAsyncClient.start();
}
}
}
@Override
public void destroy() throws Exception {
try {
super.destroy();
}
finally {
HttpAsyncClient asyncClient = getAsyncClient();
if (asyncClient instanceof Closeable) {
((Closeable) asyncClient).close();
}
}
}
HttpComponentsAsyncClientHttpRequest(HttpAsyncClient client, HttpUriRequest request, HttpContext context) {
this.httpClient = client;
this.httpRequest = request;
this.httpContext = context;
}
HttpComponentsAsyncClientHttpRequest(HttpAsyncClient client, HttpUriRequest request, HttpContext context) {
this.httpClient = client;
this.httpRequest = request;
this.httpContext = context;
}
HttpComponentsAsyncClientHttpRequest(HttpAsyncClient client, HttpUriRequest request, HttpContext context) {
this.httpClient = client;
this.httpRequest = request;
this.httpContext = context;
}
public HttpAsyncClient http() {
return clnt;
}
HttpComponentsAsyncClientHttpRequest(HttpAsyncClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) {
this.httpClient = httpClient;
this.httpRequest = httpRequest;
this.httpContext = httpContext;
}
/**
* Create a new instance of the {@code HttpComponentsAsyncClientHttpRequestFactory}
* with the given {@link HttpAsyncClient} instance and a default {@link HttpClient}.
* @param asyncClient the HttpAsyncClient instance to use for this request factory
* @since 4.3.10
*/
public HttpComponentsAsyncClientHttpRequestFactory(HttpAsyncClient asyncClient) {
super();
this.asyncClient = asyncClient;
}
/**
* Create a new instance of the {@code HttpComponentsAsyncClientHttpRequestFactory}
* with the given {@link HttpClient} and {@link HttpAsyncClient} instances.
* @param httpClient the HttpClient instance to use for this request factory
* @param asyncClient the HttpAsyncClient instance to use for this request factory
* @since 4.3.10
*/
public HttpComponentsAsyncClientHttpRequestFactory(HttpClient httpClient, HttpAsyncClient asyncClient) {
super(httpClient);
this.asyncClient = asyncClient;
}
/**
* Set the {@code HttpAsyncClient} used for
* {@linkplain #createAsyncRequest(URI, HttpMethod) synchronous execution}.
* @since 4.3.10
* @see #setHttpClient(HttpClient)
*/
public void setAsyncClient(HttpAsyncClient asyncClient) {
Assert.notNull(asyncClient, "HttpAsyncClient must not be null");
this.asyncClient = asyncClient;
}
/**
* Return the {@code HttpAsyncClient} used for
* {@linkplain #createAsyncRequest(URI, HttpMethod) synchronous execution}.
* @since 4.3.10
* @see #getHttpClient()
*/
public HttpAsyncClient getAsyncClient() {
return this.asyncClient;
}
/**
* Create a new instance of the {@code HttpComponentsAsyncClientHttpRequestFactory}
* with the given {@link HttpAsyncClient} instance and a default {@link HttpClient}.
* @param asyncClient the HttpAsyncClient instance to use for this request factory
* @since 4.3.10
*/
public HttpComponentsAsyncClientHttpRequestFactory(HttpAsyncClient asyncClient) {
super();
this.asyncClient = asyncClient;
}
/**
* Create a new instance of the {@code HttpComponentsAsyncClientHttpRequestFactory}
* with the given {@link HttpClient} and {@link HttpAsyncClient} instances.
* @param httpClient the HttpClient instance to use for this request factory
* @param asyncClient the HttpAsyncClient instance to use for this request factory
* @since 4.3.10
*/
public HttpComponentsAsyncClientHttpRequestFactory(HttpClient httpClient, HttpAsyncClient asyncClient) {
super(httpClient);
this.asyncClient = asyncClient;
}
/**
* Set the {@code HttpAsyncClient} used for
* {@linkplain #createAsyncRequest(URI, HttpMethod) synchronous execution}.
* @since 4.3.10
* @see #setHttpClient(HttpClient)
*/
public void setAsyncClient(HttpAsyncClient asyncClient) {
Assert.notNull(asyncClient, "HttpAsyncClient must not be null");
this.asyncClient = asyncClient;
}
/**
* Return the {@code HttpAsyncClient} used for
* {@linkplain #createAsyncRequest(URI, HttpMethod) synchronous execution}.
* @since 4.3.10
* @see #getHttpClient()
*/
public HttpAsyncClient getAsyncClient() {
return this.asyncClient;
}
/**
* Create a new instance of the {@code HttpComponentsAsyncClientHttpRequestFactory}
* with the given {@link HttpAsyncClient} instance and a default {@link HttpClient}.
* @param asyncClient the HttpAsyncClient instance to use for this request factory
* @since 4.3.10
*/
public HttpComponentsAsyncClientHttpRequestFactory(HttpAsyncClient asyncClient) {
super();
setAsyncClient(asyncClient);
}
/**
* Create a new instance of the {@code HttpComponentsAsyncClientHttpRequestFactory}
* with the given {@link HttpClient} and {@link HttpAsyncClient} instances.
* @param httpClient the HttpClient instance to use for this request factory
* @param asyncClient the HttpAsyncClient instance to use for this request factory
* @since 4.3.10
*/
public HttpComponentsAsyncClientHttpRequestFactory(HttpClient httpClient, HttpAsyncClient asyncClient) {
super(httpClient);
setAsyncClient(asyncClient);
}
/**
* Set the {@code HttpAsyncClient} used for
* {@linkplain #createAsyncRequest(URI, HttpMethod) synchronous execution}.
* @since 4.3.10
* @see #setHttpClient(HttpClient)
*/
public void setAsyncClient(HttpAsyncClient asyncClient) {
Assert.notNull(asyncClient, "HttpAsyncClient must not be null");
this.asyncClient = asyncClient;
}
/**
* Return the {@code HttpAsyncClient} used for
* {@linkplain #createAsyncRequest(URI, HttpMethod) synchronous execution}.
* @since 4.3.10
* @see #getHttpClient()
*/
public HttpAsyncClient getAsyncClient() {
return this.asyncClient;
}
/**
* Init a connection to a given Salt API endpoint.
*
* @param httpClientIn the HTTP client
*/
public HttpAsyncClientImpl(HttpAsyncClient httpClientIn) {
httpClient = httpClientIn;
}