THRIFT-1498:Allow TThreadedPoolServer.Args to pass a ExecutorService
Client: java
Patch: Scott Chen

Adds ability to assign a ExecutorService from TThreadedPoolServer.Args.



git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1236505 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/java/src/org/apache/thrift/server/TThreadPoolServer.java b/lib/java/src/org/apache/thrift/server/TThreadPoolServer.java
index 85537bf..0b037d8 100644
--- a/lib/java/src/org/apache/thrift/server/TThreadPoolServer.java
+++ b/lib/java/src/org/apache/thrift/server/TThreadPoolServer.java
@@ -45,6 +45,7 @@
   public static class Args extends AbstractServerArgs<Args> {
     public int minWorkerThreads = 5;
     public int maxWorkerThreads = Integer.MAX_VALUE;
+    public ExecutorService executorService;
     public int stopTimeoutVal = 60;
     public TimeUnit stopTimeoutUnit = TimeUnit.SECONDS;
 
@@ -61,6 +62,11 @@
       maxWorkerThreads = n;
       return this;
     }
+
+    public Args executorService(ExecutorService executorService) {
+      this.executorService = executorService;
+      return this;
+    }
   }
 
   // Executor service for handling client connections
@@ -76,17 +82,21 @@
   public TThreadPoolServer(Args args) {
     super(args);
 
-    SynchronousQueue<Runnable> executorQueue =
-      new SynchronousQueue<Runnable>();
-
     stopTimeoutUnit = args.stopTimeoutUnit;
     stopTimeoutVal = args.stopTimeoutVal;
 
-    executorService_ = new ThreadPoolExecutor(args.minWorkerThreads,
-                                              args.maxWorkerThreads,
-                                              60,
-                                              TimeUnit.SECONDS,
-                                              executorQueue);
+    executorService_ = args.executorService != null ?
+        args.executorService : createDefaultExecutorService(args);
+  }
+
+  private static ExecutorService createDefaultExecutorService(Args args) {
+    SynchronousQueue<Runnable> executorQueue =
+      new SynchronousQueue<Runnable>();
+    return new ThreadPoolExecutor(args.minWorkerThreads,
+                                  args.maxWorkerThreads,
+                                  60,
+                                  TimeUnit.SECONDS,
+                                  executorQueue);
   }