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; |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 5 | import com.facebook.thrift.protocol.TProtocol; |
| 6 | import com.facebook.thrift.protocol.TProtocolFactory; |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 7 | import com.facebook.thrift.protocol.TBinaryProtocol; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 8 | import com.facebook.thrift.transport.TServerTransport; |
| 9 | import com.facebook.thrift.transport.TTransport; |
| 10 | import com.facebook.thrift.transport.TTransportException; |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 11 | import com.facebook.thrift.transport.TTransportFactory; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 12 | |
| 13 | import java.util.concurrent.ExecutorService; |
| 14 | import java.util.concurrent.LinkedBlockingQueue; |
| 15 | import java.util.concurrent.ThreadPoolExecutor; |
| 16 | import java.util.concurrent.TimeUnit; |
| 17 | |
| 18 | |
| 19 | /** |
| 20 | * Server which uses Java's built in ThreadPool management to spawn off |
| 21 | * a worker pool that |
| 22 | * |
| 23 | * @author Mark Slee <mcslee@facebook.com> |
| 24 | */ |
| 25 | public class TThreadPoolServer extends TServer { |
| 26 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 27 | // Executor service for handling client connections |
| 28 | private ExecutorService executorService_; |
| 29 | |
| 30 | // Customizable server options |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 31 | public static class Options { |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 32 | public int minWorkerThreads = 5; |
| 33 | public int maxWorkerThreads = Integer.MAX_VALUE; |
| 34 | } |
| 35 | |
| 36 | public TThreadPoolServer(TProcessor processor, |
| 37 | TServerTransport serverTransport) { |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 38 | this(processor, serverTransport, |
| 39 | new TTransportFactory(), new TTransportFactory(), |
| 40 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
| 41 | new Options()); |
| 42 | } |
| 43 | |
| 44 | public TThreadPoolServer(TProcessor processor, |
| 45 | TServerTransport serverTransport, |
| 46 | TTransportFactory transportFactory, |
| 47 | TProtocolFactory protocolFactory) { |
| 48 | this(processor, serverTransport, |
| 49 | transportFactory, transportFactory, |
| 50 | protocolFactory, protocolFactory, |
| 51 | new Options()); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 52 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 53 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 54 | public TThreadPoolServer(TProcessor processor, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 55 | TServerTransport serverTransport, |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 56 | TTransportFactory inputTransportFactory, |
| 57 | TTransportFactory outputTransportFactory, |
| 58 | TProtocolFactory inputProtocolFactory, |
| 59 | TProtocolFactory outputProtocolFactory, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 60 | Options options) { |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 61 | super(processor, serverTransport, |
| 62 | inputTransportFactory, outputTransportFactory, |
| 63 | inputProtocolFactory, outputProtocolFactory); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 64 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 65 | executorService_ = null; |
| 66 | |
| 67 | LinkedBlockingQueue<Runnable> executorQueue = |
| 68 | new LinkedBlockingQueue<Runnable>(); |
| 69 | |
| 70 | executorService_ = new ThreadPoolExecutor(options.minWorkerThreads, |
| 71 | options.maxWorkerThreads, |
| 72 | 60, |
| 73 | TimeUnit.SECONDS, |
| 74 | executorQueue); |
| 75 | } |
| 76 | |
| 77 | |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 78 | public void serve() { |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 79 | try { |
| 80 | serverTransport_.listen(); |
| 81 | } catch (TTransportException ttx) { |
| 82 | ttx.printStackTrace(); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | while (true) { |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 87 | int failureCount = 0; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 88 | try { |
| 89 | TTransport client = serverTransport_.accept(); |
| 90 | WorkerProcess wp = new WorkerProcess(client); |
| 91 | executorService_.execute(wp); |
| 92 | } catch (TTransportException ttx) { |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 93 | ++failureCount; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 94 | ttx.printStackTrace(); |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 95 | } |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | private class WorkerProcess implements Runnable { |
| 100 | |
| 101 | /** |
| 102 | * Client that this services. |
| 103 | */ |
| 104 | private TTransport client_; |
| 105 | |
| 106 | /** |
| 107 | * Default constructor. |
| 108 | * |
| 109 | * @param client Transport to process |
| 110 | */ |
| 111 | private WorkerProcess(TTransport client) { |
| 112 | client_ = client; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Loops on processing a client forever |
| 117 | */ |
| 118 | public void run() { |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 119 | TTransport inputTransport = null; |
| 120 | TTransport outputTransport = null; |
| 121 | TProtocol inputProtocol = null; |
| 122 | TProtocol outputProtocol = null; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 123 | try { |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 124 | inputTransport = inputTransportFactory_.getTransport(client_); |
| 125 | outputTransport = outputTransportFactory_.getTransport(client_); |
| 126 | inputProtocol = inputProtocolFactory_.getProtocol(inputTransport); |
| 127 | outputProtocol = outputProtocolFactory_.getProtocol(outputTransport); |
| 128 | while (processor_.process(inputProtocol, outputProtocol)) {} |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 129 | } catch (TTransportException ttx) { |
| 130 | // Assume the client died and continue silently |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 131 | } catch (TException tx) { |
| 132 | tx.printStackTrace(); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 133 | } catch (Exception x) { |
| 134 | x.printStackTrace(); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 135 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 136 | |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 137 | if (inputTransport != null) { |
| 138 | inputTransport.close(); |
| 139 | } |
| 140 | |
| 141 | if (outputTransport != null) { |
| 142 | outputTransport.close(); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 143 | } |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |