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 | |
| 37 | // Customizable server options |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 38 | public static class Options { |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 39 | public int minWorkerThreads = 5; |
| 40 | public int maxWorkerThreads = Integer.MAX_VALUE; |
| 41 | } |
| 42 | |
| 43 | public TThreadPoolServer(TProcessor processor, |
| 44 | TServerTransport serverTransport) { |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 45 | this(processor, serverTransport, |
| 46 | new TTransportFactory(), new TTransportFactory(), |
| 47 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
| 48 | new Options()); |
| 49 | } |
| 50 | |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 51 | public TThreadPoolServer(TProcessorFactory processorFactory, |
| 52 | TServerTransport serverTransport) { |
| 53 | this(processorFactory, serverTransport, |
| 54 | new TTransportFactory(), new TTransportFactory(), |
| 55 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
| 56 | new Options()); |
| 57 | } |
| 58 | |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 59 | public TThreadPoolServer(TProcessor processor, |
| 60 | TServerTransport serverTransport, |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 61 | TProtocolFactory protocolFactory) { |
| 62 | this(processor, serverTransport, |
| 63 | new TTransportFactory(), new TTransportFactory(), |
| 64 | protocolFactory, protocolFactory, |
| 65 | new Options()); |
| 66 | } |
| 67 | |
| 68 | public TThreadPoolServer(TProcessor processor, |
| 69 | TServerTransport serverTransport, |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 70 | TTransportFactory transportFactory, |
| 71 | TProtocolFactory protocolFactory) { |
| 72 | this(processor, serverTransport, |
| 73 | transportFactory, transportFactory, |
| 74 | protocolFactory, protocolFactory, |
| 75 | new Options()); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 76 | } |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 77 | |
| 78 | public TThreadPoolServer(TProcessorFactory processorFactory, |
| 79 | TServerTransport serverTransport, |
| 80 | TTransportFactory transportFactory, |
| 81 | TProtocolFactory protocolFactory) { |
| 82 | this(processorFactory, serverTransport, |
| 83 | transportFactory, transportFactory, |
| 84 | protocolFactory, protocolFactory, |
| 85 | new Options()); |
| 86 | } |
| 87 | |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 88 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 89 | public TThreadPoolServer(TProcessor processor, |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 90 | TServerTransport serverTransport, |
| 91 | TTransportFactory inputTransportFactory, |
| 92 | TTransportFactory outputTransportFactory, |
| 93 | TProtocolFactory inputProtocolFactory, |
| 94 | TProtocolFactory outputProtocolFactory, |
| 95 | Options options) { |
| 96 | this(new TProcessorFactory(processor), serverTransport, |
| 97 | inputTransportFactory, outputTransportFactory, |
| 98 | inputProtocolFactory, outputProtocolFactory, |
| 99 | options); |
| 100 | } |
| 101 | |
| 102 | public TThreadPoolServer(TProcessorFactory processorFactory, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 103 | TServerTransport serverTransport, |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 104 | TTransportFactory inputTransportFactory, |
| 105 | TTransportFactory outputTransportFactory, |
| 106 | TProtocolFactory inputProtocolFactory, |
| 107 | TProtocolFactory outputProtocolFactory, |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 108 | Options options) { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 109 | super(processorFactory, serverTransport, |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 110 | inputTransportFactory, outputTransportFactory, |
| 111 | inputProtocolFactory, outputProtocolFactory); |
Mark Slee | 456b7a8 | 2006-10-25 20:53:37 +0000 | [diff] [blame] | 112 | |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 113 | executorService_ = null; |
| 114 | |
| 115 | LinkedBlockingQueue<Runnable> executorQueue = |
| 116 | new LinkedBlockingQueue<Runnable>(); |
| 117 | |
| 118 | executorService_ = new ThreadPoolExecutor(options.minWorkerThreads, |
| 119 | options.maxWorkerThreads, |
| 120 | 60, |
| 121 | TimeUnit.SECONDS, |
| 122 | executorQueue); |
| 123 | } |
| 124 | |
| 125 | |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 126 | public void serve() { |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 127 | try { |
| 128 | serverTransport_.listen(); |
| 129 | } catch (TTransportException ttx) { |
| 130 | ttx.printStackTrace(); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | while (true) { |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 135 | int failureCount = 0; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 136 | try { |
| 137 | TTransport client = serverTransport_.accept(); |
| 138 | WorkerProcess wp = new WorkerProcess(client); |
| 139 | executorService_.execute(wp); |
| 140 | } catch (TTransportException ttx) { |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 141 | ++failureCount; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 142 | ttx.printStackTrace(); |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 143 | } |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | private class WorkerProcess implements Runnable { |
| 148 | |
| 149 | /** |
| 150 | * Client that this services. |
| 151 | */ |
| 152 | private TTransport client_; |
| 153 | |
| 154 | /** |
| 155 | * Default constructor. |
| 156 | * |
| 157 | * @param client Transport to process |
| 158 | */ |
| 159 | private WorkerProcess(TTransport client) { |
| 160 | client_ = client; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Loops on processing a client forever |
| 165 | */ |
| 166 | public void run() { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 167 | TProcessor processor = null; |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 168 | TTransport inputTransport = null; |
| 169 | TTransport outputTransport = null; |
| 170 | TProtocol inputProtocol = null; |
| 171 | TProtocol outputProtocol = null; |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 172 | try { |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 173 | processor = processorFactory_.getProcessor(client_); |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 174 | inputTransport = inputTransportFactory_.getTransport(client_); |
| 175 | outputTransport = outputTransportFactory_.getTransport(client_); |
| 176 | inputProtocol = inputProtocolFactory_.getProtocol(inputTransport); |
| 177 | outputProtocol = outputProtocolFactory_.getProtocol(outputTransport); |
Mark Slee | 448849d | 2007-05-31 01:30:22 +0000 | [diff] [blame] | 178 | while (processor.process(inputProtocol, outputProtocol)) {} |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 179 | } catch (TTransportException ttx) { |
| 180 | // Assume the client died and continue silently |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 181 | } catch (TException tx) { |
| 182 | tx.printStackTrace(); |
Mark Slee | ade2c83 | 2006-09-08 03:41:50 +0000 | [diff] [blame] | 183 | } catch (Exception x) { |
| 184 | x.printStackTrace(); |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 185 | } |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 186 | |
Aditya Agarwal | 5a42958 | 2007-02-06 02:51:15 +0000 | [diff] [blame] | 187 | if (inputTransport != null) { |
| 188 | inputTransport.close(); |
| 189 | } |
| 190 | |
| 191 | if (outputTransport != null) { |
| 192 | outputTransport.close(); |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 193 | } |
Mark Slee | ffcddd6 | 2006-09-06 20:37:03 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |