Play 2.5 akka.stream.Materializer 有什么用? [英] Play 2.5 What is akka.stream.Materializer useful for?

查看:24
本文介绍了Play 2.5 akka.stream.Materializer 有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 Play 2.5,我想知道这样做的目的是什么:

I started to use Play 2.5 recently, and I was wondering what the purpose of doing the following was:

@Inject() (implicit val mat: Materializer)

我有几段代码无法工作并因此解决了这个问题,但我仍然没有看到具体化器在做什么.

I had several pieces of code not working and solved that issue thanks to this, but I still don't see what the materializer is doing.

谢谢

推荐答案

Materializing 意味着产生 图表

物化器使 actors 执行一个图形以产生这些结果.

Materializing means producing the results of a graph

A materializer makes actors execute a graph to produce those results.

最简单形式的图由提供元素的源和消耗元素的接收器组成.

A graph, in its simplest form, consists of a source that provides elements, and a sink that consumes elements.

这是一个提供整数范围的源(在本例中,整数是我们的元素):

Here's a source that provides a range of integers (in this example, integers are our elements):

val source = Source(1 to 10)

这是一个汇总它从源获得的所有整数的接收器:

And here's a sink that sums all the integers it gets from a source:

val sink = Sink.fold[Int, Int](0)(_ + _)

我们连接 source 和 sink 得到一个图:

We connect source and sink to get a graph:

val graph = source.toMat(sink)(Keep.right)

请注意,在创建图形时,没有计算,在我们的例子中是加法.代码是声明性,图表描述了我们想要如何转换我们的数据,但它是别人的实际执行计算的工作:图形是 蓝图.

Mind that no computation, addition in our case, is performed when creating a graph. The code is declarative, the graphs describe how we want to transform our data but it is someone else's job to actually perform the computation: Graphs are blueprints.

现在,具体化器呢?当我们运行图表时,物化器会采取行动:

Now, what about the materializer? The materializer takes action when we run the graph:

implicit val materializer = ActorMaterializer()
val futureResult = graph.run()

当我们run()图形时,物化器获取图形并使actor执行图形中指定的数据转换;在这个例子中,这是添加整数.

When we run() the graph, the materializer takes the graph and makes actors execute the data transformations specified in the graph; in this example, that's adding integers.

将图表想象成建造房屋的蓝图,将物化器想象成看蓝图的工头,告诉建造者如何根据蓝图实际建造房屋,这可能会有所帮助.这个类比中的构建器对应于 Akka 中的 actor.

It might be helpful to imagine the graph as a blueprint to build a house, the materializer as the foreman looking at the blueprint, telling the builders how to actually build the house according to the blueprint. The builders in this analogy correspond to actors in Akka.

您的几段代码现在可以工作的原因是,您可以通过实现器提供一种执行图形的方法.图在 Akka HTTP 中大量使用,Play 用于 HTTP请求和响应.

The reason why several pieces of your code now work, is that with the materializer you provide a way to execute graphs. Graphs are heavily used in Akka HTTP which Play uses for HTTP requests and responses.

您在评论中提到的 WSClient 使用图来执行它的请求,因此需要一个物化器.

The WSClient you mentioned in your comment uses graphs to perform its requests and thus needs a materializer.

以下是创建和运行图表的完整示例:

Here's a full example of creating and running a graph:

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Keep, Sink, Source}

object Graphs extends App {
  // The start of our simple graph. It provides elements, integers in our case
  val source = Source(1 to 10)

  // The end of our graph. It processes the source's elements
  val sink = Sink.fold[Int, Int](0)(_ + _)

 /*
  * Connect source and sink.
  * Keep only the output values (i.e., the graph's right side).
  * Note that this is declarative: no result is computed until we run the graph.
  */
  val graph = source.toMat(sink)(Keep.right)

  // The system coordinates actors and provides threads for them
  implicit val actorSystem = ActorSystem()
  // The materializer makes actors execute graphs
  implicit val materializer = ActorMaterializer()

  // Running the graph means that the materializer assigns actors to execute
  // the graph from start (source) to end (sink)
  val futureResult = graph.run()

  // Use the actor system's execution context, which provides threads,
  // to print the result of running the graph
  implicit val executionContext = actorSystem.dispatcher
  futureResult.foreach(res => println(s"Result of running the graph: $res"))

  actorSystem.terminate().foreach(_ => println("System is shut down"))
}

把它放在你的 build.sbt 中,让 Akka 的流库在你的代码中可用:

Put this in your build.sbt to make Akka's stream library available in your code:

libraryDependencies += "com.typesafe.akka" %% "akka-stream" % "2.5.19"

有关来源的更多信息和水槽.

这篇关于Play 2.5 akka.stream.Materializer 有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆