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. Laravel设置软删除及其恢复系列操作

    软删除及其相关实现 在模型类中要使用SoftDeletestrait并设置$date属性数组 <?php namespace App\Models; use Illuminate\Databas ...

  2. 使用cmd命令删除文件夹下所有文件

    rmdir 删除整个目录 好比说我要删除 222 这个目录下的所有目录和档案,这语法就是: rmdir /s/q 222 其中: /s 是代表删除所有子目录跟其中的档案. /q 是不要它在删除档案或目 ...

  3. (一)类数组对象NodeList

    NodeList对象的特点: NodeList是一种类数组对象,用于保存一组有序的节点. 可以通过方括号语法来访问NodeList的值,有item方法与length属性. 它并不是Array的实例,没 ...

  4. dart正则

    1.前言 API中对于正则表达式的注释是:正则表达式的规范和语义与JavaScript相同详细的规范可以参考:http://ecma-international.org/ecma-262/5.1/#s ...

  5. llegalStateException: getWriter() has already been called for this response

    我使用Springmvc的处理器进行向AJAX传值时出现的问题 当我使用 PrintWriter out = response.getWriter();out.print("用户不存在,请先 ...

  6. python之路--模块和包

    一 . 模块 ⾸先,我们先看⼀个老⽣常谈的问题. 什么是模块. 模块就是⼀个包含了python定义和声明的⽂件, ⽂件名就是模块的名字加上.py后缀. 换句话说我们⽬前写的所有的py⽂件都可以看成是⼀ ...

  7. ArcGIS DeskTop 10.2 的安装与破解

    ArcGIS DeskTop 10.2套件作为一组常用的ArcGIS软件为我们提供了对地图原始数据进行加工以及各种操作,通过这组软件我们能够很好地定制我们最终的地图样式,但是更多的时候我们需要对这组软 ...

  8. flask 保存文件到 七牛云

    上篇文章队长讲述了如何把前端上传的文件保存到本地项目目录 本篇 讲述一下把前端上传的文件保存到 第三方存储(七牛云) 七牛云相关步骤思路: 首先 进去七牛云官网,注册并实名认证来获取一个七牛云账号和存 ...

  9. codeforces545C

    Woodcutters CodeForces - 545C Little Susie listens to fairy tales before bed every day. Today's fair ...

  10. Nginx 如何通过连接池处理网络请求

    L:35-36 worker_connections 默认 512个 这个链接需要设置的  worker_cpu_affinity 0001 0010 0100 1000;关联CPU connecti ...