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 学习<四 ...
随机推荐
- markdownPad2 绿色破解版安装
markdownpad2,默认为markdown传统风格,不能编辑分行的代码段,可以在设置里改为Github 风格,但需要付费激活,以下是绿色安装包,附激活方法 下载地址 https://pan.ba ...
- 关于一致/非一致代码段与TSS 关系的个人看法
[0]概念定义 0.1)一致代码段: 简单理解,就是操作系统拿出来被共享的代码段,可以被低特权级的用户直接调用访问的代码, 但是特权级高的程序不允许访问特权级低的数据. 通常这些共享代码,是" ...
- WPF自定义搜索框代码分享
首先下载搜索图标: 控件中的搜索图标下载地址:http://www.easyicon.net/1183666-Search_icon.html 搜索框设计过程比较简单: 1.先定义一个Rectangl ...
- Darwin做直播时对ReflectorSession引用数的控制
在之前的博客中,我们提到了如何用Darwin&live555实现直播过程,那么更进一步,当直播结束时,我们需要关闭所有正在收看的客户端,并且delete转发会话ReflectorSession ...
- IIS的ARR实现站点的负载均衡 nginx 对比
windows下使用IIS的ARR实现站点的负载均衡 - CSDN博客 https://blog.csdn.net/zzy7075/article/details/73294713 IIS的ARR实现 ...
- wx.onNetworkStatusChange(function (res) 监听网络状态变化 实践方案
网络状态 · 小程序 https://developers.weixin.qq.com/miniprogram/dev/api/device.html#wxonnetworkstatuschangec ...
- 504 Gateway Timeout Error 502 Bad Gateway
总结 1. 502没有收到相应,或者收到了但不及时? cannot get a response in time 540收到了无效的响应 received an invalid response fr ...
- Hadoop实战-Flume之Source replicating(十四)
a1.sources = r1 a1.sinks = k1 k2 a1.channels = c1 c2 # Describe/configure the source a1.sources.r1.t ...
- 流畅python学习笔记:第十七章:并发处理二
本章讨论python3.2引入的concurrent.futures模块.future是中文名叫期物.期物是一种对象,表示异步执行的操作 在很多任务中,特别是处理网络I/O.需要使用并发,因为网络有很 ...
- cordova 插件创建
peng@PENG-PC /E/_My_File_____/_work/MyCode/myCode/cordova-workspace/plugman-test/ABCD $ npm install ...