Reputation: 5202
I started learning Akka Streams, which is a framework for processing data with back-pressure functionality. The library is part of Akka that describes itself as:
Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient message-driven applications on the JVM.
These capabilities comes from the nature of Akka actors. However, from my perspective, stream processing and actors are irrelevant concept to each other.
Question: Do Akka Streams take advantage of these features of Akka actors? If yes, would you explain how actors help streams?
Upvotes: 4
Views: 740
Reputation: 17933
A good starting point is the akka stream quickstart.
Yes, an Actor
is used to "materialize" each {Source
, Flow
, Sink
} of a Stream
. This means that when you create a Stream nothing actually happens until the stream is materialized, typically via the .run()
method call.
As an example, here is a Stream being defined:
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Source, Flow, Sink}
val stream = Source.single[String]("test")
.via(Flow[String].filter(_.size > 0))
.to(Sink.foreach{println})
Even though the stream is now a val
no computation has actually happened. The Stream is just a recipe for computation. To actually kick-off the work the Stream needs to be materialized. Here is an example that does not use implicits to clearly show how materialization occurs:
val actorSystem = ActorSystem()
val materializer = ActorMaterializer()(actorSystem)
stream.run()(materializer) //work begins
Now 3 Actors (at least) have been created: 1 for the Source.single
, 1 for the Flow.filter
, and 1 for the Sink.foreach
. Note: you can use the same materializer
to initiate other streams
val doesNothingStream = Source.empty[String]
.to(Sink.ignore)
.run()(materializer)
Upvotes: 5
Reputation: 879
Akka Streams is a higher level abstraction than actors. It's an implementation of Reactive Streams which builds on top of the actor model. It takes advantage of all the actor features because it uses actors.
You can even go back to using actors directly in any part of the stream. Look at ActorPublisher and ActorSubscriber.
Upvotes: 6