You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2013/08/28 16:26:02 UTC

svn commit: r1518216 - in /httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client: CloseableHttpAsyncClient.java CloseableHttpAsyncClientBase.java InternalHttpAsyncClient.java MinimalHttpAsyncClient.java

Author: olegk
Date: Wed Aug 28 14:26:02 2013
New Revision: 1518216

URL: http://svn.apache.org/r1518216
Log:
Refactored life cycle methods of the internal and minimal CloseableHttpAsyncClient implementations

Added:
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/CloseableHttpAsyncClientBase.java   (with props)
Modified:
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/CloseableHttpAsyncClient.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/InternalHttpAsyncClient.java
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/MinimalHttpAsyncClient.java

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/CloseableHttpAsyncClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/CloseableHttpAsyncClient.java?rev=1518216&r1=1518215&r2=1518216&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/CloseableHttpAsyncClient.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/CloseableHttpAsyncClient.java Wed Aug 28 14:26:02 2013
@@ -48,6 +48,8 @@ import org.apache.http.util.Args;
 
 public abstract class CloseableHttpAsyncClient implements HttpAsyncClient, Closeable {
 
+    public abstract boolean isRunning();
+
     public abstract void start();
 
     public <T> Future<T> execute(

Added: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/CloseableHttpAsyncClientBase.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/CloseableHttpAsyncClientBase.java?rev=1518216&view=auto
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/CloseableHttpAsyncClientBase.java (added)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/CloseableHttpAsyncClientBase.java Wed Aug 28 14:26:02 2013
@@ -0,0 +1,109 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.impl.nio.client;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.http.nio.conn.NHttpClientConnectionManager;
+import org.apache.http.nio.reactor.IOEventDispatch;
+
+import java.io.IOException;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.atomic.AtomicReference;
+
+abstract class CloseableHttpAsyncClientBase extends CloseableHttpAsyncClient {
+
+    private final Log log = LogFactory.getLog(getClass());
+
+    static enum Status { INACTIVE, ACTIVE, STOPPED }
+
+    private final NHttpClientConnectionManager connmgr;
+    private final Thread reactorThread;
+
+    private final AtomicReference<Status> status;
+
+    public CloseableHttpAsyncClientBase(
+            final NHttpClientConnectionManager connmgr,
+            final ThreadFactory threadFactory) {
+        super();
+        this.connmgr = connmgr;
+        this.reactorThread = threadFactory.newThread(new Runnable() {
+
+            public void run() {
+                doExecute();
+            }
+
+        });        this.status = new AtomicReference<Status>(Status.INACTIVE);
+    }
+
+    private void doExecute() {
+        try {
+            final IOEventDispatch ioEventDispatch = new InternalIODispatch();
+            this.connmgr.execute(ioEventDispatch);
+        } catch (final Exception ex) {
+            this.log.error("I/O reactor terminated abnormally", ex);
+        } finally {
+            this.status.set(Status.STOPPED);
+        }
+    }
+
+    @Override
+    public void start() {
+        if (this.status.compareAndSet(Status.INACTIVE, Status.ACTIVE)) {
+            this.reactorThread.start();
+        }
+    }
+
+    public void shutdown() {
+        if (this.status.compareAndSet(Status.ACTIVE, Status.STOPPED)) {
+            try {
+                this.connmgr.shutdown();
+            } catch (final IOException ex) {
+                this.log.error("I/O error shutting down connection manager", ex);
+            }
+            try {
+                this.reactorThread.join();
+            } catch (final InterruptedException ex) {
+                Thread.currentThread().interrupt();
+            }
+        }
+    }
+
+    public void close() {
+        shutdown();
+    }
+
+    @Override
+    public boolean isRunning() {
+        return getStatus() == Status.ACTIVE;
+    }
+
+    Status getStatus() {
+        return this.status.get();
+    }
+
+}

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/CloseableHttpAsyncClientBase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/CloseableHttpAsyncClientBase.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/CloseableHttpAsyncClientBase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/InternalHttpAsyncClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/InternalHttpAsyncClient.java?rev=1518216&r1=1518215&r2=1518216&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/InternalHttpAsyncClient.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/InternalHttpAsyncClient.java Wed Aug 28 14:26:02 2013
@@ -26,7 +26,6 @@
  */
 package org.apache.http.impl.nio.client;
 
-import java.io.IOException;
 import java.util.concurrent.Future;
 import java.util.concurrent.ThreadFactory;
 
@@ -45,13 +44,11 @@ import org.apache.http.cookie.CookieSpec
 import org.apache.http.nio.conn.NHttpClientConnectionManager;
 import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
 import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
-import org.apache.http.nio.reactor.IOEventDispatch;
-import org.apache.http.nio.reactor.IOReactorStatus;
 import org.apache.http.protocol.BasicHttpContext;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.util.Asserts;
 
-class InternalHttpAsyncClient extends CloseableHttpAsyncClient {
+class InternalHttpAsyncClient extends CloseableHttpAsyncClientBase {
 
     private final Log log = LogFactory.getLog(getClass());
 
@@ -62,9 +59,6 @@ class InternalHttpAsyncClient extends Cl
     private final CookieStore cookieStore;
     private final CredentialsProvider credentialsProvider;
     private final RequestConfig defaultConfig;
-    private final Thread reactorThread;
-
-    private volatile IOReactorStatus status;
 
     public InternalHttpAsyncClient(
             final NHttpClientConnectionManager connmgr,
@@ -75,7 +69,7 @@ class InternalHttpAsyncClient extends Cl
             final CredentialsProvider credentialsProvider,
             final RequestConfig defaultConfig,
             final ThreadFactory threadFactory) {
-        super();
+        super(connmgr, threadFactory);
         this.connmgr = connmgr;
         this.exec = exec;
         this.cookieSpecRegistry = cookieSpecRegistry;
@@ -83,58 +77,6 @@ class InternalHttpAsyncClient extends Cl
         this.cookieStore = cookieStore;
         this.credentialsProvider = credentialsProvider;
         this.defaultConfig = defaultConfig;
-        this.reactorThread = threadFactory.newThread(new Runnable() {
-
-            public void run() {
-                doExecute();
-            }
-
-        });
-        this.status = IOReactorStatus.INACTIVE;
-    }
-
-    private void doExecute() {
-        try {
-            final IOEventDispatch ioEventDispatch = new InternalIODispatch();
-            this.connmgr.execute(ioEventDispatch);
-        } catch (final Exception ex) {
-            this.log.error("I/O reactor terminated abnormally", ex);
-        } finally {
-            this.status = IOReactorStatus.SHUT_DOWN;
-        }
-    }
-
-    public IOReactorStatus getStatus() {
-        return this.status;
-    }
-
-    @Override
-    public void start() {
-        this.status = IOReactorStatus.ACTIVE;
-        this.reactorThread.start();
-    }
-
-    public void shutdown() {
-        if (this.status.compareTo(IOReactorStatus.ACTIVE) > 0) {
-            return;
-        }
-        this.status = IOReactorStatus.SHUTDOWN_REQUEST;
-        try {
-            this.connmgr.shutdown();
-        } catch (final IOException ex) {
-            this.log.error("I/O error shutting down connection manager", ex);
-        }
-        if (this.reactorThread != null) {
-            try {
-                this.reactorThread.join();
-            } catch (final InterruptedException ex) {
-                Thread.currentThread().interrupt();
-            }
-        }
-    }
-
-    public void close() {
-        shutdown();
     }
 
     private void setupContext(final HttpClientContext context) {
@@ -166,8 +108,9 @@ class InternalHttpAsyncClient extends Cl
             final HttpAsyncResponseConsumer<T> responseConsumer,
             final HttpContext context,
             final FutureCallback<T> callback) {
-        Asserts.check(this.status == IOReactorStatus.ACTIVE, "Request cannot be executed; " +
-                    "I/O reactor status: %s", this.status);
+        final Status status = getStatus();
+        Asserts.check(status == Status.ACTIVE, "Request cannot be executed; " +
+                "I/O reactor status: %s", status);
         final BasicFuture<T> future = new BasicFuture<T>(callback);
         final HttpClientContext localcontext = HttpClientContext.adapt(
             context != null ? context : new BasicHttpContext());

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/MinimalHttpAsyncClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/MinimalHttpAsyncClient.java?rev=1518216&r1=1518215&r2=1518216&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/MinimalHttpAsyncClient.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/MinimalHttpAsyncClient.java Wed Aug 28 14:26:02 2013
@@ -26,13 +26,13 @@
  */
 package org.apache.http.impl.nio.client;
 
-import java.io.IOException;
+import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
+import java.util.concurrent.ThreadFactory;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.http.ConnectionReuseStrategy;
-import org.apache.http.HttpRequestInterceptor;
 import org.apache.http.client.protocol.HttpClientContext;
 import org.apache.http.client.protocol.RequestClientConnControl;
 import org.apache.http.concurrent.BasicFuture;
@@ -43,8 +43,6 @@ import org.apache.http.impl.client.Defau
 import org.apache.http.nio.conn.NHttpClientConnectionManager;
 import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
 import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
-import org.apache.http.nio.reactor.IOEventDispatch;
-import org.apache.http.nio.reactor.IOReactorStatus;
 import org.apache.http.protocol.BasicHttpContext;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.protocol.HttpProcessor;
@@ -55,7 +53,7 @@ import org.apache.http.protocol.RequestU
 import org.apache.http.util.Asserts;
 import org.apache.http.util.VersionInfo;
 
-class MinimalHttpAsyncClient extends CloseableHttpAsyncClient {
+class MinimalHttpAsyncClient extends CloseableHttpAsyncClientBase {
 
     private final Log log = LogFactory.getLog(getClass());
 
@@ -63,76 +61,24 @@ class MinimalHttpAsyncClient extends Clo
     private final HttpProcessor httpProcessor;
     private final ConnectionReuseStrategy connReuseStrategy;
     private final ConnectionKeepAliveStrategy keepaliveStrategy;
-    private final Thread reactorThread;
-
-    private volatile IOReactorStatus status;
 
     public MinimalHttpAsyncClient(
-            final NHttpClientConnectionManager connmgr) {
-        super();
+            final NHttpClientConnectionManager connmgr,
+            final ThreadFactory threadFactory) {
+        super(connmgr, threadFactory);
         this.connmgr = connmgr;
-        this.httpProcessor = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
-                new RequestContent(),
+        this.httpProcessor = new ImmutableHttpProcessor(new RequestContent(),
                 new RequestTargetHost(),
                 new RequestClientConnControl(),
                 new RequestUserAgent(VersionInfo.getUserAgent(
-                        "Apache-HttpAsyncClient", "org.apache.http.nio.client", getClass()))
-        });
+                        "Apache-HttpAsyncClient", "org.apache.http.nio.client", getClass())));
         this.connReuseStrategy = DefaultConnectionReuseStrategy.INSTANCE;
         this.keepaliveStrategy = DefaultConnectionKeepAliveStrategy.INSTANCE;
-        this.reactorThread = new Thread() {
-
-            @Override
-            public void run() {
-                doExecute();
-            }
-
-        };
-        this.status = IOReactorStatus.INACTIVE;
-    }
-
-    private void doExecute() {
-        try {
-            final IOEventDispatch ioEventDispatch = new InternalIODispatch();
-            this.connmgr.execute(ioEventDispatch);
-        } catch (final Exception ex) {
-            this.log.error("I/O reactor terminated abnormally", ex);
-        } finally {
-            this.status = IOReactorStatus.SHUT_DOWN;
-        }
-    }
-
-    public IOReactorStatus getStatus() {
-        return this.status;
     }
 
-    @Override
-    public void start() {
-        this.status = IOReactorStatus.ACTIVE;
-        this.reactorThread.start();
-    }
-
-    public void shutdown() {
-        if (this.status.compareTo(IOReactorStatus.ACTIVE) > 0) {
-            return;
-        }
-        this.status = IOReactorStatus.SHUTDOWN_REQUEST;
-        try {
-            this.connmgr.shutdown();
-        } catch (final IOException ex) {
-            this.log.error("I/O error shutting down connection manager", ex);
-        }
-        if (this.reactorThread != null) {
-            try {
-                this.reactorThread.join();
-            } catch (final InterruptedException ex) {
-                Thread.currentThread().interrupt();
-            }
-        }
-    }
-
-    public void close() {
-        shutdown();
+    public MinimalHttpAsyncClient(
+            final NHttpClientConnectionManager connmgr) {
+        this(connmgr, Executors.defaultThreadFactory());
     }
 
     public <T> Future<T> execute(
@@ -140,8 +86,9 @@ class MinimalHttpAsyncClient extends Clo
             final HttpAsyncResponseConsumer<T> responseConsumer,
             final HttpContext context,
             final FutureCallback<T> callback) {
-        Asserts.check(this.status == IOReactorStatus.ACTIVE, "Request cannot be executed; " +
-                "I/O reactor status: %s", this.status);
+        final Status status = getStatus();
+        Asserts.check(status == Status.ACTIVE, "Request cannot be executed; " +
+                "I/O reactor status: %s", status);
         final BasicFuture<T> future = new BasicFuture<T>(callback);
         final HttpClientContext localcontext = HttpClientContext.adapt(
             context != null ? context : new BasicHttpContext());