| 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 |
final BasicFuture<T> future = new BasicFuture<T>(callback); |
| 94 |
final HttpClientContext localcontext = HttpClientContext.adapt( |
| 95 |
context != null ? context : new BasicHttpContext()); |
| 96 |
|
| 97 |
final MinimalClientExchangeHandlerImpl<T> handler = new MinimalClientExchangeHandlerImpl<T>( |
| 98 |
this.log, |
| 99 |
requestProducer, |
| 100 |
responseConsumer, |
| 101 |
localcontext, |
| 102 |
future, |
| 103 |
this.connmgr, |
| 104 |
this.httpProcessor, |
| 105 |
this.connReuseStrategy, |
| 106 |
this.keepaliveStrategy); |
| 107 |
execute(handler); |
| 108 |
return new FutureWrapper<T>(future, handler); |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public <T> Future<List<T>> execute( |
| 113 |
final HttpHost target, |
| 114 |
final List<? extends HttpAsyncRequestProducer> requestProducers, |
| 115 |
final List<? extends HttpAsyncResponseConsumer<T>> responseConsumers, |
| 116 |
final HttpContext context, |
| 117 |
final FutureCallback<List<T>> callback) { |
| 118 |
final BasicFuture<List<T>> future = new BasicFuture<List<T>>(callback); |
| 119 |
final HttpClientContext localcontext = HttpClientContext.adapt( |
| 120 |
context != null ? context : new BasicHttpContext()); |
| 121 |
final PipeliningClientExchangeHandlerImpl<T> handler = new PipeliningClientExchangeHandlerImpl<T>( |
| 122 |
this.log, |
| 123 |
target, |
| 124 |
requestProducers, |
| 125 |
responseConsumers, |
| 126 |
localcontext, |
| 127 |
future, |
| 128 |
this.connmgr, |
| 129 |
this.httpProcessor, |
| 130 |
this.connReuseStrategy, |
| 131 |
this.keepaliveStrategy); |
| 132 |
execute(handler); |
| 133 |
return new FutureWrapper<List<T>>(future, handler); |
| 134 |
} |
| 135 |
|
| 136 |
} |