Building Microservices with Spring Boot and Apache Thrift. Part 3. Asynchronous services

Posted on 4:12 PM  by Sergei Egorov
Have you thought about making your server asynchronous? How much time your server spent in database calls? External service calls? New shiny database library now supports asynchronous queries? It's time to processing requests in an async manner! And it's super easy with Facebook Swift!

Hardest part

How do you think, how much changes you need to do to make your Swift service asynchronous? Do you afraid of it? You already scheduled a week for this refactoring? Oh, that's nice:)

  import com.facebook.swift.service.ThriftMethod;
  import com.facebook.swift.service.ThriftService;
  +import com.google.common.util.concurrent.ListenableFuture;
   
  @ThriftService
  public interface TCalculatorService {
   
  @ThriftMethod
  - int calculate(int num1, int num2, TOperation op) throws TDivisionByZeroException;
  + ListenableFuture<Integer> calculate(int num1, int num2, TOperation op) throws TDivisionByZeroException;
  }
view rawTCalculatorService.patch hosted with ❤ by GitHub

Now it's time to update your service implementation. Are you ready?

  import com.example.calculator.protocol.TOperation;
  +import com.google.common.util.concurrent.*;
  import org.springframework.stereotype.Component;
   
  import com.example.calculator.service.CalculatorService;
  import org.springframework.beans.factory.annotation.Autowired;
   
  +import java.util.concurrent.*;
  +
  @Component
  public class CalculatorServiceHandler implements TCalculatorService {
   
  @Autowired
  CalculatorService calculatorService;
   
  + ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
  +
  @Override
  - public int calculate(int num1, int num2, TOperation op) throws TDivisionByZeroException {
  + public ListenableFuture<Integer> calculate(int num1, int num2, TOperation op) throws TDivisionByZeroException {
  + return JdkFutureAdapters.listenInPoolThread(
  + scheduledExecutorService.schedule(() -> {
  switch(op) {
  case ADD:
  return calculatorService.add(num1, num2);
  @@ -32,5 +40,7 @@
  default:
  throw new IllegalArgumentException("Unknown operation " + op);
  }
  + }, 2, TimeUnit.SECONDS)
  + );
  }
  }
view rawCalculatorServiceHandler.patch hosted with ❤ by GitHub

Hope you noticed this ScheduledExecutorService and 2 seconds delay. You shouldn't use it in your real application, this is just for example of promises (unless you want to simulate work for future optimizations like this guy: http://thedailywtf.com/articles/The-Speedup-Loop )

That's it, now your server is asynchronous. There is more - your clients can consume your service like before in non-async manner. How cool is that?

Full source code at GitHub: https://github.com/bsideup/spring-boot-swift/tree/async

Previous parts:

  • Building Microservices with Spring Boot and Apache Thrift. Part 1
  • Building Microservices with Spring Boot and Apache Thrift. Part 2. Swifty services

转载于:https://www.cnblogs.com/bigben0123/p/7483819.html

http://bsideup.blogspot.com/2015/04/spring-boot-thrift-part3.html相关推荐

  1. scheduled 一秒钟执行一次_spring boot的Scheduled帮你实现定时任务,spring boot实践(11)...

    01 spring boot读取配置信息 02 多环境配置 03 处理全局异常 04 spring boot admin 05 spring mvc + mybatis 06 spring boot ...

  2. springboot templates读取不到_整合spring mvc + mybatis,其实很简单,spring boot实践(5)

    01 spring boot读取配置信息 02 多环境配置 03 处理全局异常 04 spring boot admin 主要通过spring boot整合spring mvc 以及mybatis实现 ...

  3. 一个SpringBoot问题就干趴下了?我却凭着这份PDF文档吊打面试官(Spring Boot知识点+详解)

    随着 Spring Boot 使用越来越广泛,Spring Boot 已经成为 Java 程序员面试的知识点,很多同学对 Spring Boot 理解不是那么深刻,经常就会被几个连环追问就给干趴下了! ...

  4. Spring Boot(04)——创建自己的自动配置

    创建自己的自动配置 当你的应用需要以jar包的形式提供给其它应用使用时,可以考虑把它们封装为一个Spring Boot Starter.即该jar包是可以自动添加需要引用的依赖项,也能够对核心功能进行 ...

  5. Spring Boot基础学习笔记04:Spring Boot加载自定义配置文件

    文章目录 零.学习目标 1.熟悉使用@PropertySource加载配置文件 2.熟悉使用@ImportResource加载XML配置文件 3.掌握使用@Configuration编写自定义配置类 ...

  6. Spring Boot 学习系列(04)—分而治之,多module打包

    此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 明确功能,各司其职 在一个结构清晰的项目中,一个没有module划分的结构显然不是最佳实践.有人会说可以在同 ...

  7. springboot templates读取不到_精通 Spring Boot 系列 04

    阅读全文,约 12 分钟这是江帅帅的第004篇原创 1. Web 开发的支持 使用 Spring Boot 实现 Web 开发更加便捷了,因为直接依赖 spring-boot-starter-web ...

  8. spring boot实战(第二篇)事件监听

    前言 spring boot在启动过程中增加事件监听机制,为用户功能拓展提供极大的便利. 支持的事件类型四种 ApplicationStartedEvent ApplicationEnvironmen ...

  9. spring boot注释_使用Spring Boot和注释支持配置Spring JMS应用程序

    spring boot注释 1.简介 在以前的文章中,我们学习了如何使用Spring JMS配置项目. 如果查看有关使用Spring JMS进行消息传递的文章介绍 ,您会注意到它是使用XML配置的. ...

最新文章

  1. 非极大值抑制_非极大值抑制(Non-Maximum Suppression)
  2. python概念与术语,python之基础篇(四)
  3. Android MVP
  4. 什么是计算机领域的实时系统
  5. 如何编写有效的Bug Report
  6. Illustrator中文版教程,如何在AI中以不同的方式组合形状?
  7. 合成分红游戏源码_ThinkPHP宠物养成合成类游戏APP陀螺世界程序源码 已优化
  8. python期权定价公式_期权及 Black-Scholes模型的python实现
  9. 服务器flv文件播放不了,解决win2003服务器不能播放flv文件格式的问题
  10. 《中国医学大成》目录
  11. 长度标注神器----MarkMan
  12. 串行异步通信_什么是并行传输、串行传输、异步传输?
  13. linux第一阶段学习笔记基础
  14. placement new理解
  15. 一个39岁程序员的应聘被拒 | 文末送书
  16. 【系统设计】Verilog语法及示例(2)
  17. matplotlib animation动画保存(save函数)详解
  18. 计算机教师个人诊改总结,教师个人诊改报告
  19. BZOJ3790 神奇项链
  20. Java编程基础-方法

热门文章

  1. Visual Studio 2008下AJAX的设置
  2. Pytorch —— 权值初始化
  3. 计算机视觉模型、学习和推理
  4. 去哪儿-15-keep-alive
  5. Markdown--行内公式编辑
  6. 计算机视觉:相机成像原理:世界坐标系、相机坐标系、图像坐标系、像素坐标系之间的转换(转载)
  7. 手把手教你搭建Nginx-rtmp流媒体服务器+使用ffmpeg推流
  8. STM32 - 定时器的设定 - 基础- 0A - Timers and external trigger synchronization - 定时器和外部触发的同步
  9. sql server链接远程服务器上的csv文件_批量监控联想服务器硬件状态工具
  10. linux删除命令_【小技巧】Linux命令的快捷键