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 官方就已经 ...
随机推荐
- [微软].net2.1 的兼容支持情况.
dotnet core 现在看起来 不支持xp 不支持 win10 最早版本的 和 版本. 军工客户 如果还不升级 winxp的话 可能还是没法用(客户端运行时) 不过根据前段时间安装的国产linux ...
- python之读取和写入csv文件
写入csv文件源码: #输出数据写入CSV文件 import csv data = [ ("Mike", "male", 24), ("Lee&quo ...
- php new self()
php里new self() 一般在类内部使用,作用是对自身类实例化 <?php class test{ public function __construct(){ echo ' ...
- C#使用MemoryStream类读写内存
MemoryStream和BufferedStream都派生自基类Stream,因此它们有很多共同的属性和方法,但是每一个类都有自己独特的用法.这两个类都是实现对内存进行数据读写的功能,而不是对持久性 ...
- html5 autoplay不起作用 Uncaught (in promise) DOMException: play() failed because the user didn't interac
chrome://flags/#autoplay-policy
- 手机连接WiFi有感叹号x怎么回事?如何消除手机WiFi感叹号?
经过多年的革新,现在的安卓系统已经非常优秀了,某些程度已经超越iOS,卡顿和耗电也不再是安卓系统的代名词了.而为了体验到最优秀的安卓系统,不少人都会购买海外的手机,因为海外手机的安卓系统都比较精简,非 ...
- mysql必须知道的
https://blog.csdn.net/xlgen157387/article/details/73691848
- Matlab提供了两种除法运算:左除(\)和右除(/)
Matlab提供了两种除法运算:左除(\)和右除(/).一般情况下,x=a\b是方程a*x =b的解,而x=b/a是方程x*a=b的解.例:a=[1 2 3; 4 2 6; 7 4 9]b ...
- 动态sql and在前 逗号在后
- codeforces401C
Team CodeForces - 401C Now it's time of Olympiads. Vanya and Egor decided to make his own team to ta ...