Mark Slee | 7eb0d63 | 2007-03-01 00:00:27 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 7 | package com.facebook.thrift.server; |
| 8 | |
| 9 | import com.facebook.thrift.TException; |
| 10 | import com.facebook.thrift.TProcessor; |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 11 | import com.facebook.thrift.TProcessorFactory; |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 12 | import com.facebook.thrift.protocol.TProtocol; |
| 13 | import com.facebook.thrift.protocol.TProtocolFactory; |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 14 | import com.facebook.thrift.protocol.TBinaryProtocol; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 15 | import com.facebook.thrift.transport.TServerTransport; |
| 16 | import com.facebook.thrift.transport.TTransport; |
| 17 | import com.facebook.thrift.transport.TTransportException; |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 18 | import com.facebook.thrift.transport.TTransportFactory; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 19 | |
| 20 | import java.util.concurrent.ExecutorService; |
| 21 | import java.util.concurrent.LinkedBlockingQueue; |
| 22 | import java.util.concurrent.ThreadPoolExecutor; |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | |
| 25 | |
| 26 | /** |
| 27 | * Server which uses Java's built in ThreadPool management to spawn off |
| 28 | * a worker pool that |
| 29 | * |
| 30 | * @author Mark Slee <mcslee@facebook.com> |
| 31 | */ |
| 32 | public class TThreadPoolServer extends TServer { |
| 33 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 34 | // Executor service for handling client connections |
| 35 | private ExecutorService executorService_; |
| 36 | |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame] | 37 | // Flag for stopping the server |
| 38 | private volatile boolean stopped_; |
| 39 | |
| 40 | // Server options |
| 41 | private Options options_; |
| 42 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 43 | // Customizable server options |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 44 | public static class Options { |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 45 | public int minWorkerThreads = 5; |
| 46 | public int maxWorkerThreads = Integer.MAX_VALUE; |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame] | 47 | public int stopTimeoutVal = 60; |
| 48 | public TimeUnit stopTimeoutUnit = TimeUnit.SECONDS; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | public TThreadPoolServer(TProcessor processor, |
| 52 | TServerTransport serverTransport) { |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 53 | this(processor, serverTransport, |
| 54 | new TTransportFactory(), new TTransportFactory(), |
| 55 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
| 56 | new Options()); |
| 57 | } |
| 58 | |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 59 | public TThreadPoolServer(TProcessorFactory processorFactory, |
| 60 | TServerTransport serverTransport) { |
| 61 | this(processorFactory, serverTransport, |
| 62 | new TTransportFactory(), new TTransportFactory(), |
| 63 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
| 64 | new Options()); |
| 65 | } |
| 66 | |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 67 | public TThreadPoolServer(TProcessor processor, |
| 68 | TServerTransport serverTransport, |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 69 | TProtocolFactory protocolFactory) { |
| 70 | this(processor, serverTransport, |
| 71 | new TTransportFactory(), new TTransportFactory(), |
| 72 | protocolFactory, protocolFactory, |
| 73 | new Options()); |
| 74 | } |
| 75 | |
| 76 | public TThreadPoolServer(TProcessor processor, |
| 77 | TServerTransport serverTransport, |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 78 | TTransportFactory transportFactory, |
| 79 | TProtocolFactory protocolFactory) { |
| 80 | this(processor, serverTransport, |
| 81 | transportFactory, transportFactory, |
| 82 | protocolFactory, protocolFactory, |
| 83 | new Options()); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 84 | } |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 85 | |
| 86 | public TThreadPoolServer(TProcessorFactory processorFactory, |
| 87 | TServerTransport serverTransport, |
| 88 | TTransportFactory transportFactory, |
| 89 | TProtocolFactory protocolFactory) { |
| 90 | this(processorFactory, serverTransport, |
| 91 | transportFactory, transportFactory, |
| 92 | protocolFactory, protocolFactory, |
| 93 | new Options()); |
| 94 | } |
| 95 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 96 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 97 | public TThreadPoolServer(TProcessor processor, |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 98 | TServerTransport serverTransport, |
| 99 | TTransportFactory inputTransportFactory, |
| 100 | TTransportFactory outputTransportFactory, |
| 101 | TProtocolFactory inputProtocolFactory, |
| 102 | TProtocolFactory outputProtocolFactory, |
| 103 | Options options) { |
| 104 | this(new TProcessorFactory(processor), serverTransport, |
| 105 | inputTransportFactory, outputTransportFactory, |
| 106 | inputProtocolFactory, outputProtocolFactory, |
| 107 | options); |
| 108 | } |
| 109 | |
| 110 | public TThreadPoolServer(TProcessorFactory processorFactory, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 111 | TServerTransport serverTransport, |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 112 | TTransportFactory inputTransportFactory, |
| 113 | TTransportFactory outputTransportFactory, |
| 114 | TProtocolFactory inputProtocolFactory, |
| 115 | TProtocolFactory outputProtocolFactory, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 116 | Options options) { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 117 | super(processorFactory, serverTransport, |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 118 | inputTransportFactory, outputTransportFactory, |
| 119 | inputProtocolFactory, outputProtocolFactory); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 120 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 121 | executorService_ = null; |
| 122 | |
| 123 | LinkedBlockingQueue<Runnable> executorQueue = |
| 124 | new LinkedBlockingQueue<Runnable>(); |
| 125 | |
| 126 | executorService_ = new ThreadPoolExecutor(options.minWorkerThreads, |
| 127 | options.maxWorkerThreads, |
| 128 | 60, |
| 129 | TimeUnit.SECONDS, |
| 130 | executorQueue); |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame] | 131 | |
| 132 | options_ = options; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 136 | public void serve() { |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 137 | try { |
| 138 | serverTransport_.listen(); |
| 139 | } catch (TTransportException ttx) { |
| 140 | ttx.printStackTrace(); |
| 141 | return; |
| 142 | } |
| 143 | |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame] | 144 | stopped_ = false; |
| 145 | while (!stopped_) { |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 146 | int failureCount = 0; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 147 | try { |
| 148 | TTransport client = serverTransport_.accept(); |
| 149 | WorkerProcess wp = new WorkerProcess(client); |
| 150 | executorService_.execute(wp); |
| 151 | } catch (TTransportException ttx) { |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame] | 152 | if (!stopped_) { |
| 153 | ++failureCount; |
| 154 | ttx.printStackTrace(); |
| 155 | } |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 156 | } |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 157 | } |
Mark Slee | 0502e61 | 2007-11-03 05:30:32 +0000 | [diff] [blame] | 158 | |
| 159 | executorService_.shutdown(); |
| 160 | try { |
| 161 | executorService_.awaitTermination(options_.stopTimeoutVal, |
| 162 | options_.stopTimeoutUnit); |
| 163 | } catch (InterruptedException ix) { |
| 164 | // Ignore and more on |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | public void stop() { |
| 169 | stopped_ = true; |
| 170 | serverTransport_.interrupt(); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | private class WorkerProcess implements Runnable { |
| 174 | |
| 175 | /** |
| 176 | * Client that this services. |
| 177 | */ |
| 178 | private TTransport client_; |
| 179 | |
| 180 | /** |
| 181 | * Default constructor. |
| 182 | * |
| 183 | * @param client Transport to process |
| 184 | */ |
| 185 | private WorkerProcess(TTransport client) { |
| 186 | client_ = client; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Loops on processing a client forever |
| 191 | */ |
| 192 | public void run() { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 193 | TProcessor processor = null; |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 194 | TTransport inputTransport = null; |
| 195 | TTransport outputTransport = null; |
| 196 | TProtocol inputProtocol = null; |
| 197 | TProtocol outputProtocol = null; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 198 | try { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 199 | processor = processorFactory_.getProcessor(client_); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 200 | inputTransport = inputTransportFactory_.getTransport(client_); |
| 201 | outputTransport = outputTransportFactory_.getTransport(client_); |
| 202 | inputProtocol = inputProtocolFactory_.getProtocol(inputTransport); |
| 203 | outputProtocol = outputProtocolFactory_.getProtocol(outputTransport); |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 204 | while (processor.process(inputProtocol, outputProtocol)) {} |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 205 | } catch (TTransportException ttx) { |
| 206 | // Assume the client died and continue silently |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 207 | } catch (TException tx) { |
| 208 | tx.printStackTrace(); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 209 | } catch (Exception x) { |
| 210 | x.printStackTrace(); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 211 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 212 | |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 213 | if (inputTransport != null) { |
| 214 | inputTransport.close(); |
| 215 | } |
| 216 | |
| 217 | if (outputTransport != null) { |
| 218 | outputTransport.close(); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 219 | } |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | } |