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 学习<四 ...
 
随机推荐
- hunnu--11548--找啊找啊找朋友
			
找啊找啊找朋友 Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 14, ...
 - 多通道(比方RGB三通道)卷积过程
			
今天一个同学问 卷积过程好像是对 一个通道的图像进行卷积, 比方10个卷积核,得到10个feature map, 那么输入图像为RGB三个通道呢,输出就为 30个feature map 吗, 答案肯定 ...
 - 微信小程序TabBar的使用
			
一.TabBar使用步骤 1.创建所需要的界面和所需要的图片: 2.配置文件: 我们找到项目根目录中的配置文件 app.json 加入如下配置信息 "tabBar": { &quo ...
 - Android 关于软键盘
			
一..弹出的时候显示Editext框 添加布局replay_input <?xml version="1.0" encoding="utf-8"?> ...
 - multiple-value uuid.NewV4() in single-value context
			
[root@t ~]# go get github.com/aliyun/aliyun-sts-go-sdk/sts# github.com/aliyun/aliyun-sts-go-sdk/sts/ ...
 - Delphi String的散漫记录,真是知识无数,陷阱无数
			
真是膜拜Delphi C++ Builder编译器的作者们,要下多少苦功夫才能解决如此之多的问题,制造出一个神级作品给世人享用.另外以我的编程经验所能想到很麻烦但却是必须的还有两个地方,一个是Form ...
 - Hadoop集群部署-Hadoop 运行集群后Live Nodes显示0
			
可以尝试以下步骤解决: 1 ,分别删除:主节点从节点的 /usr/local/hadoop-2.6.2/etc/tmp 下得所有文件; 2: 编辑cd usr/local/hadoop-2.6. ...
 - (转)nginx-rtmp-module和ffmpeg搭建实时HLS切片
			
1.rtmp服务器 nginx+pcre+zlib+openssl+nginx-rtmp-module ./configure \ --prefix=/usr/local/nginx \ --sbin ...
 - 关于引用WebLogic.jar时遇到NoClassDefFoundError问题的解决方法
			
前段时间在做一个项目开发时,需要用到weblogic.jndi.WLInitialContextFactory,所以按照以前的经验,将WebLogic.jar添加到Build Path中.可是在执行时 ...
 - mysql父子查询
			
https://segmentfault.com/a/1190000007531328