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实现对异步委托的先进先出有序的回调.如果在回调的方法里面发生异常则应用程序 ...
随机推荐
- 通过session的id号获取对应的session
说说为什么要用session!!! 每次访问端通过普通http协议访问tomcat时,访问端包括网页或Android app等,tomcat都会自动生成一个不同的session,而且session的i ...
- TypeError: Cannot red property 'style' of null 错误解决
错误信息如下: JSP代码如下: <c:if test ="${not empty excelErrors}"> <div id="excelError ...
- leetcode — combination-sum-ii
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** *Source : https://oj ...
- web进修之—Hibernate 继承映射(5)
先看三个类的继承关系,Payment是父类,CashPayment和CreditCardPayment是Payment的子类: view plaincopy to clipboardprint p ...
- C# 锁系列目录
1.lock.Monitor lock(obj){} 编译之后是如下代码 Monitor.Enter(obj); try { // } finally { Monitor.Exit(obj); } 2 ...
- 微信分享JS-SDK
微信JS-SDK,提供给开发者的基于微信内的网页开发工具包 使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统的能力,同时可以直接使用微信分享.扫一扫.卡券.支付等微 ...
- git在工作中的用法总结-使用篇
上一篇介绍了git的环境安装配置,本篇对git在工作中常用的用法进行总结,已满足大部分的日常工作需求,对于其他的一些git命令用法在今后使用到时我也会更新上来,文中如有错误,欢迎大家指出来,谢谢~ 一 ...
- python对象属性管理(2):property管理属性
使用Property管理属性 python提供了一种友好的getter.setter.deleter类方法的属性管理工具:property. property()是一个内置函数,它返回一个Proper ...
- maven web工程缺少 src/main/java 和 src/test/java 资源文件夹的方法
右键打开:build path -> configure build path... 在弹出的界面,选择: 编辑后: 点击finish,即可完成
- camera测试之MTF
1.MTF介绍 MTF(Modulation Transfer Function)模量传递函数.MTF是camera成像对比度和分辨率的综合表现.从另一个角度来看,camera成像过程可以简单看成下图 ...