非阻塞模式

#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;
}

  

Thrift的C++服务端(线程池和非阻塞)模式的更多相关文章

  1. 将服务端select设置为非阻塞,处理更多业务

    服务端代码: #include<WinSock2.h> #include<Windows.h> #include<vector> #include<stdio ...

  2. Python中的Tcp协议应用之TCP服务端-线程版

    利用线程实现,一个服务端同时服务多个客户端的需求. TCP服务端-线程版代码实现: import socket import threading def handle_client_socket(ne ...

  3. Java线程池(Callable+Future模式)

    转: Java线程池(Callable+Future模式) Java线程池(Callable+Future模式) Java通过Executors提供四种线程池 1)newCachedThreadPoo ...

  4. springBoot服务整合线程池ThreadPoolTaskExecutor与@Async详解使用

    ThreadPoolExecutor:=======这个是java自己实现的线程池执行类,基本上创建线程池都是通过这个类进行的创建.ThreadPoolTaskExecutor:========这个是 ...

  5. 设计模式:基于线程池的并发Visitor模式

    1.前言 第二篇设计模式的文章我们谈谈Visitor模式. 当然,不是简单的列个的demo,我们以电商网站中的购物车功能为背景,使用线程池实现并发的Visitor模式,并聊聊其中的几个关键点. 一,基 ...

  6. spring线程池ThreadPoolTaskExecutor与阻塞队列BlockingQueue

    一: ThreadPoolTaskExecutor是一个spring的线程池技术,查看代码可以看到这样一个字段: private ThreadPoolExecutor threadPoolExecut ...

  7. java多线程:线程池原理、阻塞队列

    一.线程池定义和使用 jdk 1.5 之后就引入了线程池. 1.1 定义 从上面的空间切换看得出来,线程是稀缺资源,它的创建与销毁是一个相对偏重且耗资源的操作,而Java线程依赖于内核线程,创建线程需 ...

  8. 线程池ThreadPoolExecutor与阻塞队列BlockingQueue应用

    1.线程池介绍 JDK5.0以上: java.util.concurrent.ThreadPoolExecutor  构造函数签名: public ThreadPoolExecutor( int co ...

  9. ThreadPool.QueueUserWorkItem引发的血案,线程池异步非正确姿势导致程序闪退的问题

    ThreadPool是.net System.Threading命名空间下的线程池对象.使用QueueUserWorkItem实现对异步委托的先进先出有序的回调.如果在回调的方法里面发生异常则应用程序 ...

随机推荐

  1. MyEclipse中JavaMail冲突问题

    MyEclipse中的JavaEE5中的mail包中只有接口,而没有实现,所以不能使用 会抛出:java.lang.NoClassDefFoundError: com/sun/mail/util/BE ...

  2. sql server 索引阐述系列三 表的堆组织

    一.   概述 这一节来详细介绍堆组织,通过讲解堆的结构,堆与非聚集索引的关系,堆的应用场景,堆与聚集索引的存储空间占用,堆的页拆分现象,最后堆的使用建议 ,这几个维度来描述堆组织.在sqlserve ...

  3. C++版 - 剑指offer 面试题22:栈的压入、弹出序列 题解

    剑指offer 面试题22:栈的压入.弹出序列 提交网址: http://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106?tpId ...

  4. kafka和storm集群的环境安装

    前言 storm和kafka集群安装是没有必然联系的,我将这两个写在一起,是因为他们都是由zookeeper进行管理的,也都依赖于JDK的环境,为了不重复再写一遍配置,所以我将这两个写在一起.若只需一 ...

  5. Core2.0知识整理

    概述 Commond-Line ASP.NET结构文件 Startup 配置文件 中间件和依赖注入 依赖注入原理 框架自带的依赖注入(IServiceCollection) 依赖注入生命周期 依赖注入 ...

  6. JavaScript经典片段

    typeof jQuery != "undefined" || importjQuery(); 判断jQuery对象是否存在,如果不存在就调用importjQuery()方法加载j ...

  7. linux 命令 — cut

    cut 以列的方式格式化输出 依赖定界符 cut -f field_list filename 以默认定界符(tab,制表符)分割文件的列,输出指定的列field_list,field_list由列号 ...

  8. (亲测成功)在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机(ubuntu server版本)

    在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机 前提:服务器端安装桌面版的centos系统 CentOS Linux release 7.5.1804 (Core) ...

  9. python抓取电影<海王>影评词云生成

    海王是前段时间大热的影片,个人对这种动漫题材的电影并不是很感兴趣.然鹅,最近这部电影实在太热了,正好最近看自然语言处理的时候,无意间发现了word cloud这个生成词云的库,还蛮好玩的,那就抓抓这部 ...

  10. Tomcat 8005/8009/8080/8443端口的作用

    --关闭tomcat进程所用.当执行shutdown.sh关闭tomcat时就是连接8005端口执行“SHUTDOWN”命令--由此,我们直接telnet8005端口执行“SHUTDOWN”(要大写, ...