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

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)
  + );
  }
  }

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:

http://bsideup.blogspot.com/2015/04/spring-boot-thrift-part3.html的更多相关文章

  1. 黑马_13 Spring Boot:04.spring boot 配置文件

    13 Spring Boot: 01.spring boot 介绍&&02.spring boot 入门 04.spring boot 配置文件 05.spring boot 整合其他 ...

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

    http://bsideup.blogspot.com/2015/04/spring-boot-thrift-part2.html     In previous article I showed y ...

  3. 黑马_13 Spring Boot:05.spring boot 整合其他技术

    13 Spring Boot: 01.spring boot 介绍&&02.spring boot 入门 04.spring boot 配置文件 05.spring boot 整合其他 ...

  4. 黑马_13 Spring Boot:01.spring boot 介绍&&02.spring boot 入门

    13 Spring Boot: 01.spring boot 介绍&&02.spring boot 入门 04.spring boot 配置文件 SpringBoot基础 1.1 原有 ...

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

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

  6. spring boot 尚桂谷学习笔记04 ---Web开始

    ------web开发------ 1.创建spring boot 应用 选中我们需要的模块 2.spring boot 已经默认将这些场景配置好了 @EnableAutoConfiguration ...

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

    http://blog.csdn.net/liaokailin/article/details/48186331 前言 spring boot在启动过程中增加事件监听机制,为用户功能拓展提供极大的便利 ...

  8. Spring Boot Reference Guide

    Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch,  ...

  9. 再见 Spring Boot 1.X ,Spring Boot 2.X 走向舞台中心

    2019年8月6日,Spring 官方在其博客宣布,Spring Boot 1.x 停止维护,Spring Boot 1.x 生命周期正式结束. 其实早在2018年7月30号,Spring 官方就已经 ...

随机推荐

  1. Day 4-3 os & sys模块

    常用方法: import os os.getcwd() # 获取当前程序的工作路径(python解释器的运行路径,不是脚本所在的路径.) os.listdir() # 获取当前程序根目录下的所有文件夹 ...

  2. Chrome---谷歌浏览器修改用户缓存文件夹 如何设置缓存路径

    1.首先我们在电脑上打开chrome浏览器,然后地址栏输入chrome://Version,然后按下回车键,找到个人资料路径一项. 2.接下来我们选中个人资料路径后面所有的信息,右键点击信息后选择“复 ...

  3. Sqoop 使用详解(内含对官方文档的解析)

    Sqoop 是 Cloudera 公司创造的一个数据同步工具,现在已经完全开源了. 目前已经是 hadoop 生态环境中数据迁移的首选,另外还有 ali 开发的 DataX 属于同类型工具,由于社区的 ...

  4. Python 命令行工具 argparse 模块使用详解

    先来介绍一把最基本的用法 import argparse parser = argparse.ArgumentParser() parser.parse_args() 在执行 parse_args() ...

  5. Front-end Job Interview Questions

    Front-end Job Interview Questions 前端面试 https://github.com/h5bp/Front-end-Developer-Interview-Questio ...

  6. 移动APP用例设计中的关键点(转载)

    http://www.51testing.com/html/52/n-4421752.html 在测试工作中我们需要不断的总结和储备自己的知识和经验,譬如具备特定属性.环境以及场景,如:PC,手机,智 ...

  7. yolo检测系列

    caffe版yolov3 https://github.com/eric612/Caffe-YOLOv3-Windows Windows版本darknet https://github.com/zha ...

  8. HttpWebRequest using Basic authentication

    System.Net.CredentialCache credentialCache = new System.Net.CredentialCache(); credentialCache.Add( ...

  9. model,map,MapAndVivew用于页面跳转时候使用的即跳转后才添加属性 这样再回调中无法使用 因为回调的前提是页面不调转;解决的方法是用responsewrite(普通的字符响应)

    model,map,MapAndVivew用于页面跳转时候使用的即跳转后才添加属性 这样再回调中无法使用 因为回调的前提是页面不调转:解决的方法是用responsewrite

  10. Nginx 当上游服务器返回失败时的处理办法

    陶辉95课 Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503  ...