Creating a source from EventStream

I am using PlayFramework 2.5.3 and would like to create akka.stream.scaladsl.Source from akka.event.EventStream (eventstream is part of the actors system). The event flow generates events of a certain type, so I will need to subscribe to this type of events and click them using play.api.mvc.Results.chunked . Is there an easy way to create such a Source using Akka Streams 2.4.5?

+5
source share
1 answer

You can use Source.actorRef along with a subscription. Source.actorRef is the source that materializes in ActorRef , so you can do this:

 // choose the buffer size of the actor source and how the actor // will react to its overflow val eventListenerSource = Source.actorRef[YourEventType](32, OverflowStrategy.dropHead) // run the stream and obtain all materialized values val (eventListener, ...) = eventListenerSource .viaMat(...)(Keep.left) <...> .run() // subscribe the source actor to the stream actorSystem.eventStream.subscribe(eventListener, classOf[YourEventType]) // now events emitted by the source will go to the actor // and through it to the stream 

Please note that the source of ActorRef somewhat limited, for example, it naturally does not support the overpressure overflow strategy for its internal buffer. You can use Source.actorPublisher with an actor who extends ActorPublisher[YourEventType] , this will give you a little more control. However, since EventStream is a pure push-based source, you cannot do more with ActorPublisher than with Source.actorRef , so you can simply use a simpler approach.

+5
source

All Articles