You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2014/04/22 16:53:01 UTC

svn commit: r1589148 - in /lucene/dev/trunk/lucene/replicator/src: java/org/apache/lucene/replicator/http/ test/org/apache/lucene/replicator/ test/org/apache/lucene/replicator/http/

Author: shaie
Date: Tue Apr 22 14:53:01 2014
New Revision: 1589148

URL: http://svn.apache.org/r1589148
Log:
upgrade to new HttpClient APIs

Modified:
    lucene/dev/trunk/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpClientBase.java
    lucene/dev/trunk/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpReplicator.java
    lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java
    lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/ReplicatorTestCase.java
    lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/http/HttpReplicatorTest.java

Modified: lucene/dev/trunk/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpClientBase.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpClientBase.java?rev=1589148&r1=1589147&r2=1589148&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpClientBase.java (original)
+++ lucene/dev/trunk/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpClientBase.java Tue Apr 22 14:53:01 2014
@@ -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 imp
   
   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 imp
   
   @Override
   public void close() throws IOException {
+    httpc.close();
     closed = true;
   }
   

Modified: lucene/dev/trunk/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpReplicator.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpReplicator.java?rev=1589148&r1=1589147&r2=1589148&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpReplicator.java (original)
+++ lucene/dev/trunk/lucene/replicator/src/java/org/apache/lucene/replicator/http/HttpReplicator.java Tue Apr 22 14:53:01 2014
@@ -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
 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

Modified: lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java?rev=1589148&r1=1589147&r2=1589148&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java (original)
+++ lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java Tue Apr 22 14:53:01 2014
@@ -141,6 +141,7 @@ public class IndexAndTaxonomyRevisionTes
       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);

Modified: lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/ReplicatorTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/ReplicatorTestCase.java?rev=1589148&r1=1589147&r2=1589148&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/ReplicatorTestCase.java (original)
+++ lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/ReplicatorTestCase.java Tue Apr 22 14:53:01 2014
@@ -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
   }
   
   /**
-   * 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;

Modified: lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/http/HttpReplicatorTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/http/HttpReplicatorTest.java?rev=1589148&r1=1589147&r2=1589148&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/http/HttpReplicatorTest.java (original)
+++ lucene/dev/trunk/lucene/replicator/src/test/org/apache/lucene/replicator/http/HttpReplicatorTest.java Tue Apr 22 14:53:01 2014
@@ -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 
   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));