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.http.impl.nio.client;
28  
29  import java.util.List;
30  import java.util.concurrent.Executors;
31  import java.util.concurrent.Future;
32  import java.util.concurrent.ThreadFactory;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.apache.http.ConnectionReuseStrategy;
37  import org.apache.http.HttpHost;
38  import org.apache.http.client.protocol.HttpClientContext;
39  import org.apache.http.concurrent.BasicFuture;
40  import org.apache.http.concurrent.FutureCallback;
41  import org.apache.http.conn.ConnectionKeepAliveStrategy;
42  import org.apache.http.impl.DefaultConnectionReuseStrategy;
43  import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
44  import org.apache.http.nio.NHttpClientEventHandler;
45  import org.apache.http.nio.conn.NHttpClientConnectionManager;
46  import org.apache.http.nio.protocol.HttpAsyncRequestExecutor;
47  import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
48  import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
49  import org.apache.http.protocol.BasicHttpContext;
50  import org.apache.http.protocol.HttpContext;
51  import org.apache.http.protocol.HttpProcessor;
52  
53  class MinimalHttpAsyncClient extends CloseableHttpAsyncClientBase {
54  
55      private final Log log = LogFactory.getLog(getClass());
56  
57      private final NHttpClientConnectionManager connmgr;
58      private final HttpProcessor httpProcessor;
59      private final ConnectionReuseStrategy connReuseStrategy;
60      private final ConnectionKeepAliveStrategy keepaliveStrategy;
61  
62      public MinimalHttpAsyncClient(
63              final NHttpClientConnectionManager connmgr,
64              final ThreadFactory threadFactory,
65              final NHttpClientEventHandler eventHandler,
66              final HttpProcessor httpProcessor,
67              final ConnectionReuseStrategy connReuseStrategy,
68              final ConnectionKeepAliveStrategy keepaliveStrategy) {
69          super(connmgr, threadFactory, eventHandler);
70          this.connmgr = connmgr;
71          this.httpProcessor = httpProcessor;
72          this.connReuseStrategy = connReuseStrategy;
73          this.keepaliveStrategy = keepaliveStrategy;
74      }
75  
76      public MinimalHttpAsyncClient(
77              final NHttpClientConnectionManager connmgr,
78              final HttpProcessor httpProcessor) {
79          this(connmgr,
80                  Executors.defaultThreadFactory(),
81                  new HttpAsyncRequestExecutor(),
82                  httpProcessor,
83                  DefaultConnectionReuseStrategy.INSTANCE,
84                  DefaultConnectionKeepAliveStrategy.INSTANCE);
85      }
86  
87      @Override
88      public <T> Future<T> execute(
89              final HttpAsyncRequestProducer requestProducer,
90              final HttpAsyncResponseConsumer<T> responseConsumer,
91              final HttpContext context,
92              final FutureCallback<T> callback) {
93          ensureRunning();
94          final BasicFuture<T> future = new BasicFuture<T>(callback);
95          final HttpClientContext localcontext = HttpClientContext.adapt(
96              context != null ? context : new BasicHttpContext());
97  
98          @SuppressWarnings("resource")
99          final MinimalClientExchangeHandlerImpl<T> handler = new MinimalClientExchangeHandlerImpl<T>(
100             this.log,
101             requestProducer,
102             responseConsumer,
103             localcontext,
104             future,
105             this.connmgr,
106             this.httpProcessor,
107             this.connReuseStrategy,
108             this.keepaliveStrategy);
109         try {
110             handler.start();
111         } catch (final Exception ex) {
112             handler.failed(ex);
113         }
114         return new FutureWrapper<T>(future, handler);
115     }
116 
117     @Override
118     public <T> Future<List<T>> execute(
119             final HttpHost target,
120             final List<? extends HttpAsyncRequestProducer> requestProducers,
121             final List<? extends HttpAsyncResponseConsumer<T>> responseConsumers,
122             final HttpContext context,
123             final FutureCallback<List<T>> callback) {
124         ensureRunning();
125         final BasicFuture<List<T>> future = new BasicFuture<List<T>>(callback);
126         final HttpClientContext localcontext = HttpClientContext.adapt(
127                 context != null ? context : new BasicHttpContext());
128         @SuppressWarnings("resource")
129         final PipeliningClientExchangeHandlerImpl<T> handler = new PipeliningClientExchangeHandlerImpl<T>(
130                 this.log,
131                 target,
132                 requestProducers,
133                 responseConsumers,
134                 localcontext,
135                 future,
136                 this.connmgr,
137                 this.httpProcessor,
138                 this.connReuseStrategy,
139                 this.keepaliveStrategy);
140         try {
141             handler.start();
142         } catch (final Exception ex) {
143             handler.failed(ex);
144         }
145         return new FutureWrapper<List<T>>(future, handler);
146     }
147 
148 }