View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl.async;
28  
29  import java.util.concurrent.Future;
30  
31  import org.apache.hc.client5.http.async.HttpAsyncClient;
32  import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
33  import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
34  import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
35  import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
36  import org.apache.hc.core5.annotation.Contract;
37  import org.apache.hc.core5.annotation.ThreadingBehavior;
38  import org.apache.hc.core5.concurrent.FutureCallback;
39  import org.apache.hc.core5.function.Supplier;
40  import org.apache.hc.core5.http.HttpHost;
41  import org.apache.hc.core5.http.nio.AsyncPushConsumer;
42  import org.apache.hc.core5.http.nio.AsyncRequestProducer;
43  import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
44  import org.apache.hc.core5.http.nio.HandlerFactory;
45  import org.apache.hc.core5.http.protocol.HttpContext;
46  import org.apache.hc.core5.io.ModalCloseable;
47  import org.apache.hc.core5.reactor.IOReactorStatus;
48  import org.apache.hc.core5.util.Args;
49  import org.apache.hc.core5.util.TimeValue;
50  
51  /**
52   * Base implementation of {@link HttpAsyncClient} that also implements {@link ModalCloseable}.
53   *
54   * @since 5.0
55   */
56  @Contract(threading = ThreadingBehavior.STATELESS)
57  public abstract class CloseableHttpAsyncClient implements HttpAsyncClient, ModalCloseable {
58  
59      public abstract void start();
60  
61      public abstract IOReactorStatus getStatus();
62  
63      public abstract void awaitShutdown(TimeValue waitTime) throws InterruptedException;
64  
65      public abstract void initiateShutdown();
66  
67      protected abstract <T> Future<T> doExecute(
68              final HttpHost target,
69              final AsyncRequestProducer requestProducer,
70              final AsyncResponseConsumer<T> responseConsumer,
71              final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
72              final HttpContext context,
73              final FutureCallback<T> callback);
74  
75      public final <T> Future<T> execute(
76              final HttpHost target,
77              final AsyncRequestProducer requestProducer,
78              final AsyncResponseConsumer<T> responseConsumer,
79              final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
80              final HttpContext context,
81              final FutureCallback<T> callback) {
82          Args.notNull(requestProducer, "Request producer");
83          Args.notNull(responseConsumer, "Response consumer");
84          return doExecute(target, requestProducer, responseConsumer, pushHandlerFactory, context, callback);
85      }
86  
87      @Override
88      public final <T> Future<T> execute(
89              final AsyncRequestProducer requestProducer,
90              final AsyncResponseConsumer<T> responseConsumer,
91              final HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
92              final HttpContext context,
93              final FutureCallback<T> callback) {
94          Args.notNull(requestProducer, "Request producer");
95          Args.notNull(responseConsumer, "Response consumer");
96          return doExecute(null, requestProducer, responseConsumer, pushHandlerFactory, context, callback);
97      }
98  
99      public final <T> Future<T> execute(
100             final AsyncRequestProducer requestProducer,
101             final AsyncResponseConsumer<T> responseConsumer,
102             final HttpContext context,
103             final FutureCallback<T> callback) {
104         Args.notNull(requestProducer, "Request producer");
105         Args.notNull(responseConsumer, "Response consumer");
106         return execute(requestProducer, responseConsumer, null, context, callback);
107     }
108 
109     public final <T> Future<T> execute(
110             final AsyncRequestProducer requestProducer,
111             final AsyncResponseConsumer<T> responseConsumer,
112             final FutureCallback<T> callback) {
113         Args.notNull(requestProducer, "Request producer");
114         Args.notNull(responseConsumer, "Response consumer");
115         return execute(requestProducer, responseConsumer, null, callback);
116     }
117 
118     public final Future<SimpleHttpResponse> execute(
119             final SimpleHttpRequest request,
120             final HttpContext context,
121             final FutureCallback<SimpleHttpResponse> callback) {
122         Args.notNull(request, "Request");
123         return execute(SimpleRequestProducer.create(request), SimpleResponseConsumer.create(), context, callback);
124     }
125 
126     public final Future<SimpleHttpResponse> execute(
127             final SimpleHttpRequest request,
128             final FutureCallback<SimpleHttpResponse> callback) {
129         return execute(request, null, callback);
130     }
131 
132     /**
133      * @deprecated Use {@link org.apache.hc.core5.http.impl.routing.RequestRouter}
134      * at the construction time
135      */
136     @Deprecated
137     public abstract void register(String hostname, String uriPattern, Supplier<AsyncPushConsumer> supplier);
138 
139     /**
140      * @deprecated Use {@link org.apache.hc.core5.http.impl.routing.RequestRouter}
141      * at the construction time
142      */
143     @Deprecated
144     public final void register(final String uriPattern, final Supplier<AsyncPushConsumer> supplier) {
145         register(null, uriPattern, supplier);
146     }
147 
148 }