当前位置: 首页 > news >正文

java Thrift TThreadPoolServer 多个processor 的实现

当我们使用Thrift 通信的时候,服务端有时候需要注册多个类,去实现通信,这时候我们就不能再使用单一Processor的方式,就要使用多个Processor,那么如何去实现呢?

多个Process

服务端

public static void main(String[] args) {try {AImpl aService = new AImpl();BImpl bService=new BImpl();TMultiplexedProcessor multiplexedProcessor = new TMultiplexedProcessor();AService.Processor<AImpl> aProcessor = new AService.Processor<>(aService);multiplexedProcessor.registerProcessor("aService", aProcessor);BService.Processor<BImpl> bProcessor = new BService.Processor<>(bService);multiplexedProcessor.registerProcessor("bService", bProcessor);TServerSocket serverTransport = new TServerSocket(80000);TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport);serverArgs.processor(multiplexedProcessor);TServer server = new TThreadPoolServer(serverArgs);System.out.println("Starting the multi-processor server...");server.serve();} catch (Exception e) {e.printStackTrace();System.out.println(e.getMessage());}}

客户端

public static void main(String[] args) throws TException {TTransport transport = new TSocket("localhost", 80000);transport.open();// AServiceTMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(new TBinaryProtocol(transport), "aService");AService.Client aClient = new AService.Client(multiplexedProtocol);aClient.method();System.out.println("Calling AService method...");// BServicemultiplexedProtocol = new TMultiplexedProtocol(new TBinaryProtocol(transport), "bService");BService.Client bClient = new BService.Client(multiplexedProtocol);BClient.method();System.out.println("Calling SystemLogService method...");transport.close();}

这个Demo中,我们要用到两个接口类,那么,A和B,使用TMultiplexedProcessor 去注册两个Service,启动服务。

单个Process

服务端

            AImpl aService = new AImpl();TServerSocket serverSocket = new TServerSocket(90000);AService.Processor<AImpl> aProcessor= new AService.Processor<>(aService);TThreadPoolServer.Args serverArg = new TThreadPoolServer.Args(serverSocket);serverArg.processor(aProcessor);TThreadPoolServer server = new TThreadPoolServer(serverArg);server.serve();

客户端

 TTransport transport = new TSocket("localhost", 90000);transport.open();TBinaryProtocol protocol = new TBinaryProtocol(transport);AService.Client aClient = new AService.Client(protocol);aclient.method();

附单个process的方式。

http://www.lryc.cn/news/371288.html

相关文章:

  • 失眠焦虑的解脱之道:找回内心的平静
  • OLED柔性屏的显示效果如何
  • 百货商城优选 伊利牛奶推出全国首款减甲烷环保学生奶
  • Fluid 1.0 版发布,打通云原生高效数据使用的“最后一公里”
  • 软件测试--第十一章 设计和维护测试用例
  • 前端只允许一次函数调用
  • visdom使用时所遇的问题及解决方法
  • 密封类(sealed class)
  • 私域引流宝PHP源码 以及搭建教程
  • 磁盘管理 以及磁盘的分区 详细版
  • 加码多肤色影像技术 这是传音找到的“出海利器“?
  • C++方法封装成dll及C#调用示例
  • 定时清理Linux服务器缓存shell脚本
  • Guava常用方法
  • 干货分享:宏集物联网HMI通过S7 MPI协议采集西门子400PLC数据
  • 【Web API DOM11】节点操作
  • Unity 设置窗口置顶超级详解版
  • 编程后端:深入探索其所属的行业领域
  • ubuntu18.04离线源制作
  • 【DPDK学习路径】八、轮询
  • Mac环境下,简单反编译APK
  • 027、工具_redis-benchmark
  • 京准电钟 | 对比GPS,北斗卫星授时的场景有哪些?
  • 电脑桌面提醒做事的app 好用的桌面提醒app
  • ICC2:如何获取get_xx -filter后可用的属性有哪些?
  • SSL协议在实际生活中有哪些应用实例?
  • Python连接到Jira实例、登录、查询、修改和创建bug
  • 等保测评考试初级题大题部分
  • 【前端面试】动态表单篇
  • Mybatis save、saveOrUpdate、update的区别