zoukankan      html  css  js  c++  java
  • Thrift的C++服务端(线程池和非阻塞)模式

    非阻塞模式

    #include "RpcServiceHandler.h"
    
    #include <thrift/concurrency/ThreadManager.h>
    #include <thrift/concurrency/PosixThreadFactory.h>
    #include <thrift/protocol/TBinaryProtocol.h>
    #include <thrift/server/TThreadPoolServer.h>
    #include <thrift/server/TNonblockingServer.h>
    #include <thrift/server/TThreadedServer.h>
    #include <thrift/transport/TServerSocket.h>
    #include <thrift/transport/TBufferTransports.h>
    #include <thrift/TToString.h>
    
    int main(int argc, char **argv)
    {
        RpcServiceHandler *rpcServiceHanlder = new RpcServiceHandler();
    
        int port = CFG()->getInt(kCfgProcPort);
        int workerCount = CFG()->getInt(kCfgProcWCnt);
    
        boost::shared_ptr<RpcServiceHandler> handler(rpcServiceHanlder);
        boost::shared_ptr<TProcessor> processor(new RpcServiceProcessor(handler));
        boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
        boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
    
        boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount);
        boost::shared_ptr<PosixThreadFactory> threadFactory = boost::shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
        threadManager->threadFactory(threadFactory);
        threadManager->start();
        
        TNonblockingServer server(processor,    
                                  protocolFactory,    
                                  port,    
                                  threadManager);
    
        std::cout << "Starting the server..." << std::endl;
    
        server.serve();
        return 0;
    }
    

      

    线程池模式

    #include "RpcServiceHandler.h"
    
    #include <thrift/concurrency/ThreadManager.h>
    #include <thrift/concurrency/PosixThreadFactory.h>
    #include <thrift/protocol/TBinaryProtocol.h>
    #include <thrift/server/TThreadPoolServer.h>
    #include <thrift/server/TNonblockingServer.h>
    #include <thrift/server/TThreadedServer.h>
    #include <thrift/transport/TServerSocket.h>
    #include <thrift/transport/TBufferTransports.h>
    #include <thrift/TToString.h>
    
    int main(int argc, char **argv)
    {
        RpcServiceHandler *rpcServiceHanlder = new RpcServiceHandler();
    
        int port = CFG()->getInt(kCfgProcPort);
        int workerCount = CFG()->getInt(kCfgProcWCnt);
    
        boost::shared_ptr<RpcServiceHandler> handler(rpcServiceHanlder);
        boost::shared_ptr<TProcessor> processor(new RpcServiceProcessor(handler));
        boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
        boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
        boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
    
        boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount);
        boost::shared_ptr<PosixThreadFactory> threadFactory = boost::shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
        threadManager->threadFactory(threadFactory);
        threadManager->start();
        
        TThreadPoolServer server(processor,
                                 serverTransport,
                                 transportFactory,
                                 protocolFactory,
                                 threadManager);
    
        std::cout << "Starting the server..." << std::endl;
    
        server.serve();
        return 0;
    }
    

    单独Server模式

    #include "RpcServiceHandler.h"
    
    #include <thrift/protocol/TBinaryProtocol.h>
    #include <thrift/server/TSimpleServer.h>
    #include <thrift/transport/TServerSocket.h>
    #include <thrift/transport/TBufferTransports.h>
    #include <thrift/TToString.h>
    
    int main(int argc, char **argv)
    {
        RpcServiceHandler *rpcServiceHanlder = new RpcServiceHandler();
    
        int port = CFG()->getInt(kCfgProcPort);
    
        boost::shared_ptr<RpcServiceHandler> handler(rpcServiceHanlder);
        boost::shared_ptr<TProcessor> processor(new RpcServiceProcessor(handler));
        boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
        boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
        boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
    
    
        std::cout << "Starting the server..." << std::endl;
        TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
    
        server.serve();
        return 0;
    }
    

      

  • 相关阅读:
    The Quad
    将OrCAD Capture CIS的设计文件(.dsn)导入到PADS Logic VX.2.3
    OrCAD Capture CIS 16.6 将版本16.6的设计文件另存为版本16.2的设计文件
    Eclipse IDE 添加jar包到Java工程中
    PADS Logic VX.2.3 修改软件界面语言
    切换Allegro PCB Editor
    Allegro PCB Design GXL (legacy) 将brd文件另存为低版本文件
    Allegro PCB Design GXL (legacy) 设置自动保存brd文件
    Could not create an acl object: Role '16'
    windows 下apache开启FastCGI
  • 原文地址:https://www.cnblogs.com/voipman/p/7519657.html
Copyright © 2011-2022 走看看