Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 1 | package com.facebook.thrift.server; |
| 2 | |
| 3 | import com.facebook.thrift.TException; |
| 4 | import com.facebook.thrift.TProcessor; |
| 5 | import com.facebook.thrift.transport.TServerTransport; |
| 6 | import com.facebook.thrift.transport.TTransport; |
| 7 | import com.facebook.thrift.transport.TTransportException; |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 8 | import com.facebook.thrift.transport.TTransportFactory; |
| 9 | import com.facebook.thrift.transport.TBaseTransportFactory; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 10 | |
| 11 | import java.util.concurrent.ExecutorService; |
| 12 | import java.util.concurrent.LinkedBlockingQueue; |
| 13 | import java.util.concurrent.ThreadPoolExecutor; |
| 14 | import java.util.concurrent.TimeUnit; |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * Server which uses Java's built in ThreadPool management to spawn off |
| 19 | * a worker pool that |
| 20 | * |
| 21 | * @author Mark Slee <mcslee@facebook.com> |
| 22 | */ |
| 23 | public class TThreadPoolServer extends TServer { |
| 24 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 25 | // Executor service for handling client connections |
| 26 | private ExecutorService executorService_; |
| 27 | |
| 28 | // Customizable server options |
| 29 | public static class Options extends TServer.Options { |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 30 | public int minWorkerThreads = 5; |
| 31 | public int maxWorkerThreads = Integer.MAX_VALUE; |
| 32 | } |
| 33 | |
| 34 | public TThreadPoolServer(TProcessor processor, |
| 35 | TServerTransport serverTransport) { |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 36 | this(processor, |
| 37 | serverTransport, |
| 38 | new TBaseTransportFactory(), |
| 39 | new Options()); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 40 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 41 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 42 | public TThreadPoolServer(TProcessor processor, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 43 | TServerTransport serverTransport, |
| 44 | TTransportFactory transportFactory, |
| 45 | Options options) { |
| 46 | super(processor, serverTransport, transportFactory, options); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 47 | serverTransport_ = serverTransport; |
| 48 | executorService_ = null; |
| 49 | |
| 50 | LinkedBlockingQueue<Runnable> executorQueue = |
| 51 | new LinkedBlockingQueue<Runnable>(); |
| 52 | |
| 53 | executorService_ = new ThreadPoolExecutor(options.minWorkerThreads, |
| 54 | options.maxWorkerThreads, |
| 55 | 60, |
| 56 | TimeUnit.SECONDS, |
| 57 | executorQueue); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | public void run() { |
| 62 | try { |
| 63 | serverTransport_.listen(); |
| 64 | } catch (TTransportException ttx) { |
| 65 | ttx.printStackTrace(); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | while (true) { |
| 70 | try { |
| 71 | TTransport client = serverTransport_.accept(); |
| 72 | WorkerProcess wp = new WorkerProcess(client); |
| 73 | executorService_.execute(wp); |
| 74 | } catch (TTransportException ttx) { |
| 75 | ttx.printStackTrace(); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private class WorkerProcess implements Runnable { |
| 81 | |
| 82 | /** |
| 83 | * Client that this services. |
| 84 | */ |
| 85 | private TTransport client_; |
| 86 | |
| 87 | /** |
| 88 | * Default constructor. |
| 89 | * |
| 90 | * @param client Transport to process |
| 91 | */ |
| 92 | private WorkerProcess(TTransport client) { |
| 93 | client_ = client; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Loops on processing a client forever |
| 98 | */ |
| 99 | public void run() { |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 100 | TTransport[] io = null; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 101 | try { |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 102 | io = transportFactory_.getIOTransports(client_); |
| 103 | while (processor_.process(io[0], io[1])) {} |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame^] | 104 | } catch (TTransportException ttx) { |
| 105 | // Assume the client died and continue silently |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 106 | } catch (TException tx) { |
| 107 | tx.printStackTrace(); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame^] | 108 | } catch (Exception x) { |
| 109 | x.printStackTrace(); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 110 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 111 | |
| 112 | if (io != null) { |
| 113 | if (io[0] != null) { |
| 114 | io[0].close(); |
| 115 | } |
| 116 | if (io[1] != null) { |
| 117 | io[1].close(); |
| 118 | } |
| 119 | } |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |