我在我的C ++项目中使用Apache Thrift GlobalOutput(error_message)
。一旦启动,服务器将监听客户端连接和处理请求。一切都很好,但偶尔服务器停止没有任何迹象。
我关注了thrift库的来源,可以看到TTransportException
或TException
被捕获时创建的void TSimpleServer::serve() {
shared_ptr<TTransport> client;
shared_ptr<TTransport> inputTransport;
shared_ptr<TTransport> outputTransport;
shared_ptr<TProtocol> inputProtocol;
shared_ptr<TProtocol> outputProtocol;
// Start the server listening
serverTransport_->listen();
// Run the preServe event
if (eventHandler_) {
eventHandler_->preServe();
}
// Fetch client from server
while (!stop_) {
try {
client = serverTransport_->accept();
inputTransport = inputTransportFactory_->getTransport(client);
outputTransport = outputTransportFactory_->getTransport(client);
inputProtocol = inputProtocolFactory_->getProtocol(inputTransport);
outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
} catch (TTransportException& ttx) {
if (inputTransport) { inputTransport->close(); }
if (outputTransport) { outputTransport->close(); }
if (client) { client->close(); }
if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) {
string errStr = string("TServerTransport died on accept: ") + ttx.what();
GlobalOutput(errStr.c_str());
}
continue;
} catch (TException& tx) {
if (inputTransport) { inputTransport->close(); }
if (outputTransport) { outputTransport->close(); }
if (client) { client->close(); }
string errStr = string("Some kind of accept exception: ") + tx.what();
GlobalOutput(errStr.c_str());
continue;
} catch (string s) {
if (inputTransport) { inputTransport->close(); }
if (outputTransport) { outputTransport->close(); }
if (client) { client->close(); }
string errStr = string("Some kind of accept exception: ") + s;
GlobalOutput(errStr.c_str());
break;
}
// Get the processor
shared_ptr<TProcessor> processor = getProcessor(inputProtocol,
outputProtocol, client);
void* connectionContext = NULL;
if (eventHandler_) {
connectionContext = eventHandler_->createContext(inputProtocol, outputProtocol);
}
try {
for (;;) {
if (eventHandler_) {
eventHandler_->processContext(connectionContext, client);
}
if (!processor->process(inputProtocol, outputProtocol,
connectionContext) ||
// Peek ahead, is the remote side closed?
!inputProtocol->getTransport()->peek()) {
break;
}
}
} catch (const TTransportException& ttx) {
string errStr = string("TSimpleServer client died: ") + ttx.what();
GlobalOutput(errStr.c_str());
} catch (const std::exception& x) {
GlobalOutput.printf("TSimpleServer exception: %s: %s",
typeid(x).name(), x.what());
} catch (...) {
GlobalOutput("TSimpleServer uncaught exception.");
}
if (eventHandler_) {
eventHandler_->deleteContext(connectionContext, inputProtocol, outputProtocol);
}
try {
inputTransport->close();
} catch (const TTransportException& ttx) {
string errStr = string("TSimpleServer input close failed: ")
+ ttx.what();
GlobalOutput(errStr.c_str());
}
try {
outputTransport->close();
} catch (const TTransportException& ttx) {
string errStr = string("TSimpleServer output close failed: ")
+ ttx.what();
GlobalOutput(errStr.c_str());
}
try {
client->close();
} catch (const TTransportException& ttx) {
string errStr = string("TSimpleServer client close failed: ")
+ ttx.what();
GlobalOutput(errStr.c_str());
}
}
if (stop_) {
try {
serverTransport_->close();
} catch (TTransportException &ttx) {
string errStr = string("TServerTransport failed on close: ") + ttx.what();
GlobalOutput(errStr.c_str());
}
stop_ = false;
}
}
。我需要理解这一点,以便在服务器死亡时创建恢复机制。
以下是我所说的源代码:
enter code hererequire_once('Classes/PHPExcel.php');
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array( ' memoryCacheSize ' => '1024MB');
ini_set("memory_limit","-1");
ini_set('max_execution_time', 13600);
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Techizer")
->setLastModifiedBy("Techizer")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setCategory("Test result file");
if($file=="employer")
{
// Rename worksheet
$file = 'employer';
$objPHPExcel->getActiveSheet()->setTitle($file);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1','Employer Name')
->setCellValue('B1','Emp ID')
->setCellValue('C1','Designation')
->setCellValue('D1','Zone')
->setCellValue('E1','Password');
$query = $this->session->userdata('employ');
$query = preg_replace('/LIMIT.*/', '', $query);
$res = $this->common_model->execute_query($query);
$row = 2;
foreach($res as $result)
{
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$row,$result->profile_name)
->setCellValue('B'.$row,$result->profile_empid)
->setCellValue('C'.$row,$result->profile_desig)
->setCellValue('D'.$row,$result->profile_zone)
->setCellValue('E'.$row,$result->profile_pass);
$row++;
}
header('Content-Disposition: attachment;filename=Employer list'.date("Y-m-d").'.xls');
}
答案 0 :(得分:0)
在TOutput.cpp深处,有一行fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg);
(source here),默认情况下,所有Thrift GlobalOutput消息最终都会出现(标准错误)。
但你可以通过以函数指针的形式为GlobalOutput提供自己的处理程序来改变它(如果由于任何原因你不能使用stderr):
void myOutputFunction(const char* x)
{
fprintf(myLogFile, "Thrift internal message: %s\n", x);
}
// Inside some init function or main
GlobalOutput.setOutputFunction(myOutputFunction);