upgrade to new HttpClient APIs

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1589148 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shai Erera 2014-04-22 14:53:01 +00:00
parent 029f4fb5a8
commit c06db19203
5 changed files with 39 additions and 54 deletions

View File

@ -29,12 +29,12 @@ import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.util.IOUtils;
@ -46,18 +46,10 @@ import org.apache.lucene.util.IOUtils;
* */
public abstract class HttpClientBase implements Closeable {
/**
* Default connection timeout for this client, in milliseconds.
*
* @see #setConnectionTimeout(int)
*/
/** Default connection timeout for this client, in milliseconds. */
public static final int DEFAULT_CONNECTION_TIMEOUT = 1000;
/**
* Default socket timeout for this client, in milliseconds.
*
* @see #setSoTimeout(int)
*/
/** Default socket timeout for this client, in milliseconds. */
public static final int DEFAULT_SO_TIMEOUT = 60000;
// TODO compression?
@ -67,38 +59,29 @@ public abstract class HttpClientBase implements Closeable {
private volatile boolean closed = false;
private final HttpClient httpc;
private final CloseableHttpClient httpc;
private final RequestConfig defaultConfig;
/**
* @param conMgr connection manager to use for this http client.
* <b>NOTE:</b>The provided {@link ClientConnectionManager} will not be
* {@link ClientConnectionManager#shutdown()} by this class.
* @param conMgr
* connection manager to use for this http client. <b>NOTE:</b>The
* provided {@link HttpClientConnectionManager} will not be
* {@link HttpClientConnectionManager#shutdown()} by this class.
* @param defaultConfig
* the default {@link RequestConfig} to set on the client. If
* {@code null} a default config is created w/ the default connection
* and socket timeouts.
*/
protected HttpClientBase(String host, int port, String path, ClientConnectionManager conMgr) {
protected HttpClientBase(String host, int port, String path, HttpClientConnectionManager conMgr, RequestConfig defaultConfig) {
url = normalizedURL(host, port, path);
httpc = new DefaultHttpClient(conMgr);
setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
setSoTimeout(DEFAULT_SO_TIMEOUT);
}
/**
* Set the connection timeout for this client, in milliseconds. This setting
* is used to modify {@link HttpConnectionParams#setConnectionTimeout}.
*
* @param timeout timeout to set, in millisecopnds
*/
public void setConnectionTimeout(int timeout) {
HttpConnectionParams.setConnectionTimeout(httpc.getParams(), timeout);
}
/**
* Set the socket timeout for this client, in milliseconds. This setting
* is used to modify {@link HttpConnectionParams#setSoTimeout}.
*
* @param timeout timeout to set, in millisecopnds
*/
public void setSoTimeout(int timeout) {
HttpConnectionParams.setSoTimeout(httpc.getParams(), timeout);
if (defaultConfig == null) {
this.defaultConfig = RequestConfig.custom()
.setConnectionRequestTimeout(DEFAULT_CONNECTION_TIMEOUT)
.setSocketTimeout(DEFAULT_SO_TIMEOUT).build();
} else {
this.defaultConfig = defaultConfig;
}
httpc = HttpClientBuilder.create().setConnectionManager(conMgr).setDefaultRequestConfig(this.defaultConfig).build();
}
/** Throws {@link AlreadyClosedException} if this client is already closed. */
@ -285,6 +268,7 @@ public abstract class HttpClientBase implements Closeable {
@Override
public void close() throws IOException {
httpc.close();
closed = true;
}

View File

@ -23,7 +23,7 @@ import java.io.InputStream;
import java.util.concurrent.Callable;
import org.apache.http.HttpResponse;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.lucene.replicator.Replicator;
import org.apache.lucene.replicator.Revision;
import org.apache.lucene.replicator.SessionToken;
@ -38,8 +38,8 @@ import org.apache.lucene.replicator.http.ReplicationService.ReplicationAction;
public class HttpReplicator extends HttpClientBase implements Replicator {
/** Construct with specified connection manager. */
public HttpReplicator(String host, int port, String path, ClientConnectionManager conMgr) {
super(host, port, path, conMgr);
public HttpReplicator(String host, int port, String path, HttpClientConnectionManager conMgr) {
super(host, port, path, conMgr, null);
}
@Override

View File

@ -141,6 +141,7 @@ public class IndexAndTaxonomyRevisionTest extends ReplicatorTestCase {
Revision rev = new IndexAndTaxonomyRevision(indexWriter, taxoWriter);
for (Entry<String,List<RevisionFile>> e : rev.getSourceFiles().entrySet()) {
String source = e.getKey();
@SuppressWarnings("resource") // silly, both directories are closed in the end
Directory dir = source.equals(IndexAndTaxonomyRevision.INDEX_SOURCE) ? indexDir : taxoDir;
for (RevisionFile file : e.getValue()) {
IndexInput src = dir.openInput(file.fileName, IOContext.READONCE);

View File

@ -19,8 +19,8 @@ package org.apache.lucene.replicator;
import java.util.Random;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
import org.eclipse.jetty.server.Connector;
@ -38,7 +38,7 @@ import org.junit.AfterClass;
@SuppressCodecs("Lucene3x")
public abstract class ReplicatorTestCase extends LuceneTestCase {
private static ClientConnectionManager clientConnectionManager;
private static HttpClientConnectionManager clientConnectionManager;
@AfterClass
public static void afterClassReplicatorTestCase() throws Exception {
@ -144,15 +144,15 @@ public abstract class ReplicatorTestCase extends LuceneTestCase {
}
/**
* Returns a {@link ClientConnectionManager}.
* Returns a {@link HttpClientConnectionManager}.
* <p>
* <b>NOTE:</b> do not {@link ClientConnectionManager#shutdown()} this
* <b>NOTE:</b> do not {@link HttpClientConnectionManager#shutdown()} this
* connection manager, it will be shutdown automatically after all tests have
* finished.
*/
public static synchronized ClientConnectionManager getClientConnectionManager() {
public static synchronized HttpClientConnectionManager getClientConnectionManager() {
if (clientConnectionManager == null) {
PoolingClientConnectionManager ccm = new PoolingClientConnectionManager();
PoolingHttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager();
ccm.setDefaultMaxPerRoute(128);
ccm.setMaxTotal(128);
clientConnectionManager = ccm;

View File

@ -21,7 +21,7 @@ import java.io.File;
import java.io.IOException;
import java.util.Collections;
import org.apache.http.impl.conn.BasicClientConnectionManager;
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
@ -130,7 +130,7 @@ public class HttpReplicatorTest extends ReplicatorTestCase {
public void testServerErrors() throws Exception {
// tests the behaviour of the client when the server sends an error
// must use BasicClientConnectionManager to test whether the client is closed correctly
BasicClientConnectionManager conMgr = new BasicClientConnectionManager();
BasicHttpClientConnectionManager conMgr = new BasicHttpClientConnectionManager();
Replicator replicator = new HttpReplicator(host, port, ReplicationService.REPLICATION_CONTEXT + "/s1", conMgr);
ReplicationClient client = new ReplicationClient(replicator, new IndexReplicationHandler(handlerIndexDir, null),
new PerSessionDirectoryFactory(clientWorkDir));