4

My akka-streams learn-o-thon continues. I'd like to integrate my akka-streams application with akka-cluster and DistributedPubSubMediator.

Adding support for Publish is fairly straight forward, but the Subscribe part I'm having trouble with.

For reference, a subscriber is given as follows in the Typesafe sample:

class ChatClient(name: String) extends Actor {
  val mediator = DistributedPubSub(context.system).mediator
  mediator ! Subscribe("some topic", self)

  def receive = {
    case ChatClient.Message(from, text) =>
      ...process message...
  }
}

My question is, how should I integrate this actor with my flow, and how should I ensure I'm getting publish messages in the absence of stream backpressure?

I'm trying to accomplish a pubsub model where one stream may publish a message and another stream would consume it (if subscribed).

Will I Am
  • 2,614
  • 3
  • 35
  • 61

2 Answers2

8

You probably want to make your Actor extend ActorPublisher. Then you can create a Source from it and integrate that into your stream.

See the docs on ActorPublisher here: http://doc.akka.io/docs/akka-stream-and-http-experimental/2.0.3/scala/stream-integrations.html

ulas
  • 463
  • 5
  • 10
jamesmulcahy
  • 307
  • 1
  • 8
  • The part I'm not getting is that you normally create one actor per subscription. So, as I process subscription messages, the Flow itself becomes an actor factory. A stream would create one actor for each subscription it desires, and those actors would be tied to the life of the stream (unless I receive an unsubscribe request). I will re-read the documentation, maybe a light bulb will go off. – Will I Am Feb 03 '16 at 03:34
  • 1
    I figured out how to create the per-stream actor, still working on working with two sources. Thanks for the suggestion, i will update. – Will I Am Feb 03 '16 at 04:30
2

The other answers are outdated: they suggest using ActorPublisher, which has been deprecated since version 2.5.0.

For those interested in a current approach, Colin Breck wrote an excellent series in his blog about integrating Akka Streams and Akka actors. Over the course of the series, Breck fleshes out a system that begins with Akka Streams and plain actors, then incorporates Akka Cluster and Akka Persistence. The first post in the series is here (the distributed stream processing piece is in part 3).

Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54