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