From: Jake Farrell Date: Fri, 27 Jan 2012 03:19:01 +0000 (+0000) Subject: THRIFT-1498:Allow TThreadedPoolServer.Args to pass a ExecutorService X-Git-Tag: 0.9.1~456 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=100f616afd0f31346f958339cc83f71330fd7593;p=common%2Fthrift.git 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 85537bf2..0b037d80 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 class TThreadPoolServer extends TServer { public static class Args extends AbstractServerArgs { 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 @@ public class TThreadPoolServer extends TServer { 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 class TThreadPoolServer extends TServer { public TThreadPoolServer(Args args) { super(args); - SynchronousQueue executorQueue = - new SynchronousQueue(); - 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 executorQueue = + new SynchronousQueue(); + return new ThreadPoolExecutor(args.minWorkerThreads, + args.maxWorkerThreads, + 60, + TimeUnit.SECONDS, + executorQueue); }