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 官方就已经 ...
随机推荐
- MySQL5.5 安装配置方法教程
MySQL下载地址:http://dev.mysql.com/downloads/installer/ 1.首先进入的是安装引导界面 2.然后进入的是类型选择界面,这里有3个类型:Typical(典型 ...
- TextView不用ScrollViewe也可以滚动的方法
转自:http://www.jb51.net/article/43377.htm android TextView不用ScrollViewe也可以滚动的方法. TextView textview = ...
- Golang的select多路复用以及channel使用实践
看到有个例子实现了一个类似于核弹发射装置,在发射之前还是需要随时能输入终止发射. 这里就可以用到cahnnel 配合select 实现多路复用. select的写法用法有点像switch.但是和swi ...
- @Autowired 与@Resource的区别(详细)
参考:@Autowired 与@Resource的区别(详细) spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@Pos ...
- Python:matplotlib绘制线条图
线型图是学习matplotlib绘图的最基础案例.我们来看看具体过程: 下面我们将两条曲线绘制到一个图形里: 可以看到这种方式下,两个线条共用一个坐标轴,并且自动区分颜色. plot方法的核心是 ...
- 五、同一台MySQL服务器启动多个端口-为读写分离做准备
一.安装数据库 https://www.cnblogs.com/huiyi0521/p/10113280.html 二.使用 mysql 命令为 root 用户授权 mysql 远程连接服务 mysq ...
- MySQL 5.7 关闭严格模式
If your app was written for older versions of MySQL and is not compatible with strict SQL mode in My ...
- rediret 加/与不加/的区别
- Nginx 防盗链 secure_link 模块
L:76 需要通过 --with-http_secure_link_module 编译进Nginx secure_link 指令 Syntax: secure_link expression; Def ...
- python命令里运行正确但是pycharm里面运行就是报错的问题
这两天在学习爬虫,第一步就是 import scrapy class StackOverflowSpider(scrapy.Spider): 结果一直报错,说是scrapy没有spider这个方法,各 ...