* block until all the workers have finished their work. At that point
* the ThreadManager will transition into the STOPPED state.
*/
+ virtual void join() = 0;
+
enum STATE {
UNINITIALIZED,
STARTING,
virtual void serve() = 0;
+ virtual void stop() {}
+
// Allows running the server as a Runnable thread
virtual void run() {
serve();
}
// Fetch client from server
- while (true) {
+ while (!stop_) {
try {
client = serverTransport_->accept();
inputTransport = inputTransportFactory_->getTransport(client);
}
}
- // TODO(mcslee): Could this be a timeout case? Or always the real thing?
+ if (stop_) {
+ try {
+ serverTransport_->close();
+ } catch (TTransportException &ttx) {
+ cerr << "TServerTransport failed on close: " << ttx.what() << endl;
+ }
+ stop_ = false;
+ }
}
}}} // facebook::thrift::server
shared_ptr<TServerTransport> serverTransport,
shared_ptr<TTransportFactory> transportFactory,
shared_ptr<TProtocolFactory> protocolFactory) :
- TServer(processor, serverTransport, transportFactory, protocolFactory) {}
+ TServer(processor, serverTransport, transportFactory, protocolFactory),
+ stop_(false) {}
TSimpleServer(shared_ptr<TProcessor> processor,
shared_ptr<TServerTransport> serverTransport,
shared_ptr<TProtocolFactory> outputProtocolFactory):
TServer(processor, serverTransport,
inputTransportFactory, outputTransportFactory,
- inputProtocolFactory, outputProtocolFactory) {}
+ inputProtocolFactory, outputProtocolFactory),
+ stop_(false) {}
~TSimpleServer() {}
void serve();
+ void stop() {
+ stop_ = true;
+ }
+
+ protected:
+ bool stop_;
+
};
}}} // facebook::thrift::server
shared_ptr<TProtocolFactory> protocolFactory,
shared_ptr<ThreadManager> threadManager) :
TServer(processor, serverTransport, transportFactory, protocolFactory),
- threadManager_(threadManager) {}
+ threadManager_(threadManager),
+ stop_(false) {}
TThreadPoolServer::TThreadPoolServer(shared_ptr<TProcessor> processor,
shared_ptr<TServerTransport> serverTransport,
shared_ptr<ThreadManager> threadManager) :
TServer(processor, serverTransport, inputTransportFactory, outputTransportFactory,
inputProtocolFactory, outputProtocolFactory),
- threadManager_(threadManager) {}
+ threadManager_(threadManager),
+ stop_(false) {}
TThreadPoolServer::~TThreadPoolServer() {}
void TThreadPoolServer::serve() {
-
shared_ptr<TTransport> client;
shared_ptr<TTransport> inputTransport;
shared_ptr<TTransport> outputTransport;
return;
}
- while (true) {
+ while (!stop_) {
try {
// Fetch client from server
client = serverTransport_->accept();
break;
}
}
+
+ // If stopped manually, join the existing threads
+ if (stop_) {
+ try {
+ serverTransport_->close();
+ threadManager_->join();
+ } catch (TException &tx) {
+ cerr << "TThreadPoolServer: Exception shutting down: " << tx.what() << endl;
+ }
+ }
+ stop_ = false;
+
}
}}} // facebook::thrift::server
virtual ~TThreadPoolServer();
virtual void serve();
+
+ virtual void stop() { stop_ = true; }
protected:
shared_ptr<ThreadManager> threadManager_;
+
+ volatile bool stop_;
};