Repository: incubator-juneau
Updated Branches:
  refs/heads/master fbcd1890b -> 11aa7625f


Add default executor to RestClient.

Project: http://git-wip-us.apache.org/repos/asf/incubator-juneau/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-juneau/commit/11aa7625
Tree: http://git-wip-us.apache.org/repos/asf/incubator-juneau/tree/11aa7625
Diff: http://git-wip-us.apache.org/repos/asf/incubator-juneau/diff/11aa7625

Branch: refs/heads/master
Commit: 11aa7625f8d13273b368057434bda5cb77c8f9f7
Parents: fbcd189
Author: JamesBognar <[email protected]>
Authored: Sat Apr 8 14:24:25 2017 -0400
Committer: JamesBognar <[email protected]>
Committed: Sat Apr 8 14:24:25 2017 -0400

----------------------------------------------------------------------
 .../org/apache/juneau/rest/client/RestCall.java | 28 ++++++--------------
 .../apache/juneau/rest/client/RestClient.java   | 14 ++++++++--
 .../juneau/rest/client/RestClientBuilder.java   |  5 +++-
 .../juneau/rest/test/ClientFuturesTest.java     | 26 +++++++-----------
 4 files changed, 34 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/11aa7625/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java
----------------------------------------------------------------------
diff --git 
a/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java 
b/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java
index 3e36cf3..52af23f 100644
--- 
a/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java
+++ 
b/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java
@@ -81,7 +81,6 @@ public final class RestCall {
        private Serializer serializer;
        private Parser parser;
        private URIBuilder uriBuilder;
-       private final ExecutorService executorService;
 
        /**
         * Constructs a REST call with the specified method name.
@@ -101,7 +100,6 @@ public final class RestCall {
                this.retryInterval = client.retryInterval;
                this.serializer = client.serializer;
                this.parser = client.parser;
-               this.executorService = client.executorService;
                uriBuilder = new URIBuilder(uri);
        }
 
@@ -1046,14 +1044,13 @@ public final class RestCall {
 
        /**
         * Same as {@link #run()} but allows you to run the call asynchronously.
-        * <p>
-        * This method cannot be used unless the executor service was defined 
on the client using {@link 
RestClientBuilder#executorService(ExecutorService,boolean)}.
         *
         * @return The HTTP status code.
         * @throws RestCallException If the executor service was not defined.
+        * @see RestClientBuilder#executorService(ExecutorService, boolean) for 
defining the executor service for creating {@link Future Futures}.
         */
        public Future<Integer> runFuture() throws RestCallException {
-               return getExecutorService().submit(
+               return client.getExecutorService(true).submit(
                        new Callable<Integer>() {
                                @Override /* Callable */
                                public Integer call() throws Exception {
@@ -1326,14 +1323,13 @@ public final class RestCall {
 
        /**
         * Same as {@link #getResponse(Class)} but allows you to run the call 
asynchronously.
-        * <p>
-        * This method cannot be used unless the executor service was defined 
on the client using {@link 
RestClientBuilder#executorService(ExecutorService,boolean)}.
         *
         * @return The response as a string.
         * @throws RestCallException If the executor service was not defined.
+        * @see RestClientBuilder#executorService(ExecutorService, boolean) for 
defining the executor service for creating {@link Future Futures}.
         */
        public Future<String> getResponseAsStringFuture() throws 
RestCallException {
-               return getExecutorService().submit(
+               return client.getExecutorService(true).submit(
                        new Callable<String>() {
                                @Override /* Callable */
                                public String call() throws Exception {
@@ -1381,17 +1377,16 @@ public final class RestCall {
 
        /**
         * Same as {@link #getResponse(Class)} but allows you to run the call 
asynchronously.
-        * <p>
-        * This method cannot be used unless the executor service was defined 
on the client using {@link 
RestClientBuilder#executorService(ExecutorService,boolean)}.
         *
         * @param <T> The class type of the object being created.
         * See {@link #getResponse(Type, Type...)} for details.
         * @param type The object type to create.
         * @return The parsed object.
         * @throws RestCallException If the executor service was not defined.
+        * @see RestClientBuilder#executorService(ExecutorService, boolean) for 
defining the executor service for creating {@link Future Futures}.
         */
        public <T> Future<T> getResponseFuture(final Class<T> type) throws 
RestCallException {
-               return getExecutorService().submit(
+               return client.getExecutorService(true).submit(
                        new Callable<T>() {
                                @Override /* Callable */
                                public T call() throws Exception {
@@ -1455,8 +1450,6 @@ public final class RestCall {
 
        /**
         * Same as {@link #getResponse(Class)} but allows you to run the call 
asynchronously.
-        * <p>
-        * This method cannot be used unless the executor service was defined 
on the client using {@link 
RestClientBuilder#executorService(ExecutorService,boolean)}.
         *
         * @param <T> The class type of the object being created.
         * See {@link #getResponse(Type, Type...)} for details.
@@ -1467,9 +1460,10 @@ public final class RestCall {
         *      <br>Ignored if the main type is not a map or collection.
         * @return The parsed object.
         * @throws RestCallException If the executor service was not defined.
+        * @see RestClientBuilder#executorService(ExecutorService, boolean) for 
defining the executor service for creating {@link Future Futures}.
         */
        public <T> Future<T> getResponseFuture(final Type type, final 
Type...args) throws RestCallException {
-               return getExecutorService().submit(
+               return client.getExecutorService(true).submit(
                        new Callable<T>() {
                                @Override /* Callable */
                                public T call() throws Exception {
@@ -1591,12 +1585,6 @@ public final class RestCall {
                return this;
        }
 
-       private ExecutorService getExecutorService() throws RestCallException {
-               if (executorService == null)
-                       throw new RestCallException("Future method cannot be 
called.  Executor service was not defined via the 
RestClientBuilder.executorService() method.");
-               return executorService;
-       }
-
        /**
         * Adds a {@link RestCallLogger} to the list of interceptors on this 
class.
         *

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/11aa7625/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
----------------------------------------------------------------------
diff --git 
a/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
 
b/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
index 12b578f..7b8e0c0 100644
--- 
a/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
+++ 
b/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
@@ -67,8 +67,8 @@ public class RestClient extends CoreObject {
        final long retryInterval;
        final boolean debug;
        final RestCallInterceptor[] interceptors;
-       final ExecutorService executorService;
-       final boolean executorServiceShutdownOnClose;
+       private volatile ExecutorService executorService;
+       boolean executorServiceShutdownOnClose = true;
 
        /**
         * Create a new REST client.
@@ -521,6 +521,16 @@ public class RestClient extends CoreObject {
                return new URI(s);
        }
 
+       ExecutorService getExecutorService(boolean create) {
+               if (executorService != null || ! create)
+                       return executorService;
+               synchronized(this) {
+                       if (executorService == null)
+                               executorService = new ThreadPoolExecutor(1, 1, 
30, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
+                       return executorService;
+               }
+       }
+
        @Override
        protected void finalize() throws Throwable {
                if (! isClosed && ! keepHttpClientOpen) {

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/11aa7625/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClientBuilder.java
----------------------------------------------------------------------
diff --git 
a/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClientBuilder.java
 
b/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClientBuilder.java
index 70f6970..92a0ed8 100644
--- 
a/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClientBuilder.java
+++ 
b/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClientBuilder.java
@@ -429,13 +429,16 @@ public class RestClientBuilder extends CoreObjectBuilder {
        /**
         * Defines the executor service to use when calling future methods on 
the {@link RestCall} class.
         * <p>
-        * You must specify the executor service if you want to use any of the 
following methods:
+        * This executor service is used to create {@link Future} objects on 
the following methods:
         * <ul>
         *      <li>{@link RestCall#runFuture()}
         *      <li>{@link RestCall#getResponseFuture(Class)}
         *      <li>{@link RestCall#getResponseFuture(Type,Type...)}
         *      <li>{@link RestCall#getResponseAsString()}
         * </ul>
+        * <p>
+        * The default executor service is a single-threaded {@link 
ThreadPoolExecutor} with a 30 second timeout
+        * and a queue size of 10.
         *
         * @param executorService The executor service.
         * @param shutdownOnClose Call {@link ExecutorService#shutdown()} when 
{@link RestClient#close()} is called.

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/11aa7625/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/ClientFuturesTest.java
----------------------------------------------------------------------
diff --git 
a/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/ClientFuturesTest.java
 
b/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/ClientFuturesTest.java
index b361ba5..d89d9de 100644
--- 
a/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/ClientFuturesTest.java
+++ 
b/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/ClientFuturesTest.java
@@ -30,21 +30,15 @@ public class ClientFuturesTest extends RestTestcase {
        
//====================================================================================================
        @Test
        public void test() throws Exception {
-               RestClient client = null;
-               try {
-                       ExecutorService es = new ThreadPoolExecutor(1, 1, 5, 
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1));
-                       client = TestMicroservice.client().executorService(es, 
true).build();
-
-                       Future<Integer> f = client.doGet(URL).runFuture();
-                       assertEquals(200, f.get().intValue());
-
-                       Future<ObjectMap> f2 = 
client.doGet(URL).getResponseFuture(ObjectMap.class);
-                       assertObjectEquals("{foo:'bar'}", f2.get());
-
-                       Future<String> f3 = 
client.doGet(URL).getResponseAsStringFuture();
-                       assertObjectEquals("'{\"foo\":\"bar\"}'", f3.get());
-               } finally {
-                       client.closeQuietly();
-               }
+               RestClient client = TestMicroservice.DEFAULT_CLIENT;
+
+               Future<Integer> f = client.doGet(URL).runFuture();
+               assertEquals(200, f.get().intValue());
+
+               Future<ObjectMap> f2 = 
client.doGet(URL).getResponseFuture(ObjectMap.class);
+               assertObjectEquals("{foo:'bar'}", f2.get());
+
+               Future<String> f3 = 
client.doGet(URL).getResponseAsStringFuture();
+               assertObjectEquals("'{\"foo\":\"bar\"}'", f3.get());
        }
 }
\ No newline at end of file

Reply via email to