Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Akka Stream, Source from function?

I want to have a Source that evaluates a function at given intervals and emits its output. As a workaround, I can do it with a Source.queue + offer, but haven't found a cleaner way to do it. Ideally I would have something like

def myFunction() = ....                     // function with side-effects 
Source.tick(1.second, 1.second, myFunction) // myFunction is evaluated at every tick

Any ideas?

like image 381
ticofab Avatar asked Apr 24 '17 13:04

ticofab


People also ask

Which are the 3 main components in a Akka stream?

Akka streams consist of three major components in it – Source, Flow and Sink.

What is source in Akka?

The starting point is called Source and can be a collection, an iterator, a block of code which is evaluated repeatedly or a org.reactivestreams.Publisher. A flow with an attached input and open output is also a Source. A flow may also be defined without an attached input or output and that is then a Flow.

What is the difference between Akka and Kafka?

Kafka is an event streaming platform, loosely residing in the Message-Oriented Middleware (MoM) space. Akka is an Actor Model — a mechanism for concurrent computation based on the concepts of agents, immutability, and message passing.

What is Akka Materializer?

Actor Materializer Lifecycle. The Materializer is a component that is responsible for turning the stream blueprint into a running stream and emitting the “materialized value”.


1 Answers

Probably the cleanest way is to use map

Source.tick(1.second, 1.second, NotUsed).map(_ ⇒ myFunction())
like image 153
Stefano Bonetti Avatar answered Nov 03 '22 09:11

Stefano Bonetti