The external Apache Thrift event loop

In C ++, how can I get the Apache Thrift server to handle external events? I would like to write my own event loop and manually make the unfinished processes of the Thrift process without blocking.

I would like to do something like this:

//...
TSimpleServer server(processor,
                       serverTransport,
                       transportFactory,
                       protocolFactory);

boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.async_wait(&onTimer);

while (!finished)
{
    server.poll();
    io.poll();
}

In the above loop, I understand that there will be many wasteful lively expectations if there are no pending events. Any suggestions on how to avoid this?

+4
source share
1 answer

As far as I can tell, there is no built-in way to get Apache Thrift to interact with Boost.Asio or any other loop of external events.

/ Thrift Asio. Apache Thrift JIRA ( ), :

THRIFT-579: ASIO / ++

, -, / Asio.

, , TAsioServer boost::asio::io_service.

int main(int argc, char **argv) {
  boost::asio::io_service io_service;

  boost::shared_ptr<protocol::TProtocolFactory> protocolFactory(new protocol::TBinaryProtocolFactory());
  boost::shared_ptr<CalculatorAsyncHandler> handler(new CalculatorAsyncHandler(io_service));
  boost::shared_ptr<TProcessor> processor(new CalculatorAsyncProcessor(handler));

  boost::shared_ptr<apache::thrift::async::TAsioServer> server(
                                   new apache::thrift::async::TAsioServer(
                                                      io_service,
                                                      9090,
                                                      protocolFactory,
                                                      protocolFactory,
                                                      processor));

  server->start(); // Nonblocking
  io_service.run(); // Blocking

  return 0;
}

TAsioServer, , , boost::asio::serial_port boost::asio::deadline_timer boost::asio::io_service.

+2

All Articles