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;
}
Thrift的C++服务端(线程池和非阻塞)模式的更多相关文章
- 将服务端select设置为非阻塞,处理更多业务
服务端代码: #include<WinSock2.h> #include<Windows.h> #include<vector> #include<stdio ...
- Python中的Tcp协议应用之TCP服务端-线程版
利用线程实现,一个服务端同时服务多个客户端的需求. TCP服务端-线程版代码实现: import socket import threading def handle_client_socket(ne ...
- Java线程池(Callable+Future模式)
转: Java线程池(Callable+Future模式) Java线程池(Callable+Future模式) Java通过Executors提供四种线程池 1)newCachedThreadPoo ...
- springBoot服务整合线程池ThreadPoolTaskExecutor与@Async详解使用
ThreadPoolExecutor:=======这个是java自己实现的线程池执行类,基本上创建线程池都是通过这个类进行的创建.ThreadPoolTaskExecutor:========这个是 ...
- 设计模式:基于线程池的并发Visitor模式
1.前言 第二篇设计模式的文章我们谈谈Visitor模式. 当然,不是简单的列个的demo,我们以电商网站中的购物车功能为背景,使用线程池实现并发的Visitor模式,并聊聊其中的几个关键点. 一,基 ...
- spring线程池ThreadPoolTaskExecutor与阻塞队列BlockingQueue
一: ThreadPoolTaskExecutor是一个spring的线程池技术,查看代码可以看到这样一个字段: private ThreadPoolExecutor threadPoolExecut ...
- java多线程:线程池原理、阻塞队列
一.线程池定义和使用 jdk 1.5 之后就引入了线程池. 1.1 定义 从上面的空间切换看得出来,线程是稀缺资源,它的创建与销毁是一个相对偏重且耗资源的操作,而Java线程依赖于内核线程,创建线程需 ...
- 线程池ThreadPoolExecutor与阻塞队列BlockingQueue应用
1.线程池介绍 JDK5.0以上: java.util.concurrent.ThreadPoolExecutor 构造函数签名: public ThreadPoolExecutor( int co ...
- ThreadPool.QueueUserWorkItem引发的血案,线程池异步非正确姿势导致程序闪退的问题
ThreadPool是.net System.Threading命名空间下的线程池对象.使用QueueUserWorkItem实现对异步委托的先进先出有序的回调.如果在回调的方法里面发生异常则应用程序 ...
随机推荐
- 剑指offer【07】- 斐波那契数列(java)
题目:斐波那契数列 考点:递归和循环 题目描述:大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0),n<=39. 法一:递归法,不过递归比较慢, ...
- vue 项目实战 (vue全家桶之--- vuex)
老规矩先安装 npm install vuex --save 在看下面内容之前 你应该大概的看了一边vuex官方的文档对vuex有个大概对了解 首先 vuex 是什么? vuex 是属于vue中的什么 ...
- ionic 热更新 cordova-hot-code-push
cordova-hot-code-push ,Cordova热代码推送插件提供了在应用程序中执行基于Web的内容的自动更新的功能.使用此插件可以更新存储在项目的www文件夹中的所有内容. cordov ...
- 腾讯qq发邮件
<本人新手> 首先要添加引用
- 【原创】Python第二章——字符串
字符串是一个字符序列,(提醒:序列是Python的一个重要的关键词),其中存放UNICODE字符.Python中的字符串是不可变的(immutable),即对字符串执行操作时,总是产生一个新的字符串而 ...
- 使用awk和sed获取文件奇偶数行的方法总结
测试文件test.file [root@localhost ~]# cat test.file 111111111111111 222222222222222 333333333333333 4444 ...
- 如何像Python高手(Pythonista)一样编程
最近在网上看到一篇介绍Pythonic编程的文章:Code Like a Pythonista: Idiomatic Python,其实作者在2006的PyCon会议后就写了这篇文章,写这篇文章的主要 ...
- lsyncd —— 多机器实时同步文件神器
lsyncd 是一个支持实时.双向.多机器的多模式文件同步工具. 使用 Lua 语言封装了 inotify 和 rsync 工具,采用了 Linux 内核(2.6.13 及以后)里的 inotify ...
- IPv6笔记
单播地址包含的几种类型:1.AGUA地址:2000::到3FFF:FFFF:FFFF:FFFF:FFFF..... 一个接口可以拥有若干个AGUA地址2.Link-Local 地址:FE80::/10 ...
- 关于Vue数组操作
Vue的数组操作的实现代码大致如下: const aryMethods = ['push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse ...