http://bsideup.blogspot.com/2015/04/spring-boot-thrift-part3.html
Building Microservices with Spring Boot and Apache Thrift. Part 3. Asynchronous services

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的更多相关文章
- 黑马_13 Spring Boot:04.spring boot 配置文件
13 Spring Boot: 01.spring boot 介绍&&02.spring boot 入门 04.spring boot 配置文件 05.spring boot 整合其他 ...
- 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 ...
- 黑马_13 Spring Boot:05.spring boot 整合其他技术
13 Spring Boot: 01.spring boot 介绍&&02.spring boot 入门 04.spring boot 配置文件 05.spring boot 整合其他 ...
- 黑马_13 Spring Boot:01.spring boot 介绍&&02.spring boot 入门
13 Spring Boot: 01.spring boot 介绍&&02.spring boot 入门 04.spring boot 配置文件 SpringBoot基础 1.1 原有 ...
- Spring Boot 学习系列(04)—分而治之,多module打包
此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 明确功能,各司其职 在一个结构清晰的项目中,一个没有module划分的结构显然不是最佳实践.有人会说可以在同 ...
- spring boot 尚桂谷学习笔记04 ---Web开始
------web开发------ 1.创建spring boot 应用 选中我们需要的模块 2.spring boot 已经默认将这些场景配置好了 @EnableAutoConfiguration ...
- spring boot实战(第二篇)事件监听
http://blog.csdn.net/liaokailin/article/details/48186331 前言 spring boot在启动过程中增加事件监听机制,为用户功能拓展提供极大的便利 ...
- Spring Boot Reference Guide
Spring Boot Reference Guide Authors Phillip Webb, Dave Syer, Josh Long, Stéphane Nicoll, Rob Winch, ...
- 再见 Spring Boot 1.X ,Spring Boot 2.X 走向舞台中心
2019年8月6日,Spring 官方在其博客宣布,Spring Boot 1.x 停止维护,Spring Boot 1.x 生命周期正式结束. 其实早在2018年7月30号,Spring 官方就已经 ...
随机推荐
- DTW的原理及matlab实现
参考: https://www.cnblogs.com/Daringoo/p/4095508.html
- Django--CRM--modelformset的用法
一 . modelformset用法 其实和modelform方法差不多,只不过是显示的时候可以直接修改,显示的select的那种模式 from django.forms import modelfo ...
- django rest framework权限和认证
Django rest framework之权限 一.Authentication用户认证配置 1.四种验证及官网描述: BasicAuthentication 此身份验证方案使用HTTP基本身份验证 ...
- mktemp -t -d用法
mktemp命令用于建立暂存文件或者文件夹,帮助文档如下: Usage: mktemp [OPTION]... [TEMPLATE] Create a temporary file or direct ...
- Docker入门与实践
一.Docker介绍 docker官网:https://www.docker.com/ Docker hub地址: https://hub.docker.com/ 1.基本概念 Docker ...
- jenkins 邮箱设置
一.先设置管理员邮箱地址 二.设置邮箱
- adoquery怎样判断数据在缓存中有修改啊
ADOQry.Filtered:=false; ADOQry.Filtered:=true; ADOQry.FilterGroup:=fgPendingRecords ;Fil ...
- 微信小程序flex佈局
聲明:display:flex 換行flex-wrap:flex-wrap:nowrap(不換行).wrap(換行).wrap-reserve(第一行在下面): 主軸對齊(橫向對齊)justify-c ...
- webpack始出来
一直想好好整理一下webpack,现在就整理吧. 总结自己的实际搭建的整理情况,我还是要先对自己说一句,以后给文件夹起名字的时候不要用一些特殊的关键字,比如我在做这个demo的时候,我用的文件夹名称叫 ...
- jpa 比较复杂的查询和用in关键字
in关键字使用代码