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. [转帖]windows+xshell+xming访问非桌面版Linux服务器

    windows+xshell+xming访问非桌面版Linux服务器 2016年06月05日 00:09:11 jxxiaohou 阅读数:11996 标签: Linux 更多 个人分类: Linux ...

  2. spring AOP的用法

    AOP,面向切面编程,它能把与核心业务逻辑无关的散落在各处并且重复的代码给封装起来,降低了模块之间的耦合度,便于维护.具体的应用场景有:日志,权限和事务管理这些方面.可以通过一张图来理解下: Spri ...

  3. flutter屏幕适配

    现在的手机品牌和型号越来越多,导致我们平时写布局的时候会在个不同的移动设备上显示的效果不同, 比如我们的设计稿一个View的大小是300px,如果直接写300px,可能在当前设备显示正常,但到了其他设 ...

  4. JavaSE从入门到精通

      1.JavaSE的安装 windows下安装完成后,配置环境变量如下: JAVA_HOME       C:\Program Files (x86)\Java\jdk1.8.0_91 CLASSP ...

  5. Delphi之TStrings和TStringLists类

    Delphi之TStrings和TStringLists类 有些类不是组件,但它们支持存在的组件.这些类是其他组件的典型属性,直接由TPersistent派生,如TStrings.TCanvas和TC ...

  6. Maven自动部署jar包到Neuxs

      1. 修改maven配置(setting.xml) 添加neuxs的用户名和密码: <server> <id>my-deploy-release</id> &l ...

  7. ExportHandler.ashx

    using KYZWeb.Common;using Liger.Data;//using Microsoft.Office.Interop.Excel;using System;using Syste ...

  8. 学习 Spring (十一) 注解之 Spring 对 JSR 支持

    Spring入门篇 学习笔记 @Resource Spring 还支持使用 JSR-250 中的 @Resource 注解的变量或 setter 方法 @Resource 有一个 name 属性,并且 ...

  9. hibernate一对多映射文件的配置

    其中一个Customer对应多个LinkMan Customer的映射文件 Customer.hbm.xml-------------->一对多 <?xml version="1 ...

  10. [Codeforces235D]Graph Game——概率与期望+基环树+容斥

    题目链接: Codeforces235D 题目大意:给出一棵基环树,并给出如下点分治过程,求点数总遍历次数的期望. 点分治过程: 1.遍历当前联通块内所有点 2.随机选择联通块内一个点删除掉 3.对新 ...