Lagom学习 六 Akka Stream
lagom中的stream 流数据处理是基于akka stream的,异步的处理流数据的。如下看代码:
流式service好处是:
A: 并行: hellos.mapAsync(8, name -> helloService.hello(name).invoke())), 八个线程并行处理;
B: 异步: 返回completedFuture, 使用基于Web Socket的方式。
C: 全双工:
package com.example.hello.stream.impl; import akka.NotUsed;
import akka.stream.javadsl.Source;
import com.lightbend.lagom.javadsl.api.ServiceCall;
import com.example.hello.hello.api.HelloService;
import com.example.hello.stream.api.StreamService; import javax.inject.Inject; import static java.util.concurrent.CompletableFuture.completedFuture; /**
* Implementation of the HelloString.
*/
public class StreamServiceImpl implements StreamService { private final HelloService helloService;
private final StreamRepository repository; @Inject
public StreamServiceImpl(HelloService helloService, StreamRepository repository) {
this.helloService = helloService;
this.repository = repository;
} @Override
public ServiceCall<Source<String, NotUsed>, Source<String, NotUsed>> directStream() {
return hellos -> completedFuture(
hellos.mapAsync(8, name -> helloService.hello(name).invoke()));
} @Override
public ServiceCall<Source<String, NotUsed>, Source<String, NotUsed>> autonomousStream() {
return hellos -> completedFuture(
hellos.mapAsync(8, name -> repository.getMessage(name).thenApply( message ->
String.format("%s, %s!", message.orElse("Hello"), name)
))
);
}
}
调用streamed service 接口的方式:
Source<String, ?> response = await(streamService.directStream().invoke(
Source.from(Arrays.asList("a", "b", "c"))
.concat(Source.maybe())));
List<String> messages = await(response.take(3).runWith(Sink.seq(), mat));
assertEquals(Arrays.asList("Hello, a!", "Hello, b!", "Hello, c!"), messages);
private <T> T await(CompletionStage<T> future) throws Exception { //等待10秒 拿结果
return future.toCompletableFuture().get(10, TimeUnit.SECONDS);
}
Lagom学习 六 Akka Stream的更多相关文章
- (六)jdk8学习心得之Stream流
六.Stream流 1. 什么是stream流 现阶段,可以把stream流看成一个高级版的Iterator.普通的Iterator只能实现遍历,遍历做什么,就需要具体些功能代码函数了.而这个stre ...
- Lagom学习 (二)
以一个官方的例子,开启lagom的学习之旅. 1: git clone https://github.com/lagom/activator-lagom-java-chirper.git. 2: ...
- Akka Stream之Graph
最近在项目中需要实现图的一些操作,因此,初步考虑使用Akka Stream的Graph实现.从而学习了下: 一.介绍 我们知道在Akka Stream中有三种简单的线性数据流操作:Source/Flo ...
- Lagom学习(一)
Lagom是JAVA系下响应式 微服务框架,其特性包括: 目前,大多数已有的微服务框架关注于简化单个微服务的构建,Lagom将其扩展到了微服务所构成的系统,分布式系统的复杂性. 同步通信使用HTTP, ...
- Hbase深入学习(六) Java操作HBase
Hbase深入学习(六) ―― Java操作HBase 本文讲述如何用hbase shell命令和hbase java api对hbase服务器进行操作. 先看以下读取一行记录hbase是如何进行工作 ...
- TweenMax动画库学习(六)
目录 TweenMax动画库学习(一) TweenMax动画库学习(二) TweenMax动画库学习(三) Tw ...
- Akka Stream文档翻译:Motivation
动机 Motivation The way we consume services from the internet today includes many instances of streami ...
- 报错:Flink Could not resolve substitution to a value: ${akka.stream.materializer}
报错现象: Exception in thread "main" com.typesafe.config.ConfigException$UnresolvedSubstitutio ...
- SVG 学习<六> SVG的transform
目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...
随机推荐
- 五分钟了解 Service Mesh
1 背景 1.1 多语言 微服务理念是提倡不同业务使用最适合它的语言开发,现实情况也确实如此,尤其是AI的兴起,一般大型互联网公司存在 C/C++.Java.Golang.PHP.Pyth ...
- 前端要给力之:语句在JavaScript中的值
文件夹 文件夹 问题是语句有值吗 那么说你骗我咯 有啥米用呢 研究这个是不是闲得那个啥疼 ES5ES6有什么差异呢 结论是ES6是改了规则但更合理 最后不不过if语句 这两天在写语言精髓那本书的第三版 ...
- 【CF830C】Bamboo Partition 分块
[CF830C]Bamboo Partition 题解:给你n个数a1,a2...an和k,求最大的d使得$\sum\limits_{i=1}^n((d-a[i] \% d) \% d) \le k$ ...
- 关于EF输出sql的执行日志
sqlserver中可以使用sql profiler:但是mysql当中无法查看:只能借助于组件: ADO.NET Entity Framework CodeFirst 如何输出日志(EF4.3) 用 ...
- /etc/init.d/iptables stop
/etc/init.d/iptables stop systemctl stop firewalld.service [root@bigdata-server-01 myrestserve ...
- mysql一:操作数据库
创建数据库是指在数据库空间中划出一块空间用来存储相关的数据,表就是存储在对应的数据库里面.首先来看下查看数据库的命令:show databases. 这个是用来查询数据库空间下所有的数据库,其中inf ...
- reduce python 的用法
1.查看reduce 的用法 在python 命令查看 import functools help(functools) help(functools.reduce) 或者 from functool ...
- 【python】python调用shell方法
在python脚本中,有时候需要调用shell获取一下信息,下面介绍两种常用的调用方法. 第一种,os.system() 这个函数获取的是命令的执行状态,比如 >>> import ...
- 测试drawable animation
public class DAActivity extends Activity implements OnClickListener { private ImageView iv_da_mm; pr ...
- 序列化组件(get/put/delete接口设计),视图优化组件
一 . 知识点回顾 1 . 混入类 , 多继承 class Animal(object): def eat(self): print("Eat") def walk(self): ...