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 官方就已经 ...
随机推荐
- 6 基于Servlet 的分页
实现上图的分页功能,每页显示3条数据,每页显示3页供用户访问,点击左右双箭头时,可以跳转至上一个或者下一个大页,如点击右双箭头显示如下: 1 连接数据库的工具类 package com.latiny. ...
- Go To Oracle
1.下载mingw (gcc 编译)---win32 2.下载OCI最新版,存放于C:\instantclient_12_1 ---win32 3.下载OCI SDK最新版,存放于C:\ins ...
- 10分钟让你的代码更加pythonic
参考: https://blog.csdn.net/g8433373/article/details/80709116
- python学习笔记(5)-time库的使用
import time 一.时间获取函数 time(), ctime(),gmtime() >>> import time >>> time.time() 1524 ...
- 错误模块名称: KERNELBASE.dll错误
今天在部署一个C/S程序的时候出了bug,日志都没有记载:本地调试当然是没问题的,所以不是代码问题,百度之发现KERNELBASE.dll这个文章说的比较靠谱,仔细研究了自己的配置文件后,果然是配置文 ...
- jdbc工具类2..0
一.创建外部文件 url=jdbc:mysql:///qy66 use=root password=root driver=com.mysql.jdbc.Driver 二.创建工具类 package ...
- PLSQL过期:Your trial period for PL/SQL Developer is over .If you want to continue using this software ,you must purchase the retail version.
PLSQL过期:Your trial period for PL/SQL Developer is over .If you want to continue using this software ...
- JarvisOJ Basic 爱吃培根的出题人
听说你也喜欢吃培根?那我们一起来欣赏一段培根的介绍吧: bacoN is one of aMerICa'S sWEethEartS. it's A dARlinG, SuCCulEnt fOoD tH ...
- SpringBoot部署jar与war
jar部署与启动/关闭 1.打包 clean 清理已有target目录 package 重新打包 获取打包路径,通过 scp命令发送到服务器端,scp -P ${port} ${.jar} ${use ...
- Luogu5205 【模板】多项式开根(NTT+多项式求逆)
https://www.cnblogs.com/HocRiser/p/8207295.html 安利! 写NTT把i<<=1写成了i<<=2,又调了一年.发现我的日常就是数组开 ...